you can interact with redis using the built-in client:
类型常量
对象的名称
type命令输出值
redis_string
字符串对象
“string”
redis_list
列表对象
“list”
redis_hash
哈希对象
“hash”
redis_set
集合对象
“set”
redis_zset
有序集合对象
“zset”
对应的为decr。
增加指定数字:incrby,对应的为decrby。
获取距离失效的时间
rpush puts the new value at the end of the list.
lpush puts the new value at the start of the list.
lpop、rpop为移除列表的第一个/最后一个元素。
lrange gives a subset of the list. it takes the index of the first element you want to retrieve as its first parameter and the index of the last element you want to retrieve as its second parameter. a value of -1 for the second parameter means to retrieve elements until the end of the list.
llen returns the current length of the list.
lpop removes the first element from the list and returns it.
rpop removes the last element from the list and returns it.
note that the list now only has one element:
sadd adds the given value to the set.
srem removes the given value from the set.
sismember tests if the given value is in the set. it returns 1 if the value is there and 0 if it is not.
smembers returns a list of all the members of this set.
sunion combines two or more sets and returns the list of all elements.
sets are a very handy data type, but as they are unsorted they don’t work well for a number of problems. this is why redis 1.2 introduced sorted sets.
a sorted set is similar to a regular set, but now each value has an associated score. this score is used to sort the elements in the set.
in these examples, the scores are years of birth and the values are the names of famous hackers.
simple strings, sets and sorted sets already get a lot done but there is one more data type redis can handle: hashes.
hashes are maps between string fields and string values, so they are the perfect data type to represent objects (eg: a user with a number of fields like name, surname, age, and so forth):
to get back the saved data use hgetall:
you can also set multiple fields at once:
if you only need a single field value that is possible as well:
redis是一个基于key-value的存储系统,但同样提供了许多其他的使用的组件,比如subscribe、publish这两个命令。这两个命令能让你非常方便地再两个进程中进行通信。
订阅/发布/取消订阅
subscribe、publish、unsubscribe
redis事务让一组命令在单个步骤执行。事务中有两个属性,说明如下:
在一个事务中的所有命令按顺序执行作为单个隔离操作。通过另一个客户端发出的请求在redis的事务的过程中执行,这是不可能的。
redis的事务具有原子性。原子意味着要么所有的命令都执行或都不执行。
redis的事务由指令多重发起,然后需要传递在事务,而且整个事务是通过执行命令exec执行命令列表。
首先需要下载jedis.jar,将其加入类路径,或者添加maven依赖。
代码示例如下:
转载:http://blog.csdn.net/foreverling/article/details/50960640