本文共 6976 字,大约阅读时间需要 23 分钟。
wget http://download.redis.io/redis-stable.tar.gztar zxvf redis-stable.tar.gzcd redis-stable.tar.gzmakemake install
redis-server
redis-cli shutdown
方法一
redis-server /path/to/redis.confredis-server /path/to/redis.conf --loglevel warning //设置日志级别
方法二
redis-cliredis > CONFIG SET loglevel warning
> select 1
(1)添加数据和查找数据,通过set/get命令添加和获取数据
127.0.0.1:6379> select 1OK127.0.0.1:6379[1]> set bar 1OK127.0.0.1:6379[1]> get bar"1"127.0.0.1:6379[1]> get aaa(nil)
(2)判断一个键是否存在,exists命令可以判断key是否存在,存在返回1,不存在返回0
127.0.0.1:6379[1]> exists bar(integer) 1127.0.0.1:6379[1]> exists aaa(integer) 0
(3)删除数据,del命令删除key,返回值为成功删除key的个数
127.0.0.1:6379[1]> del aaa(integer) 0127.0.0.1:6379[1]> del bar(integer) 1
(4)获取value的类型
127.0.0.1:6379[1]> set bar 1OK127.0.0.1:6379[1]> type barstring
(5)自增和自减,incr 、decr以及incrby和decrby实现整数的加减
127.0.0.1:6379[1]> INCR ab(integer) 1127.0.0.1:6379[1]> INCR ab(integer) 2127.0.0.1:6379[1]> INCRBY ab 2(integer) 4127.0.0.1:6379[1]> DECR ab(integer) 3127.0.0.1:6379[1]> DECRBY ab 2(integer) 1
(6)增加指定浮点数,incrbyfloat可以指定自增的浮点数
127.0.0.1:6379[1]> INCRBYFLOAT ab 1.1"2.1"
(7)向尾部追加,append命令可以向字符尾部追加内容
127.0.0.1:6379[1]> set key helloOK127.0.0.1:6379[1]> append key world(integer) 10127.0.0.1:6379[1]> get key"helloworld"
(8)获取字符串长度,strlen获取value的长度
127.0.0.1:6379[1]> STRLEN key(integer) 10
(9)批量设置和获取,通过mset和mget命令可以批量执行设置和获取
127.0.0.1:6379[1]> mset key1 va1 key2 va2OK127.0.0.1:6379[1]> mget key1 key21) "va1"2) "va2"
(10)位操作
getbit key offsetsetbit key offsetbitcount key [start] [end]bitop operation destkey key [key ...]
{ "id":1, "name":"test"}
(1)存储方法,可以通过hset和hget来设置和获取数据
127.0.0.1:6379[1]> hset user id 1(integer) 1127.0.0.1:6379[1]> hset user name test(integer) 1127.0.0.1:6379[1]> hget user id"1"127.0.0.1:6379[1]> hget user name"test"
(2)判断字段是否存在,hexists命令,返回1存在,返回0不存在
127.0.0.1:6379[1]> hexists user id(integer) 1127.0.0.1:6379[1]> hexists user age(integer) 0
(3)使用hsetnx(hset if not exists)如果不存在赋值
127.0.0.1:6379[1]> hsetnx user age 111(integer) 1127.0.0.1:6379[1]> hget user age"111"
(4)增加数字
127.0.0.1:6379[1]> hincrby user score 60(integer) 60127.0.0.1:6379[1]> hget user score"60"
(5)删除字段
127.0.0.1:6379[1]> hdel user score(integer) 1127.0.0.1:6379[1]> hget user score(nil)
键 字段 字段值post:id field value
127.0.0.1:6379[1]> incr userid(integer) 1127.0.0.1:6379[1]> hset user:1 name test(integer) 1127.0.0.1:6379[1]> hset user:1 score 90(integer) 1127.0.0.1:6379[1]> incr userid(integer) 2127.0.0.1:6379[1]> hset user:2 name test2(integer) 1127.0.0.1:6379[1]> hset user:2 score 99(integer) 1
添加了两条数据,id为1的名字为test分数为90,而id为2的名字为test2分数为90.
127.0.0.1:6379[1]> hmget user:1 name score1) "test"2) "90"
127.0.0.1:6379[1]> hgetall user:21) "name"2) "test2"3) "score"4) "99"
127.0.0.1:6379[1]> hkeys user:11) "name"2) "score"127.0.0.1:6379[1]> hvals user:11) "test"2) "90"
127.0.0.1:6379[1]> hlen user:1(integer) 2
需要注意的是,散列类型无法获取所有存在的键值,也就是id,如果删除了中间某个id的话,只可以使用exist命令来判断key是否存在。
列表的基本操作命令都是以l开头的。
127.0.0.1:6379> lpush user test(integer) 1127.0.0.1:6379> rpush user test1(integer) 2127.0.0.1:6379> lpop user "test"127.0.0.1:6379> rpop user"test1"127.0.0.1:6379>
127.0.0.1:6379> llen user(integer) 0127.0.0.1:6379> lpush user test(integer) 1127.0.0.1:6379> llen user(integer) 1
127.0.0.1:6379> lrange user 0 31) "test"2) "test1"3) "test2"4) "test3"127.0.0.1:6379> lrange user 0 21) "test"2) "test1"3) "test2"
lrem key count value@count 为删除个数,大于0的话从左边开始,小于0的话从右边开始@value 要删除的值
我们可以把数据的id存储在列表中,当某一行数据被删除时候,只需要删除为该value为key即可,而查询数据的时候,需要先从列表中读取所有的id,再从散列表中读取数据。
127.0.0.1:6379> lrange user 0 -11) "test"2) "test1"3) "test2"4) "test3"127.0.0.1:6379> linsert user before test1 test0(integer) 5127.0.0.1:6379> linsert user after test1 test1.5(integer) 6127.0.0.1:6379> lrange user 0 -11) "test"2) "test0"3) "test1"4) "test1.5"5) "test2"6) "test3"
rdroplpush source destination从source列表的右侧弹出一个数据,添加到destination列表的左侧
127.0.0.1:6379> sadd key 1(integer) 1127.0.0.1:6379> sadd key 2(integer) 1127.0.0.1:6379> sadd key 2(integer) 0127.0.0.1:6379> smembers key1) "1"2) "2"127.0.0.1:6379> srem key 2(integer) 1127.0.0.1:6379> smembers key1) "1"
127.0.0.1:6379> sismember key 1(integer) 1127.0.0.1:6379> sismember key 3(integer) 0
sdiff key [key...]sinter key [key...]sunion key [key...]
127.0.0.1:6379> scard key(integer) 1
sdiffstore destination key [key...]sinterstore destination key[key...]sunionstore destination key[key...]
srandmember key [count] 127.0.0.1:6379> srandmember key 31) "4"2) "2"3) "3"
(1)zadd添加元素,并设置分数score
127.0.0.1:6379> zadd students 60 xiaoming(integer) 1127.0.0.1:6379> zadd students 80 daxiong(integer) 1
127.0.0.1:6379> zscore students xiaoming"60"
127.0.0.1:6379> zrange students 0 11) "xiaoming"2) "daxiong"
127.0.0.1:6379> zrangebyscore students 0 701) "xiaoming"127.0.0.1:6379> zrangebyscore students 0 1001) "xiaoming"2) "daxiong"
获取从0开始的前2个数据127.0.0.1:6379> zrangebyscore students 0 100 limit 0 21) "xiaoming"2) "daxiong"获取从1开始的前2个数据127.0.0.1:6379> zrangebyscore students 0 100 limit 1 21) "daxiong"
127.0.0.1:6379> zincrby students 5 xiaoming"65"127.0.0.1:6379> zincrby students -5 xiaoming"60"
127.0.0.1:6379> zcard students(integer) 2
zcount key min max127.0.0.1:6379> zcount students 0 70(integer) 1127.0.0.1:6379> zcount students 0 90(integer) 2
zrem key [key...]
zremrangebyrank key start stop
zremrangebyscore key min max
127.0.0.1:6379> zrank students xiaoming(integer) 0