近期回顾了Redis方面的技术,本文就redis的5大基础数据类型的指令做了一个总结并附上示例
Redis是一种开源的内存数据库,它被用作缓存、消息代理和键值存储。它支持多种数据结构,如字符串、哈希、列表、集合、有序集合等,这使得它非常灵活且适用于各种用途。Redis的速度很快,因为它将数据存储在内存中,并且可以持久化到磁盘,确保数据在重启后不会丢失。它也支持多种语言的客户端库,这使得它在各种应用和开发环境中都很流行。
SET mykey "Hello"
GET mykey
MSET key1 "value1" key2 "value2"
DEL key1
INCR counter
APPEND mykey " World"
EXPIRE mykey 60
TTL mykey
GETRANGE mykey 0 3
STRLEN mykey
MGET key1 key2
SETNX mykey "newvalue"
SETEX mykey 60 "value"
GETSET mykey "newvalue"
INCRBY counter 5
DECR counter
DECRBY counter 3
SETBIT mybitmap 7 1
GETBIT mybitmap 7
BITCOUNT mybitmap
HSET user:id:001 username Alice
HGET user:id:001 username
HMSET user:id:001 username Alice age 30
HDEL user:id:001 age
HGETALL user:id:001
HEXISTS user:id:001 username
HKEYS user:id:001
HVALS user:id:001
HINCRBY user:id:001 age 5
HLEN user:id:001
HMGET user:id:001 username age
HSETNX user:id:001 email "alice@example.com"
HSTRLEN user:id:001 username
HSCAN user:id:001 0 MATCH "user*"
HINCRBYFLOAT user:id:001 balance 10.5
HRANDFIELD user:id:001 2
HSTRLEN user:id:001 username
HSCAN user:id:001 0 MATCH "user*"
HINCRBYFLOAT user:id:001 balance 10.5
HRANDFIELD user:id:001 2
LPUSH mylist "world"
RPUSH mylist "world"
LPOP mylist
RPOP mylist
LRANGE mylist 0 -1
LLEN mylist
LINDEX mylist 0
LINSERT mylist BEFORE|AFTER pivot value
LREM mylist 0 "value"
RPOPLPUSH source destination
LTRIM mylist 0 2
BLPOP key [key ...] timeout
BRPOP key [key ...] timeout
RPOPLPUSH source destination
BRPOPLPUSH source destination timeout
LRANGE mylist 0 -1
LPUSHX mylist "value1" "value2"
RPUSHX mylist "value1" "value2"
LTRIM mylist 0 2
RPOPLPUSH source destination
SADD myset "apple"
SREM myset "apple"
SMEMBERS myset
SISMEMBER myset "apple"
SCARD myset
SUNION key1 key2
SINTER key1 key2
SDIFF key1 key2
SPOP myset
SMOVE source destination "apple"
SRANDMEMBER myset 3
SRANDMEMBER myset 2
SPOP myset
SSCAN myset cursor [MATCH pattern] [COUNT count]
SUNIONSTORE destination key1 key2
SINTERSTORE destination key1 key2
SDIFFSTORE destination key1 key2
SSCAN myset cursor MATCH "pattern" COUNT count
SCARD myset
SSCAN myset cursor COUNT count
ZADD myzset 1 "one"
ZRANGE myzset 0 -1 WITHSCORES
ZRANGEBYSCORE myzset 0 5
ZRANK myzset "one"
ZSCORE myzset "one"
ZREM myzset "one"
ZCARD myzset
ZCOUNT myzset 0 5
ZINCRBY myzset 5 "one"
ZRANGE myzset 0 -1 WITHSCORES
ZRANGEBYSCORE myzset 0 5 WITHSCORES
ZREVRANGE myzset 0 -1 WITHSCORES
ZREVRANGEBYSCORE myzset 5 0 WITHSCORES
ZREMRANGEBYRANK myzset 0 1
ZREMRANGEBYSCORE myzset 0 5
这些示例展示了如何使用Redis中的不同命令执行各种操作,包括设置和获取值、操作列表、集合和有序集合,以及如何管理哈希中的字段。