2.redis命令

redis基础数据结构

string(字符串) list(列表) set(集合) zset(有序集合) hash(哈希)

image.png

1.字符串

字符串是最简单也是最常用的数据结构。比如可以存储用户信息,将用户信息使用json格式序列化成字符串进行存储。

单个键值对操作:

127.0.0.1:6379> set name dck
OK
127.0.0.1:6379> get name
"dck"
127.0.0.1:6379> exists name
(integer) 1
127.0.0.1:6379> del name
(integer) 1
127.0.0.1:6379> get name
(nil)

批量键值对操作:对多个键值对操作,节省网络开销

127.0.0.1:6379> mset name1 zhangsan name2 lisi name3 wangwu
OK
127.0.0.1:6379> mget name1 name2 name3 name4
1) "zhangsan"
2) "lisi"
3) "wangwu"
4) (nil)

过期时间设置和set命令扩展:对键设置过期时间,到期自动删除,常用来设置缓存的失效时间

127.0.0.1:6379> set name dck
OK
127.0.0.1:6379> get name
"dck"
127.0.0.1:6379> expire name 5 #  expire 5秒之后过期
(integer) 1
127.0.0.1:6379> get name
"dck"
127.0.0.1:6379> get name  #5秒后
(nil)
127.0.0.1:6379> setex name 5 dck # 5秒后过期。等价于 set+expire
OK
127.0.0.1:6379> get name
"dck"
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> setnx name dck  # name 键不存在,则执行set创建。存在则不执行,不改变name值
(integer) 1
127.0.0.1:6379> setnx name dck111
(integer) 0
127.0.0.1:6379> get name
"dck"

原子计数:如果 value 值是一个整数,还可以对它进行自增操作。自增是有范围的,它的范围是 signed long 的最大最小值,超过了这个值,Redis 会报错

127.0.0.1:6379> set age 1
OK
127.0.0.1:6379> incr age # age加1
(integer) 2
127.0.0.1:6379> incrby age 5 #age加5
(integer) 7
127.0.0.1:6379> decr age # age减1
(integer) 6
127.0.0.1:6379> decrby age 3 # age减3
(integer) 3
127.0.0.1:6379> set age 9223372036854775807  #set age long类型最大值
OK
127.0.0.1:6379> incr age
(error) ERR increment or decrement would overflow

2.list(列表)

redis的list结构相当于java里的LinkedList,因此插入删除很快,时间复杂度为O(1),但是查询较慢,时间复杂度为O(n)。当最后一个元素被弹出时,该数据结构被删除。
常被用来做异步队列使用,将待处理的任务结构体序列化成字符串,放到列表中,另一个线程轮训数据进行处理。
队列结构:先进先出

127.0.0.1:6379> rpush books java c c++ python # 右侧插入数据
(integer) 4
127.0.0.1:6379> llen books # 数据个数
(integer) 4
127.0.0.1:6379> lpop books # 左侧弹出数据
"java"
127.0.0.1:6379> lpop books
"c"
127.0.0.1:6379> lpop books
"c++"
127.0.0.1:6379> lpop books
"python"
127.0.0.1:6379> lpop books
(nil)
127.0.0.1:6379> EXISTS books
(integer) 0

栈:先进后出

127.0.0.1:6379> rpush books java c++ python
(integer) 3
127.0.0.1:6379> rpop books
"python"
127.0.0.1:6379> rpop books
"c++"
127.0.0.1:6379> rpop books
"java"
127.0.0.1:6379> rpop books
(nil)

3.hash(字典)

hash结构 相当于java里的HashMap也是采用数组+链表形式存储。

127.0.0.1:6379> hset test name dck nickname test age 28
(integer) 3
127.0.0.1:6379> hlen test
(integer) 3
127.0.0.1:6379> hgetall test
1) "name"
2) "dck"
3) "nickname"
4) "test"
5) "age"
6) "28"
127.0.0.1:6379> hget test name
"dck"
127.0.0.1:6379> hmset books java "learn java" c++ "learn c++" python "learn python"
OK
127.0.0.1:6379> hgetall books
1) "java"
2) "learn java"
3) "c++"
4) "learn c++"
5) "python"
6) "learn python"

4.set(集合)

相当于java中的HashSet

127.0.0.1:6379> sadd books java python c++ # 添加元素
(integer) 3
127.0.0.1:6379> smembers books   # 集合内全部元素注意是无序的
1) "python"
2) "c++"
3) "java"
127.0.0.1:6379> scard books # 集合内元素的个数
(integer) 3
127.0.0.1:6379> sismember books java #是否存在某个元素,存在返回1
(integer) 1
127.0.0.1:6379> sismember books c
(integer) 0
127.0.0.1:6379> spop books  # 弹出一个元素
"python" 
127.0.0.1:6379> srem books c++ # 移除某个元素
(integer) 1

5.zset (有序集合)

相当于java中的 SortedSet跟HashMap结合体,用set保证元素唯一,另外给每个元素赋予了一个score,用做排序权重。

127.0.0.1:6379> zadd books 9 "thinking in java"
(integer) 1
127.0.0.1:6379> zadd books 8.8 "unix program"
(integer) 1
127.0.0.1:6379> zadd books 8.9 "thinking in redis"
(integer) 1
127.0.0.1:6379> zrange books 0 -1 withscores # 按照分数降序,参数区间为排名范围
1) "unix program"
2) "8.8000000000000007"
3) "thinking in redis"
4) "8.9000000000000004"
5) "thinking in java"
6) "9"
127.0.0.1:6379> zrevrange books 0 -1 withscores # 按照分数升序 参数区间为排名
1) "thinking in java"
2) "9"
3) "thinking in redis"
4) "8.9000000000000004"
5) "unix program"
6) "8.8000000000000007"
127.0.0.1:6379> zcard books # key的元素个数  count()
(integer) 3
127.0.0.1:6379> zscore books "thinking in redis"  #某个元素的分数
"8.9000000000000004"
127.0.0.1:6379> zrank books "thinking in java" # 某个元素的排名 排名从0开始,分数降序排名
(integer) 2
127.0.0.1:6379> zrangebyscore books 8 9 # 根据分数值遍历zset  分数前后包括
1) "unix program"
2) "thinking in redis"
3) "thinking in java"
127.0.0.1:6379> zrangebyscore books -inf 8.91 withscores #[-∞,8.91]遍历
1) "unix program"
2) "8.8000000000000007"
3) "thinking in redis"
4) "8.9000000000000004"
127.0.0.1:6379> zrem books "thinking in redis" # 删除books里的元素
(integer) 1
127.0.0.1:6379> zrange books 0 -1
1) "unix program"
2) "thinking in java"

6.其他命令

keys:遍历匹配键,线上环境慎用此命令,当redis键比较多时,性能差,单线程的原因,阻塞其他命令的执行

127.0.0.1:6379> keys *
1) "name"
2) "name3"
3) "age"
4) "name1"
5) "name2"
6) "books"
7) "spring:session:index:org.springframework.session.FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME:admin"
8) "test"
9) "name6"
127.0.0.1:6379> keys name*
1) "name"
2) "name3"
3) "name1"
4) "name2"
5) "name6"

scan: 获取指定数量匹配的键
scan 游标(从零开始到零结束) match 匹配表达式 count 数值 (返回键数量)

游标值从0开始 以返回的游标值作为第二次查找,直到返回0即为查找结束。
count指定返回键的最大数值,可能不一定等于该数值。

127.0.0.1:6379> scan 0 match name* count 2
1) "12"
2) 1) "name"
   2) "name2"
127.0.0.1:6379> scan 12 match name* count 2
1) "9"
2) 1) "name1"
   2) "name3"
127.0.0.1:6379> scan 9 match name* count 2
1) "7"
2) (empty list or set)
127.0.0.1:6379> scan 7 match name* count 2
1) "0"
2) 1) "name6"

Info:查看redis服务运行信息,分为 9 大块,每个块都有非常多的参数,这 9 个块分别是:
Server 服务器运行的环境参数
Clients 客户端相关信息
Memory 服务器运行内存统计数据
Persistence 持久化信息
Stats 通用统计数据
Replication 主从复制相关信息
CPU CPU 使用情况
Cluster 集群信息

127.0.0.1:6379> info
# Server
redis_version:5.0.3
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:746c24e474ef3155
redis_mode:standalone
os:Darwin 17.6.0 x86_64
arch_bits:64
multiplexing_api:kqueue
atomicvar_api:atomic-builtin
gcc_version:4.2.1
process_id:35032
run_id:b8b4f2859bd1e423256ad6929ea6ea2eae9c2ee1
tcp_port:6379
uptime_in_seconds:1443294
uptime_in_days:16
hz:10
configured_hz:10
lru_clock:5326063
executable:/program/redis/redis-5.0.3/redis-server
config_file:/program/redis/redis-5.0.3/redis.conf

# Clients
connected_clients:1
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0

# Memory
used_memory:1041184
used_memory_human:1016.78K
used_memory_rss:1585152
used_memory_rss_human:1.51M
used_memory_peak:1337568
used_memory_peak_human:1.28M
used_memory_peak_perc:77.84%
used_memory_overhead:1038246
used_memory_startup:988032
used_memory_dataset:2938
used_memory_dataset_perc:5.53%
allocator_allocated:1006384
allocator_active:1547264
allocator_resident:1547264
total_system_memory:17179869184
total_system_memory_human:16.00G
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:1.54
allocator_frag_bytes:540880
allocator_rss_ratio:1.00
allocator_rss_bytes:0
rss_overhead_ratio:1.02
rss_overhead_bytes:37888
mem_fragmentation_ratio:1.58
mem_fragmentation_bytes:578768
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:49694
mem_aof_buffer:0
mem_allocator:libc
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1548827641
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0

# Stats
total_connections_received:273
total_commands_processed:13469
instantaneous_ops_per_sec:0
total_net_input_bytes:1363338
total_net_output_bytes:3631120
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:20
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
keyspace_hits:1683
keyspace_misses:491
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:404
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0

# Replication
role:master
connected_slaves:0
master_replid:2aad384ea130f11f4fe1c88b298c60d7e7807383
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:512.685157
used_cpu_user:211.872089
used_cpu_sys_children:0.059274
used_cpu_user_children:0.020748

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=9,expires=0,avg_ttl=0

你可能感兴趣的:(2.redis命令)