1 普通连接
from redis import Redis
conn=Redis()
2 连接池
import redis
POOL = redis.ConnectionPool(host='127.0.0.1', port=6379,max_connections=100)
import redis
from t_redis_pool import POOL
r = redis.Redis(connection_pool=POOL)
ret=r.get('name')
print(ret)
3 String类型
conn.set('height','190',ex=True)
conn.set('height','190',nx=True)
conn.set('height1','190',xx=True)
conn.mset({'name1':'11','name3':'dasfd'})
conn.mget(['name1','name','name3'])
conn.getset('name1', '999')
conn.getrange('name1',0,0)
conn.setrange('name1',1,88888)
conn.getbit('name1',9)
conn.incr('name1',1)
conn.decr('name1',1)
conn.append('name1','oo')
4 hash类型
conn.hset('hash1','name','aaa')
ret=conn.hget('hash1','name')
conn.hmset('hash2',{'key1':'value1','key2':'value2'})
conn.hmget('hash1','name','name2')
conn.hmget('hash1',['name','name2'])
conn.hgetall('hash1')
ret=conn.hlen('hash1')
ret=conn.hkeys('hash1')
ret=conn.hexists('hash1','name1')
conn.hdel('hash1','name')
ret=conn.hincrby('hash1','name',1)
ret=conn.hscan_iter('hash1')
5 list类型
conn.lpush('list1',1,2,3,4,5)
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')
ret=conn.lrem('list1',-1,'5')
ret=conn.lrem('list1',0,'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)
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 = 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}
}
}
}
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'))
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!