redis
Posted kakawith
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis相关的知识,希望对你有一定的参考价值。
1,redis是一个软件,帮助开发者对一台机器的内存进行操作。
2,使用
import redis conn = redis.Redis(host=‘47.94.172.250‘,port=6379,password=‘123456‘) conn.set(‘x1‘,‘wangwangwang‘,ex=5)#5s后超时 val = conn.get(‘x1‘) print(val)
3,连接池
import redis pool = redis.ConnectionPool(host=‘10.211.55.4‘, port=6379,password=‘123456‘,max_connections=1000) conn = redis.Redis(connection_pool=pool) conn.set(‘foo‘, ‘Bar‘)
from redis_pool import POOL while True: key = input(‘请输入key:‘) value = input(‘请输入value:‘) # 创建连接池 # 去连接池中获取连接 conn = redis.Redis(connection_pool=POOL) # 设置值 conn.set(key, value)
4,自定义连接池
import redis from django.shortcuts import render,HttpResponse from utils.redis_pool import POOL def index(request): conn = redis.Redis(connection_pool=POOL) conn.hset(‘kkk‘,‘age‘,18) return HttpResponse(‘设置成功‘) def order(request): conn = redis.Redis(connection_pool=POOL) conn.hget(‘kkk‘,‘age‘) return HttpResponse(‘获取成功‘)
hset用于可哈希字段的赋值
5,手动操作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(‘获取成功‘)
#settings中配置 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": "密码", } } } CACHES = { "back": { "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": "密码", } } }
以上是关于redis的主要内容,如果未能解决你的问题,请参考以下文章