django 目录配置与使用redis
1,以后的django项目都是在DRF框架的之上的
2,setting文件下dev之中添加添加redis
CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379/0", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", } }, "session": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379/1", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", } }, "verify_codes": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379/2", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", } } }
3,verify_codes 是自定义的redis 库用来存储验证码
from django_redis import get_redis_connection #连接自定义redis redis_conn = get_redis_connection("verify_codes") #将验证码存储到redis之中 redis_conn.setex("img_%s" % image_code_id, constants.IMAGE_CODE_REDIS_EXPIRES, text) data_fo = redis_code.get("img_%s" % image_code_id)