1.python连接redis
import redis # # conn=redis.Redis(host='192.168.25.132',port=6379) # # conn.set('x1','huge',ex=5) # # print(conn.get('x1')) pool=redis.ConnectionPool(host='192.168.25.132',port=6379,max_connections=1000) r=redis.Redis(connection_pool=pool) # r.set('foo','Bar') print(r.get('foo'))
2.redis字典操作
import redis pool = redis.ConnectionPool(host='192.168.25.132', port=6379, max_connections=1000) conn = redis.Redis(connection_pool=pool) """ 字典操作 redis={ person:{ username:weihu, age:18 } } """ conn.hset('person', 'username', 'weihu') conn.hset('person', 'age', 18) val = conn.hget('person', 'username') print(val) # b'weihu' print(conn.hgetall('person')) # {b'username': b'weihu', b'age': b'18'} # 计算器 print(conn.hget('person', 'age')) conn.hincrby('person', 'age', amount=2) # age增加2 print(conn.hget('person', 'age')) # 问题:如果redis的person对应的字典中有1000w条数据,请打印所有的数据。 # 不可取:从redis取到数据之后,服务器内存无法承受,爆栈 # result = conn.hgetall('person') # print(result) ret = conn.hscan_iter('person', count=100) for item in ret: print(item)
3.redis列表操作
import redis """ redis={ grade:[60,70,80,90] } """ pool = redis.ConnectionPool(host='192.168.25.132', port=6379, max_connections=1000) conn = redis.Redis(connection_pool=pool) # 左插入 # conn.lpush('k1',11) # 右插入 # conn.rpush('k1',33) # 左获取 # val = conn.lpop('k1') # val = conn.blpop('k1',timeout=10) # hang住 # 右获取 # val = conn.rpop('k1') # val = conn.brpop('k1',timeout=10) # hang住 # conn.lpush('k1',*[11,22,33,44,55,66,77,88,99,66,666,2234,345234,234]) def list_iter(key, count=100): index = 0 while True: data_list = conn.lrange('k1', index, index + count - 1) if not data_list: return index += count for item in data_list: yield item print(conn.lrange('k1', 0, 101)) for item in list_iter('k1', count=3): print(item)
4.redis事物
import redis """ redis={ grade:[60,70,80,90] } """ pool = redis.ConnectionPool(host='192.168.25.132', port=6379, max_connections=1000) conn = redis.Redis(connection_pool=pool) pipe = conn.pipeline(transaction=True) pipe.multi() pipe.set('k2','123') pipe.hset('k3','n1',666) pipe.lpush('k4','laonanhai') pipe.execute()
5.redis增删改查
import redis import json conn = redis.Redis(host='140.143.227.206', port=6379, password='1234') """ -----> 第一版 { luffy_shopping_car:{ 6:{ 11:{ 'title':'21天入门到放弃', 'src':'xxx.png' }, 12:{ 'title':'21天入门到放弃', 'src':'xxx.png' } } } } -----> 第二版 { luffy_shopping_car_6_11:{ 'title':'21天入门到放弃', 'src':'xxx.png' }, luffy_shopping_car_6_12:{ 'title':'21天入门到放弃', 'src':'xxx.png' }, luffy_shopping_car_6_14:{ 'title':'21天入门到放弃', 'src':'xxx.png' } } """ # conn.flushall() #删除所有数据 # 添加课程 # redis_key = "luffy_shopping_car_%s_%s" %(7,12,) # conn.hmset(redis_key,{'title':'21天入门到放弃','src':'xxx.png'}) # 删除课程 # conn.delete('luffy_shopping_car_6_12') # print(conn.keys()) # 修改课程 # conn.hset('luffy_shopping_car_6_11','src','x1.png') # print(conn.hget('luffy_shopping_car_6_11','src')) # 查看所有课程 # print(conn.keys("luffy_shopping_car_6_*")) # for item in conn.scan_iter('luffy_shopping_car_6_*',count=10): # course = conn.hgetall(item) # print(course) # conn.set('k1',123) # print(conn.type('luffy_shopping_car_6_11')) # print(conn.type('k1')) from django.core.cache import cache # print(conn.keys()) # # for key in conn.scan_iter("luffy_shopping_car_1*"): # # title = conn.hget(key,'title') # img = conn.hget(key, 'img') # policy = conn.hget(key, 'policy') # default_policy = conn.hget(key, 'default_policy') # # # print(str(title,encoding='utf-8')) # print(str(img,encoding='utf-8')) # print(json.loads(str(policy,encoding='utf-8'))) # print(str(default_policy,encoding='utf-8')) print(conn.keys()) conn.scan_iter() print(conn.exists('luffy_sg_car_1_1'))
6.django-redis
应用(django): 1. 自定义使用redis 2. 使用第三方组件 pip3 install django-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} # "PASSWORD": "密码", } } } 使用: import redis from django.shortcuts import render,HttpResponse from django_redis import get_redis_connection def index(request): conn = get_redis_connection("default") return HttpResponse('设置成功') def order(request): conn = get_redis_connection("back") return HttpResponse('获取成功') 高级使用: 1. 全站缓存 2. 单视图 3. 局部页面 补充:rest framework框架访问频率限制推荐放到 redis/memecached
浙公网安备 33010602011771号