Django-redis 使用

Django-redis 使用

  • 安装 django-redis

    pip 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": "密码",
            }
        }
    }
  • 使用 views.py

    from django.core.cache import cache
    from rest_framework.views import APIViews
    
    class OrderViews(APIViews):
        # 读取缓存信息
        def get(self,request):
            cache.set('key','value',timeout=10)
            return HttpResponse('ok')
        # 设置缓存信息
        def post(self,request):
            tmp = cache.get('key')
            return HttpResponse(tmp)
  • url.py

    from django.urls import path,include
    from . import views
    urlpatterns = [
        path('cache/',views.OrderView.as_view()),
    ]

你可能感兴趣的:(Django-redis 使用)