redis基础数据结构

    命令非常多,熟能生巧,常用每个都敲一遍。

Redis Key

DEL key [key …]
    DEL 用于删除已有的键。
KEYS pattern
    KEYS 查找所有符合给定模式 pattern 的 key 。
EXISTS key [key …]
    EXISTS 检查给定 key 是否存在。
127.0.0.1:6379> set redis helloworld
OK
127.0.0.1:6379> set jedis javaclient
OK
127.0.0.1:6379> keys *
1) "jedis"
2) "redis"
127.0.0.1:6379> keys *dis
1) "jedis"
2) "redis"
127.0.0.1:6379> keys re*
1) "redis"
127.0.0.1:6379> exists redis jedis
(integer) 2
127.0.0.1:6379> exists not_this_key
(integer) 0
127.0.0.1:6379> del redis jedis
(integer) 2
127.0.0.1:6379> exists redis
(integer) 0
EXPIRE key seconds /Expireat /PEXPIRE /PEXPIREAT
    EXPIRE(秒)/PEXPIRE (毫秒)  用于设置 key 的过期时间。key 过期后将不再可用。
    Expireat/ PEXPIREAT 以 UNIX 时间戳(unix timestamp)格式设置 key 的过期时间。
127.0.0.1:6379> expire redis 10  //设置10秒
(integer) 1
127.0.0.1:6379> exists redis
(integer) 1
127.0.0.1:6379> exists redis
(integer) 0
127.0.0.1:6379> pexpire redis 5000  //设置5000毫秒
(integer) 1
127.0.0.1:6379> exists redis
(integer) 1
127.0.0.1:6379> exists redis
(integer) 0
PERSIST key
    PERSIST 用于移除给定 key 的过期时间,使得 key 永不过期。
127.0.0.1:6379> set redis helloworld
OK
127.0.0.1:6379> expire redis 10
(integer) 1
127.0.0.1:6379> persist redis
(integer) 1
127.0.0.1:6379> exists redis  //等待20秒
(integer) 1
TTL key /Pttl
    TTL (秒)/Pttl (毫秒)返回 key 的剩余过期时间。
127.0.0.1:6379> ttl redis  //永久时,返回-1
(integer) -1
127.0.0.1:6379> pttl redis
(integer) -1
127.0.0.1:6379> expire redis 10
(integer) 1
127.0.0.1:6379> ttl redis   //有时间时,返回相应的生存时间
(integer) 7
127.0.0.1:6379> ttl redis   //不存在此key时,返回-2
(integer) -2
RANDOMKEY
    RANDOMKEY 从当前数据库中随机返回一个 key 
127.0.0.1:6379> mset key1 value1 key2 value2 key3 value3
OK
127.0.0.1:6379> randomkey 
"key1"
127.0.0.1:6379> randomkey 
"key1"
127.0.0.1:6379> randomkey 
"key1"
127.0.0.1:6379> randomkey 
"key3"
127.0.0.1:6379> keys *
1) "key1"
2) "key3"
3) "key2"

Redis String

SET KEY_NAME VALUE /GET KEY/Getset
    SET :用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型.
    GET 获取指定 key 的值。如果 key 不存在,返回 nil 。如果key 储存的值不是字符串类型,返回一个错误。
    Getset 设置指定 key 的值,并返回 key 旧的值。
127.0.0.1:6379> set redis hellowold
OK
127.0.0.1:6379> set redis example
OK
127.0.0.1:6379> get redis
"example"
127.0.0.1:6379> getset redis newvalue
"example"
SETBIT key offset value / GETBIT key offset
   SETBIT  对 key 所储存的字符串值,设置或清除指定偏移量上的位(bit)
   GETBIT  用于对 key 所储存的字符串值,获取指定偏移量上的位(bit)
127.0.0.1:6379> setbit bitkey 100 1
(integer) 0
127.0.0.1:6379> getbit bitkey 100
(integer) 1
127.0.0.1:6379> getbit bitkey 100000  //未设置的默认为0
(integer) 0
INCR key / INCRBY key increment /INCRBYFLOAT
    INCR (Decr ) 将 key 中储存的数字值增一。如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作。如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。
    INCRBY(Decrby ) 将 key 中储存的数字值增加指定数值
    INCRBYFLOAT 为 key 中所储存的值加上指定的浮点数增量值。
127.0.0.1:6379> set num 100
OK
127.0.0.1:6379> incr num
(integer) 101
127.0.0.1:6379> incrby num 20
(integer) 121
127.0.0.1:6379> incrbyfloat num 0.3
"121.3"
APPEND key value
     APPEND  命令用于为指定的 key 追加值。如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾.如果 key 不存在, APPEND 就简单地将给定 key 设为 value ,就像执行 SET key value 一样
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> append newkey hello
(integer) 5
127.0.0.1:6379> get newkey
"hello"
127.0.0.1:6379> append newkey world
(integer) 10
127.0.0.1:6379> get newkey
"helloworld"

Redis List

LPUSH key value [value …] /LPOP key/RPUSH /RPOP
    LPUSH(左)/RPUSH(右)  将一个或多个值 value 插入到列表 key 的表头
    LPOP(左) /RPOP (右)移除并返回列表 key 的头元素。
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> lpush lkey value1
(integer) 1
127.0.0.1:6379> lpop lkey 
"value1"
127.0.0.1:6379> rpush lkey 1 2 3 4
(integer) 4
127.0.0.1:6379> rpop lkey 
"4"
127.0.0.1:6379> lpop lkey
"1"
LRANGE key start stop
    LRANGE  返回列表 key 中指定区间内的元素,区间以偏移量 start 和 stop 指定。
127.0.0.1:6379> lrange lkey 0 -1
1) "2"
2) "3"
127.0.0.1:6379> lrange lkey 0 0
1) "2"
RPOPLPUSH source destination
    RPOPLPUSH 在一个原子时间内,执行以下两个动作:
将列表 source 中的最后一个元素(尾元素)弹出,并返回给客户端。
将 source 弹出的元素插入到列表 destination ,作为 destination 列表的的头元素。
127.0.0.1:6379> rpush rkey 1 2 3 4
(integer) 4
127.0.0.1:6379> lrange rkey 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
127.0.0.1:6379> rpoplpush rkey lkey
"4"
127.0.0.1:6379> lrange lkey 0 -1
1) "4"
2) "2"
3) "3"
127.0.0.1:6379> lrange rkey 0 -1
1) "1"
2) "2"
3) "3"
BRPOPLPUSH source destination timeout
    BRPOPLPUSH 是 RPOPLPUSH 的阻塞版本,当给定列表 source 不为空时, BRPOPLPUSH 的表现和 RPOPLPUSH 一样。
当列表 source 为空时, BRPOPLPUSH 命令将阻塞连接,直到等待超时,或有另一个客户端对 source 执行 LPUSH 或 RPUSH 命令为止。
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> lpush lkey 1 2 3 
(integer) 3
127.0.0.1:6379> rpoplpush lkey rkey
"1"
127.0.0.1:6379> rpoplpush lkey rkey
"2"
127.0.0.1:6379> rpoplpush lkey rkey
"3"
127.0.0.1:6379> rpoplpush lkey rkey
(nil)
127.0.0.1:6379> lpush lkey 4
(integer) 1
127.0.0.1:6379> rpoplpush lkey rkey
"4"
127.0.0.1:6379> lrange rkey 0 -1
1) "4"
2) "3"
3) "2"
4) "1"

Redis Set

SADD key member [member …]/SMEMBERS key
    SADD  将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略。
    SMEMBERS 返回集合 key 中的所有成员。不存在的 key 被视为空集合。
127.0.0.1:6379> sadd skey  1 2 3 4
(integer) 4
127.0.0.1:6379> smembers skey 
1) "1"
2) "2"
3) "3"
4) "4"
SCARD key /SISMEMBER key member
    SCARD 返回集合 key 的基数(集合中元素的数量)。
    SISMEMBER  判断 member 元素是否集合 key 的成员。
127.0.0.1:6379> scard skey
(integer) 4
127.0.0.1:6379> sismember skey 1
(integer) 1
127.0.0.1:6379> sismember skey 3
(integer) 1
127.0.0.1:6379> sismember skey 5
(integer) 0
SDIFF FIRST_KEY OTHER_KEY1..OTHER_KEYN /SINTER
    SDIFF(Sdiffstore ) 命令返回给定集合之间的差集。不存在的集合 key 将视为空集。
    SINTER(Sinterstore ) 命令返回给定所有给定集合的交集。 不存在的集合 key 被视为空集。 当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)。
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> sadd key1 1 2 3 4
(integer) 4
127.0.0.1:6379> sadd key2 3 4 5 6
(integer) 4
127.0.0.1:6379> sdiff key1 key2 
1) "1"
2) "2"
127.0.0.1:6379> sdiff key2 key1
1) "5"
2) "6"
127.0.0.1:6379> sinter key1 key2
1) "3"
2) "4"
127.0.0.1:6379> sinter key2 key1
1) "3"
2) "4"
127.0.0.1:6379> Sdiffstore save  key1 key2  //新建集合装填
(integer) 2
127.0.0.1:6379> smembers save
1) "1"
2) "2"

Redis Hash

HSET KEY_NAME FIELD VALUE /HGET KEY_NAME FIELD_NAME / HMSET /HMGET /HGETALL
    HSET(HMSET(多个)) 于为哈希表中的字段赋值 ,如果哈希表不存在,一个新的哈希表被创建并进行 HSET 操作。如果字段已经存在于哈希表中,旧值将被覆盖。
    HGET(HMGET /HGETALL ) 返回给定字段的值。如果给定的字段或 key 不存在时,返回 nil 。
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> hset hkey hash 1
(integer) 1
127.0.0.1:6379> hmset hkey tree 2 linked 3
OK
127.0.0.1:6379> hget hkey hash
"1"
127.0.0.1:6379> hmget hkey tree linked
1) "2"
2) "3"
127.0.0.1:6379> hget hkey null
(nil)
HEXISTS KEY_NAME FIELD_NAME /HDEL
    HEXISTS  命令用于查看哈希表的指定字段是否存在
    HDEL 用于删除哈希表 key 中的一个或多个指定字段,不存在的字段将被忽略
127.0.0.1:6379> hexists hkey hash
(integer) 1
127.0.0.1:6379> hexists hkey null
(integer) 0
127.0.0.1:6379> hdel hkey hash
(integer) 1
127.0.0.1:6379> hdel hkey hash null 
(integer) 0
127.0.0.1:6379> hmget hkey tree linked hash
1) "2"
2) "3"
3) (nil)
127.0.0.1:6379> hdel hkey tree linked
(integer) 2
127.0.0.1:6379> hmget hkey tree linked hash
1) (nil)
2) (nil)
3) (nil)
HKEYS KEY /HVALS
    HKEYS  命令用于获取哈希表中的所有字段名。
    HVALS 返回哈希表所有字段的值。
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> hmset hkey hash 1 tree 2 linked 3
OK
127.0.0.1:6379> hkeys hkey
1) "hash"
2) "tree"
3) "linked"
127.0.0.1:6379> hvals hkey
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> hkeys null 
(empty list or set)
127.0.0.1:6379> hvals null 
(empty list or set)

Redis Sorted

ZADD KEY_NAME SCORE1 VALUE1.. SCOREN VALUEN/ZSCORE key member
    ZADD  用于将一个或多个成员元素及其分数值加入到有序集当中。如果某个成员已经是有序集的成员,那么更新这个成员的分数值,并通过重新插入这个成员元素,来保证该成员在正确的位置上。分数值可以是整数值或双精度浮点数。如果有序集合 key 不存在,则创建一个空的有序集并执行 ZADD 操作。当 key 存在但不是有序集类型时,返回一个错误。
    ZSCORE 返回有序集中,成员的分数值。 如果成员元素不是有序集 key 的成员,或 key 不存在,返回 nil 。
127.0.0.1:6379> zadd score 95 zs 87 ls 100 ww 78 zl
(integer) 4
127.0.0.1:6379> zscore score zs
"95"
ZRANGE key start stop [WITHSCORES]/Zrevrange
    ZRANGE 返回有序集中,指定区间内的成员。其中成员的位置按分数值递增(从小到大)来排序。具有相同分数值的成员按字典序(lexicographical order )来排列。
127.0.0.1:6379> zrange score 0 -1 withscores 
1) "zl"
2) "78"
3) "ls"
4) "87"
5) "zs"
6) "95"
7) "ww"
8) "100"
127.0.0.1:6379> zrange score 0 2
1) "zl"
2) "ls"
3) "zs"
127.0.0.1:6379> Zrevrange  score 0 -1 
1) "ww"
2) "zs"
3) "ls"
4) "zl"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值