redis
安装redis
brew install redis
brew services start redis
pip3 install redis
redis数据类型
1. 字符串
- 设置值
set
127.0.0.1:6379> set name daxiong
OK
- 获取单个key的值
get
127.0.0.1:6379> get name
"daxiong"
- 获取指定索引
getrange
127.0.0.1:6379> getrange name 2 6
"xiong"
GETSET
重新赋值key, 返回原来的值
127.0.0.1:6379> GETSET name xiaoxiong
"daxiong"
127.0.0.1:6379> GET name
"xiaoxiong"
- 获取多个key的值
mget
127.0.0.1:6379> SET age 38
OK
127.0.0.1:6379> get age
"38"
127.0.0.1:6379> mget name age
1) "xiaoxiong"
2) "38"
- 设置key并指定过期时间
SETEX
127.0.0.1:6379> SETEX sex 3 male
OK
127.0.0.1:6379> get sex
"male"
127.0.0.1:6379> get sex
(nil)
- 设置不存在的key, 如果key存在, 无法设置成功
SETNX
127.0.0.1:6379> EXISTS money
(integer) 0
127.0.0.1:6379> SETNX money 10000
(integer) 1
127.0.0.1:6379> SETNX money 9999
(integer) 0
127.0.0.1:6379> GET money
"10000"
- 获取字符串长度
STRLEN
127.0.0.1:6379> GET money
"10000"
127.0.0.1:6379> STRLEN money
(integer) 5
- 字符串如果为数字, 做加法运算
INCRBY
127.0.0.1:6379> GET money
"10000"
127.0.0.1:6379> STRLEN money
(integer) 5
127.0.0.1:6379> INCRBY money 99
(integer) 10099
- 字符串如果为整数, 做减法运算
DECRBY
127.0.0.1:6379> GET money
"10099"
127.0.0.1:6379> DECRBY money 99
(integer) 10000
127.0.0.1:6379> GET money
"10000"
- 字符串拼接
APPEND
127.0.0.1:6379> GET name
"xiaoxiong"
127.0.0.1:6379> APPEND name lovedaxiong
(integer) 20
127.0.0.1:6379> GET name
"xiaoxionglovedaxiong"
2.hash
- 增
127.0.0.1:6379> HSET people name daxiong age 18
(integer) 2
- 单查
127.0.0.1:6379> HGET people name
"daxiong"
127.0.0.1:6379> HGET people age
"18"
- 多查
127.0.0.1:6379> HGETALL people
1) "name"
2) "daxiong"
3) "age"
4) "18"
- 查看哈希表key中指定的字段是否存在
127.0.0.1:6379> HEXISTS people name
(integer) 1
127.0.0.1:6379> HEXISTS people name2
(integer) 0
字段name存在返回1(True), 字段name2不存在返回0(False)
- 获取哈希表字段的数量
127.0.0.1:6379> HLEN people
(integer) 2
- 获取哈希表中的字段
127.0.0.1:6379> HKEYS people
1) "name"
2) "age"
- 获取哈希表中所有的值
127.0.0.1:6379> HVALS people
1) "daxiong"
2) "18"
- 字段不存在时候设置哈希表的值
使用HSET
赋值, 可以覆盖已存在的值
127.0.0.1:6379> HGET people name
"daxiong"
127.0.0.1:6379> HGET people age
"18"
127.0.0.1:6379> HSET people name xiaoxiong
(integer) 0
127.0.0.1:6379> HGET people name
"xiaoxiong"
使用HSETNX
赋值, 只能给不存在的字段赋值, 已存在的字段赋值不覆盖原有的值
127.0.0.1:6379> HSET people sex nan
(integer) 1
127.0.0.1:6379> HGET people sex
"nan"
- 删(可以多删)
127.0.0.1:6379> HDEL people name age
(integer) 2
127.0.0.1:6379> HGET people name
(nil)
127.0.0.1:6379> HGET people age
(nil)
3.列表
- 设置值
127.0.0.1:6379> RPUSH status success warning failure
(integer) 3
- 末尾插入值
127.0.0.1:6379> RPUSHX status danger
(integer) 4
- 获取值
127.0.0.1:6379> LRANGE status 0 -1
1) "success"
2) "warning"
3) "failure"
4) "danger"
- 修改值
127.0.0.1:6379> LSET status 3 dangerous
OK
- 移除列表最后一个元素
127.0.0.1:6379> RPOP status
"dangerous"
4.集合
- 添加值
127.0.0.1:6379> SADD python django mysql redis rabbitmq
(integer) 4
127.0.0.1:6379> SADD java spring mysql redis kafka
(integer) 4
- 查看值
127.0.0.1:6379> SMEMBERS python
1) "django"
2) "rabbitmq"
3) "redis"
4) "mysql"
127.0.0.1:6379> SMEMBERS java
1) "kafka"
2) "spring"
3) "redis"
4) "mysql"
- 查看集合数量
127.0.0.1:6379> SCARD python
(integer) 4
127.0.0.1:6379> SCARD java
(integer) 4
- 求集合差集
127.0.0.1:6379> SDIFF python java
1) "django"
2) "rabbitmq"
127.0.0.1:6379> SDIFF java python
1) "spring"
2) "kafka"
- 求集合交集
127.0.0.1:6379> SINTERSTORE pJava python java
(integer) 2
127.0.0.1:6379> SMEMBERS pJava
1) "redis"
2) "mysql"
- 求集合并集
127.0.0.1:6379> SUNION python java
1) "mysql"
2) "django"
3) "rabbitmq"
4) "spring"
5) "kafka"
6) "redis"
5.有序集合
- 添加
127.0.0.1:6379> ZADD score 30 a1 60 a2 10 a3 90 a4
(integer) 4
- 获取集合的数量
127.0.0.1:6379> ZCARD score
(integer) 4
- 指定索引 范围 从高到低获取集合的对象
127.0.0.1:6379> ZREVRANGE score 0 -1
1) "a4"
2) "a2"
3) "a1"
4) "a3"
127.0.0.1:6379> ZREVRANGE score 0 2
1) "a4"
2) "a2"
3) "a1"
- 指定value 范围 从高到低获取集合的对象
127.0.0.1:6379> ZREVRANGEBYSCORE score 80 20
1) "a2"
2) "a1"
- 对集合value做加法并重新赋值
127.0.0.1:6379> ZINCRBY score 100 a3
"110"
127.0.0.1:6379> ZREVRANGE score 0 2
1) "a3"
2) "a4"
3) "a2"
python使用redis
1. 安装
pip3 install redis
2. 直接使用
import redis
# 直接使用
r = redis.Redis()
r.getset('name', 'xiaoxiong')
print(r.get('name'))
r1 = redis.Redis()
print(r1.get('name'))
3. 连接池使用
import redis
pool = redis.ConnectionPool(host='127.0.0.1', port=6379, max_connections=100)
r = redis.Redis(connection_pool=pool)
r.getset('name', 'xiaoxiong')
print(r.get('name'))
r1 = redis.Redis()
print(r1.get('name'))
4. 作为缓存数据库使用
- 安装django-redis 依赖
pip3 install django-redis
- 配置文件中配置缓存指向redis
# 缓存 - redis
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {"max_connections": 100}
}
}
}
- 测试代码
from django.core.cache import cache
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'luffyapi.settings.dev')
django.setup()
cache.set('name', 'bigBear')
print(cache.get('name'))
# nx = True key值不存在才创建, 存在不创建
cache.set('test01', 000, nx=True)
print(cache.get('test01'))
cache.set('test01', 111, nx=True)
print(cache.get('test01'))