Some business scenarios require frequent read operations on the database, but there are relatively few write operations. Causes greater pressure on the database. In order to reduce the pressure on the database, you can use the django model semaphore and redisco to achieve database read and write separation.
The basic idea
Reading uses redisco to read data from redis, writing uses django model to write data to the database, and uses the semaphore of django model to trigger the synchronization of changes in the database to redis. Since redis is based on memory, the amount of data in separate tables for reading and writing cannot be too large, otherwise it will occupy too much memory. There are many online tutorials on semaphores and redisco of django model, so I wo n’t repeat them here.
Implementation
Define the django model model in models.py
from django.db import models
class ArticleModel(models.Model):
class Meta:
db_table = 'article'
title = models.CharField(max_length=100, primary_key=True)
content = models.CharField(max_length=200)
def __str__(self):
return self.title
Define the redisco model in models.py, whose fields are exactly the same as the django model model
from redisco import models as redis_models
class ArticleRedisModel(redis_models.Model):
title = redis_models.CharField(max_length=100)
content = redis_models.CharField(max_length=200)
def __str__(self):
return str(self.title)
Define the operations triggered by the semaphore of the django model in models.py, that is, after the database is modified, synchronize the modified content to redis
Since redis is based on memory, restarting may lose data. After redis restarts, the data needs to be synchronized to ensure that the memory in the database is the same as that in redis. Define synchronous data interface in urls.py and views.py
from django.conf.urls import url
urlpatterns = [
url(r'^sync$', sync_redis_db),
]
from django.http import HttpResponse
from .models import ArticleModel, ArticleRedisModel
def sync_redis_db(request):
[article.delete() for article in ArticleRedisModel.objects.all()]
[ArticleRedisModel(title=article.title, content=article.content).save() for article in ArticleModel.objects.all()]
return HttpResponse('sync db to redis success')
Then after each redis restart, the sync interface is called to synchronize the data in the database to redis. The synchronization operation can be written into the redis startup script
So far, the write (create, delete, update) operations to the database are implemented through the django model (that is, the ArticleModel here). The reading operation of the data can be read through redisco (that is, ArticleRedisModel here). Thus, the read-write separation of the database is realized. In the scenario of frequently reading the database, the pressure on the database can be greatly reduced. At the same time, because redis is based on memory, the reading speed will be greatly enhanced.