Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use django-redis in redis

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly discusses the use of django-redis in redis. There is a certain reference value, friends in need can refer to, follow the editor to see how to use it.

Custom connection pool

This is the same way as ordinary py files to manipulate redis, with the following code

Views.py

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 (' set successfully') def order (request): conn = redis.Redis (connection_pool=POOL) conn.hget ('kkk','age') return HttpResponse (' get successful')

Operate redis through third-party components

Installation

Pip3 install django-redis

Configuration:

Settings.py

# redis configuration 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": "password",}

Use:

Views.py

Import redisfrom django.shortcuts import render,HttpResponsefrom django_redis import get_redis_connection def index (request): conn = get_redis_connection ("default") return HttpResponse ('set successfully') def order (request): conn = get_redis_connection ("default") return HttpResponse ('get success') from rest_framework.views import APIViewfrom rest_framework.response import Responsefrom django.core.cache import cache class OrderView (APIView): def get (self,request,*args) * * kwargs): # conn = get_redis_connection ('default') cache.set (' axim') print (cache.get ('a')) return Response ('..') Site-wide cache

Using middleware, after a series of authentication and other operations, if the content exists in the cache, use FetchFromCacheMiddleware to get the content and return it to the user

Before returning to the user, determine whether the cache already exists. If it does not exist, UpdateCacheMiddleware will save the cache to the cache, thus realizing the site-wide cache.

MIDDLEWARE = ['django.middleware.cache.UpdateCacheMiddleware', # other middleware... 'django.middleware.cache.FetchFromCacheMiddleware',]

One at the top and one at the bottom.

Views.py

From django.shortcuts import render,HttpResponseimport time def index (request): ctime = str (time.time ()) return HttpResponse (ctime) def order (request): ctime = str (time.time ()) return HttpResponse (ctime)

The full-site cache is configured, and the time returned by the above two views is the same at different times (within a certain range), both of which are cached.

Separate view caching

Way 1: through the decorator

From django.views.decorators.cache import cache_page @ cache_page (60 * 15) def my_view (request):... Method 2: through url

From django.views.decorators.cache import cache_page urlpatterns = [url (r'^ foo/ ([0-9] {1jue 2}) / $', cache_page (60 * 15) (my_view)),]

Local page cache

1. Introduction of TemplateTag

{% load cache%}

two。 Use cach

{% cache 5000 cached key%} cached content {% endcache%}

After reading the above, do you have a general idea of how to use django-redis in redis? If you want to know more about the content of the article, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report