redis 使用笔记

Redis 5.0.7
make test 的时候,会报很多 Killing 的错误。解决方法:直接 sudo make 就行,执行完毕后,再 make test 就没问题。

字符串默认字节编码,decode_responses=True 将默认行为改成utf-8
命名空间, db index: 0-15
清空当前库 flushdb
清空所有库 flushall
解决中文乱码 redis-cli –raw

redis-server会关闭空闲超时的连接
redis.conf中可以设置超时时间:

Close the connection after a client is idle for N seconds (0 to disable)

timeout 300

持久化策略
https://blog.csdn.net/tiantang_1986/article/details/90695952
关闭持久化: save ""
关闭AOF: appendonly no

10种必须知道的参数
https://baijiahao.baidu.com/s?id=1636391018961856175&wfr=spider&for=pc
redis最大可用内存: maxmemory

指定日志文件: logfile "/var/log/redis/redis-server.log"

默认以守护进程运行: daemonized yes

redis服务启动及停止:

服务启动:
redis-server &
redis-server /usr/local/redis/etc/redis.conf

客户端连接方法:
redis-cli -p 6379
redis-cli -h 127.0.0.1 -p 6379 -a 12345

停止服务:
redis-cli shutdown
kill -9 PID

Redis配置文件redis.conf详细配置说明:
参考: 配置文件字段介绍(排版较清晰)
参考: 配置文件字段介绍(排版较乱)

key模糊匹配:
https://blog.csdn.net/honer123/article/details/79698828

参考文档: https://www.cnblogs.com/xiao-apple36/articles/8383718.html

conn = redis.Redis(host=’localhost’, port=6379, db=1)

conn.set("name", "abc")
print(type(conn.get(‘name’)))#byte类型(utf8格式16进制字节码)

print(conn.get(‘name’))

if isinstance(conn.get(‘name’), bytes):

字节码转换为字符串

print(conn.get('name').decode('utf8'))

conn.set(‘name’,’祖国’)

decode默认为utf8格式解码

print(conn.get(‘name’).decode())
print(conn.get(‘name’))

使用decode_responses=True 用于关闭默认以字节存储

conn = redis.Redis(host=’localhost’, port=6379, decode_responses=True, db=1)

windows上安装Redis:

https://github.com/MicrosoftArchive/redis
部署文档:
https://github.com/microsoftarchive/redis/tree/win-3.0.504
https://raw.githubusercontent.com/MSOpenTech/redis/3.0/Windows%20Service%20Documentation.md

shell 中直接指定要用哪个db

redis-cli -n 2 set "name" "value"
该命令的效果:select 2

shell中批量设置key

#!/bin/sh

for k in `redis-cli -n 2 --raw keys '*'` ;
do
echo "key="$k
redis-cli -n 2 –raw set $k "value";
done

Leave a Reply

Your email address will not be published.