redis基础数据结构操作
命令
进入docker redis容器
bash
[root@VM-16-8-centos ~]# docker exec -it redis redis-cli -a '你的密码'
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> ping
PONG
基础操作
- 字符串 string
- 列表 list
- 哈希 hash
- 集合 set
- 有序集合 zset
- 位图 bitmaps
- 超日志 hyperLogLogs
- 地理空间 geospatial
- 发布订阅 pub/sub
- 流 Streams
- 模块 modules
sql
-- 字符串
-- set myKey abc;
-- del myKey;
-- del myKey;
-- del name;
-- del s;
-- set myKey a;
-- 出现错误
-- incr myKey;
-- 哈希
-- hset hKey hello 你好;
-- HGET hKey hello;
-- HGETALL hKey;
-- hset h2 hello 你好 good 好;
-- hgetall h2;
-- 列表
-- lpush l1 c;
-- lpush l1 a;
-- rpush l1 d;
-- lpop l1;
-- rpop l1;
-- set
-- sadd s1 hello s2 good;
-- srem s1 s2 good;
-- sadd s1 happy lucky;
-- smembers s1;
-- SISMEMBER s1 lucky;
-- SISMEMBER s1 sad;
-- 测试set的唯一性 看返回值 1为true 0为false
-- sadd s1 good;
-- sadd s1 gery;
-- zset
-- zadd z1 0 happy;
-- zadd z1 1 normal;
-- zadd z1 2 sad;
-- zadd z1 0.5 angry;
-- zadd z1 1.3 blue;
-- 获取分数内的数据
-- ZRANGEBYSCORE z1 0 1.1
发布订阅
bash
# 1
127.0.0.1:6379> subscribe schat
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "schat"
3) (integer) 1
1) "message"
2) "schat"
3) "nihao"
1) "message"
2) "schat"
3) "henhao"
# 2
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> publish schat nihao
(integer) 1
127.0.0.1:6379> publish schat henhao
(integer) 1
事务
redis的事物就是批量执行的脚本。
脚本
bash
EVAL "return redis.call('set', KEYS[1], ARGV[1])" 1 s3 ridiculous;
连接
bash
[root@VM-16-8-centos ~]docker exec -it redis redis-cli
127.0.0.1:6379> Auth '你的密码'
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> select 16
(error) ERR DB index is out of range
127.0.0.1:6379> select 10
OK
服务器
- info
GEO
用于存储地理位置信息
流
用于消息队列,提供了消息的持久化和主备复制功能
高级教程
数据备份
bash
# save
# 安装目录创建 dump.rdb 文件
# CONFIG GET dir
# 获取当前目录 移动到当前目录
# bgsave
# 命令在后台执行
安全
bash
# CONFIG get requirepass
# 获取密码
# CONFIG set requirepass "你的密码"
性能测试
bash
redis-benchmark [option] [option value]