[PYTHON] I tried using firebase for Django's cache server

Background of use

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.

Easy cache method

Put it in memory

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
    }

Put on DB

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

Think about what you can do with Firebase

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.

Introduction

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.

A trap from here ...

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

I tried using firebase for Django's cache server
I tried using parameterized
I tried using argparse
I tried using scrapy for the first time
I tried using mimesis
I tried using anytree
vprof --I tried using the profiler for Python
I tried using aiomysql
I tried using Summpy
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried using openpyxl
I tried using Ipython
I tried using PyCaret
I tried using cron
I tried using ngrok
I tried using Jupyter
I tried using PyCaret
I tried using Heapq
I tried using doctest
I tried using folium
I tried using jinja2
I tried using folium
I tried using time-window
[Python] I tried running a local server using flask
[I tried using Pythonista 3] Introduction
I tried using easydict (memo).
I tried face recognition using Face ++
I tried using Random Forest
I tried using BigQuery ML
I tried using Amazon Glacier
I tried using git inspector
[Python] I tried using OpenPose
I tried using magenta / TensorFlow
I tried using AWS Chalice
I tried using Slack emojinator
I tried starting Django's server with VScode instead of Pycharm
I tried using Tensorboard, a visualization tool for machine learning
Miscellaneous notes that I tried using python for the matter
I tried to output the access log to the server using Node.js
[For beginners] I tried using the Tensorflow Object Detection API
I tried using Rotrics Dex Arm # 2
I tried using Rotrics Dex Arm
I tried using GrabCut of OpenCV
I tried using Thonny (Python / IDE)
I tried server-client communication using tmux
I tried reinforcement learning using PyBrain
I tried deep learning using Theano
Somehow I tried using jupyter notebook
[Kaggle] I tried undersampling using imbalanced-learn
I tried shooting Kamehameha using OpenPose
I tried using the checkio API
[Python] I tried using YOLO v3
I tried asynchronous processing using asyncio
I tried using the python module Kwant for quantum transport calculation
I tried using GLM (generalized linear model) for stock price data
I tried using Amazon SQS with django-celery