“This is the 11th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

preface

Many people assume that Django is AN SQL-oriented framework, because most of Django’s Models are SQL oriented. There are many reasons for this, but the main reason is that in most cases we rely more on SQL, a website can not go on without Redis but without SQL, unless you are just a very simple static page of information display, no data interaction, or very little interaction. Other databases are also available in Django, but the support is not as good as SQL.

Native cache

This caching service actually comes with a built-in cache in Django, which stores the cache to the database. Of course, it’s not as fast as Redis, but hard disk is much cheaper than memory. You have to build a table for this

python manage.py createcachetable my_table_name
Copy the code

Configuration:This opens the Settings file

CHACHES={
    'default': {'BACKEND':"django.core.cache.backends.db.DatabaseCache".'LOCATION':"my_cache_table".'TIMEOUT':'60',}}Copy the code

Of course, there are other configurations, but the first ones are basically enough. And of course you can add options

CHACHES={
    'default': {'BACKEND':"django.core.cache.backends.db.DatabaseCache".'LOCATION':"my_cache_table".'TIMEOUT':'60'.'OPTIONS': {"MAX_ENIRIES":'300',},
		'KEY_PREFIX':'Huterox',}}Copy the code

There are a number of ways to use cache acceleration, the simplest of which is simply to use a cache decorator.

def index(request) :
	sleep(5)
	return HttpResponse("Hello")
Copy the code
@chache_page(60.'Huterox')# Expiration time, if you add option then add the corresponding parameter
def index(request) :
	sleep(5)
	return HttpResponse("Hello")
Copy the code

Then a complete process is as follows:

Custom cache

The front decorator is a one-sip-fits-all, which is obviously not very good, and after we do limit the number of user access should also be customized. Let’s start with a couple of methods set(key,value) get(key) is the core of this

def index(request) :
	result=cache.get('index')
	if result:
		return HttpResponse(result)
	else:
		#index=....
		# here is the operation of the index page for the sake of simplicity I will just let it sleep for five seconds to simulate loading a lot of pages.
		sleep(5)
		index="Hello"
		cache.set('index',index,timeout=60)
		return Httpresponse(index)
Copy the code

Using Redis

Since django doesn’t have Redis native, we need to install a third-party django plugin

pip install django-redis
pip install django-redis-cache
Copy the code

And then you can configure this configuration and there’s actually two ways to do it one is directly configure it

CHACHES={
    'default': {'BACKEND':"django_redis.cache.RedisCache".'LOCATION':"Redis: / / 127.0.0.1:6379/1".Data address port and database library (/1 first library)
       	"OPTIONS": {"CLIENT_CLASS":"django_redis.client.DefaultClient",}}}Copy the code

So if this configuration is used, then the old method is used. There is also a configuration for both.

CHACHES={
    'default': {'BACKEND':"django.core.cache.backends.db.DatabaseCache".'LOCATION':"my_cache_table".'TIMEOUT':'60',}'redis': {'BACKEND':"django_redis.cache.RedisCache".'LOCATION':"Redis: / / 127.0.0.1:6379/1".Data address port and database library (/1 first library)
   	"OPTIONS": {"CLIENT_CLASS":"django_redis.client.DefaultClient",}}}Copy the code

If you want to continue using the native cache() or

cache = caches['default']
@cache_page(60,cache='default')
Copy the code

If you use Redis

cache = caches['redis']
@cache_page(60,cache='redis')
Copy the code