1 普通连接
from redis import Redis
conn=Redis() # 连接对象
2 连接池
import redis
#pool必须是单例的
POOL = redis.ConnectionPool(host='127.0.0.1', port=6379,max_connections=100) # 池子的最大连接为100
import redis
from t_redis_pool import POOL # 导入生成的池子,为实现单例将池子放在另一个py文件中
r = redis.Redis(connection_pool=POOL) # 只要执行这一句话,就是从池中拿出一个连接
ret=r.get('name')
print(ret)
3 String类型
conn.set('height','190',ex=True) # px,过期时间(毫秒) ex,过期时间(秒)
conn.set('height','190',nx=True) # nx如果设置为True,则只有name不存在时,当前set操作才执行,值存在,就修改不了,执行没效果
conn.set('height1','190',xx=True) # xx如果设置为True,则只有name存在时,当前set操作才执行,值存在才能修改,值不存在,不会设置新值
conn.mset({'name1':'11','name3':'dasfd'}) # 批量插入
conn.mget(['name1','name','name3']) # 批量获取
conn.getset('name1', '999') # 获取name1的值并修改为999
conn.getrange('name1',0,0) # 前闭后闭区间,获取name1对应value的第0位
conn.setrange('name1',1,88888) # 从第二为后设置成88888
conn.getbit('name1',9) # 获取name1对应值的第10为二进制数
conn.incr('name1',1) # 只要一执行,数字加1,可以为负数
conn.decr('name1',1) # 只要一执行,数字减1,可以为负数
conn.append('name1','oo') # 在name1的值最后添加oo
4 hash类型
conn.hset('hash1','name','aaa') # 添加hansh类型,hash1对应的可以理解为python中的字典
ret=conn.hget('hash1','name') # 只能取一个
conn.hmset('hash2',{'key1':'value1','key2':'value2'}) # 批量添加
conn.hmget('hash1','name','name2') # 批量获取,name和name2的值
conn.hmget('hash1',['name','name2'])
conn.hgetall('hash1') # 获取hash1的所有内容,尽量少用
ret=conn.hlen('hash1') # 返回hash1的长度
ret=conn.hkeys('hash1') # 返回hash1的键值
ret=conn.hexists('hash1','name1') # 返回布尔值,hash1中是否存在name1
conn.hdel('hash1','name') # 删除hash1中name键值
ret=conn.hincrby('hash1','name',1) # name字段加1
ret=conn.hscan_iter('hash1') # 返回一个生成器,相当于hash1所有内容但是以生成器的形式,不占用内存
5 list类型
conn.lpush('list1',1,2,3,4,5) # 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作
conn.rpush('list1',999)
conn.lpushx('list2',1)
conn.lpushx('list1',888) # 将一个或多个值插入到已存在的列表头部,列表不存在时操作无效
conn.rpushx('list1',666)
ret=conn.llen('list1')
conn.linsert('list1','before','3','77777777')
conn.linsert('list1','after','3','66666666')
conn.lset('list1',3,'22222')
conn.lset('list1',0,'11111')
ret=conn.lrem('list1',2,'5') # 从前往后删除两个5
ret=conn.lrem('list1',-1,'5') # 从后往前删除1个5
ret=conn.lrem('list1',0,'5') # 删除所有5
ret=conn.lpop('list1')
ret=conn.rpop('list1')
ret=conn.lindex('list1',0)
ret=conn.lrange('list1',0,2) # 前闭后闭
ret=conn.ltrim('list1',1,2)
ret=conn.blpop('list1',timeout=10) # 重点block,阻塞,可以写一个超时时间,没有超时时间会阻塞等待数据
5.1 由于列表没有像hash类型的hscan_iter方法,所以我们需要自己指定获取列表内所有内容的方法
def scan_list(name,count=2):
index=0
while True:
data_list=conn.lrange(name,index,count+index-1)
if not data_list:
return
index+=count
for item in data_list:
yield item
6 通用方法
conn.delete('list1')
conn.delete('hash1')
ret=conn.exists('hash2')
ret=conn.keys('cache*') #查询以cache开头的所有key
ret=conn.expire('hash2',2)
ret=conn.type('name3')
ret=conn.type('test')
ret=conn.type('test')
7 管道(redis通过管道实现事务)
import redis
pool = redis.ConnectionPool(host='127.0.0.1', port=6379)
conn = redis.Redis(connection_pool=pool)
# pipe = r.pipeline(transaction=False)默认的情况下,管道里执行的命令可以保证执行的原子性,执行pipe = r.pipeline(transaction=False)可以禁用这一特性。
pipe = conn.pipeline(transaction=True)
pipe.multi() # 事务开始
pipe.set('name', 'alex')
pipe.set('role', 'sb')
pipe.execute() # 这句话,才真正的去执行
8 django中使用redis
-pip install django-redis
-setting中配置
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}
# "PASSWORD": "123",
}
}
}
1 使用cache
from django.core.cache import cache
cache.set('name',user)
2 直接使用conn对象
from django_redis import get_redis_connection
conn = get_redis_connection('default')
print(conn.hgetall('xxx'))