django缓存 学习笔记

数据库缓存

  1. setting.py文件配置
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'db_cache',         # 指定缓存表
    }
}
  1. 创建cache表
    表名需和配置文件文件保持一致
python manage.py createcachetable db_cache
  1. 视图函数中使用
def cache_fruits(request):
    print(request.path)
    db_cache = caches["default"]        # 获取数据库缓存
    data = db_cache.get("fruits_cache")
    if data:
        print("缓存命中了,下面展示的是缓存的数据")
        return HttpResponse(data)
    else:
        print("没找到缓存,只能硬着头皮往前冲")
        time.sleep(2)
        fruits = ["apple", "banana", "orange", "watermelon"]
        response = render(request, "my_app/h_pager_cache/cache_fruits.html", {"fruits": fruits})
        db_cache.set("fruits_cache", response.content, timeout=10)    # 将缓存写入数据库并设置有效时间为10秒
        return response

Redis缓存

redis安装

  1. 安装依赖包
pip install django-redis
pip install django-redis-cache
  1. setting.py文件配置
# 缓存配置
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'db_cache',         # 指定缓存表
    },
    'Redis': {
        'BACKEND': "django_redis.cache.RedisCache",
        'LOCATION': 'redis://127.0.0.1:6379/1',            # 指定缓存数据库
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient',
        }
    },
}

  1. 视图函数中使用
def redis_cache(request):
    cache = caches["Redis"]
    result = cache.get("fruits")       # 获取redis的缓存
    if result:
        print("缓存命中了,下面展示的是缓存的数据")
        return HttpResponse(result)
    else:
        print("没找到缓存,只能硬着头皮往前冲")
        time.sleep(5)
        fruits = ["apple", "banana", "orange"]
        response = render(request, "my_app/h_pager_cache/cache_fruits.html", {"fruits": fruits})
        cache.set("fruits", response.content, timeout=600)
        return response

Memcache缓存

Mecache安装

  1. 依赖安装
pip install python-memcached
  1. setting.py文件配置
'Memcached': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211'
    }
  1. 视图函数中使用
def mem_cache(request):
    cache = caches["Memcached"]
    result = cache.get("fruits")       # 获取redis的缓存
    if result:
        print("缓存命中了,下面展示的是缓存的数据")
        return HttpResponse(result)
    else:
        print("没找到缓存,只能硬着头皮往前冲")
        time.sleep(5)
        fruits = ["苹果", "香蕉", "橘子", "西瓜"]
        response = render(request, "my_app/h_pager_cache/cache_fruits.html", {"fruits": fruits})
        cache.set("fruits", response.content, timeout=300)
        return response

程序层缓存

from django.views.decorators.cache import cache_page


@cache_page(30)         # 设置过期时间30秒
def dev_cache(request):
    fruits = ["苹果", "香蕉", "橘子", "西瓜"]
    return render(request, "my_app/h_pager_cache/cache_fruits.html", {"fruits": fruits})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值