django中集成redis

django中集成redis

方案一:

-写一个pool包
import redis
POOL=redis.ConnectionPool(max_connections=1024,decode_responses=True)
-在使用的位置导入
    conn = Redis(connection_pool=POOL)
    res = conn.get('name')

方案二:使用第三方:django-redis

1.安装第三方库 
	pip install django-redis

2.在django配置文件中配置
	 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",
                }
            }
        }
     -在使用的位置
    	conn = get_redis_connection()
        conn.set.....
        
    # 一旦配置文件配置 CACHES 后,django的缓存框架(默认是内存),存储的位置也是redis
    # 以后根本不需要会redis的操作,只需要使用 
    cache.set('name',对象)
    cache.get()
    # 强大之处在于不需要关注设置的类型,直接设置就行
    # 把python的对象,通过pickle序列化后,以string形式存到redis中了
posted @ 2022-07-14 17:35  春游去动物园  阅读(189)  评论(0编辑  收藏  举报