标签:style c class blog code java
其官方介绍是: Redis is what is called a key-value store, often referred to as a NoSQL database.
Redis is an open source, BSD licensed, advanced key-value store. It is often referred to as a data structure server sincekeys can contain strings, hashes, lists, sets and sorted sets.
意思是:redis是一个key-value (键值对) 存储的NoSQL数据库,是开源的,可以用来存储的key的类型分为字符串,哈希,list,set和可排序的set.
需要安装一下redis,最新的版本为2.8.9,下载地址为:Redis 2.8.9 is the latest stable version
下载完成后,ubuntu下执行命令进行解压:
tar -zxvf redis-2.8.9.tar.gz
cd redis-2.8.9 #首先,编译redis make #其次,启动redis cd src ./redis-server
cd src ./redis-cli redis> ping PONG redis> set foo bar OK redis> get foo "bar" redis> incr mycounter (integer) 1 redis> incr mycounter (integer) 2 redis>
连接好了以后就可以使用redis了.
如果不想安装redis,可以直接在线学习redis:http://try.redis.io/
同时,也可以直接查看其命令:http://redis.io/commands
SET server:name "fido" GET server:name => "fido"
这样就进行了赋值和取值,server:name就是key,value就是"fido".
127.0.0.1:6379> set a 10 OK 127.0.0.1:6379> decr a (integer) 9 127.0.0.1:6379> incr a (integer) 10 127.0.0.1:6379> del a (integer) 1 127.0.0.1:6379> get a (nil)
INCR是自增命令,默认每执行一次INCR命令自增1,而DECR则是自减命令,每次自减1,del则是删除指定的key.
INCR是原子性命令,对于其数据修改,要么全都执行,要么全都不执行.
SET resource:lock "Redis Demo 1" EXPIRE resource:lock 120 TTL resource:lock => 119 SET resource:lock "Redis Demo 2" TTL resource:lock => -1
EXPIRE命令是设置超时命令的,即如上面的例子,resource:lock只能存在120秒,超时即被删除.
TTL命令是查看key剩余存活时间.
RPUSH friends "Alice" RPUSH friends "Bob"
RPUSH是每次都将value添加到list的最后面.
LPUSH friends "Sam"
LPUSH 是每次都将value添加到list的最前面.
LRANGE friends 0 -1 => ["Sam","Alice","Bob"] LRANGE friends 0 1 => ["Sam","Alice"] LRANGE friends 1 2 => ["Alice","Bob"]
LRANGE key 0 -1表示查看所有list的value,如果是大于-1的数值,例如0 1,则表示显示下表为0到1的值.
由lrange输出可以看出,lpush ‘sam‘是后添加的数据,却排到了最前面.
LLEN friends => 3
LLEN key 表示的输出list的长度.
LPOP friends => "Sam"
LPOP表示删除第一条数据,并且回显删除的数据,与上面的LPUSH相反,这里要特别注意,别搞混了.
RPOP friends => "Bob"
RPOP则与LPOP相反是删除list最后一条数据.
set命令与list命令非常相似,但不同的是,其是无序的,值只能出现一次的,值不能重复出现,key和value是一对多的关系.
SADD superpowers "flight" SADD superpowers "x-ray vision" SADD superpowers "reflexes"
SADD命令即是将值添加到set中.
SREM superpowers "reflexes"
SREM表示删除set中的指定的value.
SISMEMBER superpowers "flight" => true SISMEMBER superpowers "reflexes" => false
sismember判断值到底是不是SET中的,是就返回true,不是,就返回false.
SMEMBERS superpowers => ["flight","x-ray vision"]
SMEMBERS返回所有的value.
SADD birdpowers "pecking" SADD birdpowers "flight" SUNION superpowers birdpowers => ["flight","x-ray vision","pecking"]
SUNION合并两个SET集体,由于set是无序的,所以这里顺序是不确定的.
ZADD hackers 1940 "Alan Kay" ZADD hackers 1906 "Grace Hopper" ZADD hackers 1953 "Richard Stallman" ZADD hackers 1965 "Yukihiro Matsumoto" ZADD hackers 1916 "Claude Shannon" ZADD hackers 1969 "Linus Torvalds" ZADD hackers 1957 "Sophie Wilson" ZADD hackers 1912 "Alan Turing"
ZADD将数据添加到有序set集合中,进行自动排序.
ZRANGE hackers 2 4 => ["Claude Shannon", "Alan Kay","Richard Stallman"]
ZRANGE表示输出指定范围的数据.
127.0.0.1:6379> hset user:1000 name ‘amosli‘ (integer) 1 127.0.0.1:6379> hset user:1000 email ‘hi_amsli@outlook.com‘ (integer) 1 127.0.0.1:6379> hset user:1000 password ‘123456‘ (integer) 1 127.0.0.1:6379> hgetall user:1000 1) "name" 2) "amosli" 3) "email" 4) "hi_amsli@outlook.com" 5) "password" 6) "123456"
HASH与JAVA中的Map集合非常类似,一个key对应一个value.
HSET命令就是set指定的key和value.
HGETALL命令即是指获取所有的key-value
127.0.0.1:6379> hmset user:1001 name ‘hiamos‘ password ‘11223344‘ email ‘hi_amosOKutlook.com‘
HMSET一次性设置多个key-value
HSET user:1000 visits 10 HINCRBY user:1000 visits 1 => 11 HINCRBY user:1000 visits 10 => 21 HDEL user:1000 visits HINCRBY user:1000 visits 1 => 1
HINCRBY可以理解为加法,将一个值加上另一个值.
HDEL则是删除.
flushall:将所有数据清空.
brpop:阻塞,只到有数据返回,有数据时删除list中的最后一条并返回.
keys:查询匹配规则的所有key
redis> MSET one 1 two 2 three 3 four 4 OK redis> KEYS *o* 1) "one" 2) "four" 3) "two" redis> KEYS t?? 1) "two" redis> KEYS * 1) "one" 2) "three" 3) "four" 4) "two"
client list:列出client的相关内容
127.0.0.1:6379> client list addr=127.0.0.1:39414 fd=6 name= age=8734 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
半小时快速了解redis,基于ubuntu 12.04 + redis 2.8.9,布布扣,bubuko.com
半小时快速了解redis,基于ubuntu 12.04 + redis 2.8.9
标签:style c class blog code java
原文地址:http://www.cnblogs.com/amosli/p/3736065.html