The product operated by the company uses AWS Redis as a cache server, but it is expensive for my own use and can only be accessed from within the VPC (although it can be done by proxy). It's a hassle, so I searched for something else that could be used as a cache server.
I think the easiest way is to put it in local memory, which is fine for testing, but often not enough for production.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'default',
'TIMEOUT': 24 * 60 * 60
}
Create a table in the DB, create cache data in it, and go to read it. Naturally, the load on the DB will increase, and the DB will become bloated.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'cache_table',
'TIMEOUT': 24 * 60 * 60
}
}
It is essential to make a table.
python manage.py createcachetable
I thought that AWS couldn't be used and memory and DB were difficult, but it was a Firebase service that I often use these days. Looking at Firebase, I wonder if there are people who are making packages. https://github.com/christippett/django-firebase-cache
So I decided to use this package.
It's a little more complicated to use than the simple one above.
Package installation.
pip install django-firebase-cache
Set in CACHES.
CACHES = {
'default': {
'BACKEND': 'django_firebase_cache.FirestoreCache',
'LOCATION': 'collection_name',
'TIMEOUT': 24 * 60 * 60
}
}
Set the Firebase credential file and pass it through the path.
I was a little confused here, but I need to pass the path to the environment variable GOOGLE_APPLICATION_CREDENTIALS
as follows. The documentation isn't complete at all ...
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = './client_credentials.json'
This completes the settings. With this, you can confirm that the data will be stored in Firebase's FireStore when you actually move it.
Although cashing has become possible up to this point, the upper limit of the free quota has been reached unexpectedly soon. .. In that case, of course, an error is returned, but the site has been dead for several hours without any processing to avoid the error. ..
Let's pay for it or handle errors.
Recommended Posts