简体   繁体   中英

Can I cache data permanently using database caching on python Django

I have a method in python which do some processing on the input i send, and it returns a value after processing is done.

This method is taking lot of time to give the results. So, what i am doing now is for similar inputs I am caching return value using django Database caching. it worked well.

But I need the data to be stored in caching database for permanent use.

On Django website it is mentioned that "none of the Django caching backends should be used for permanent storage"( https://docs.djangoproject.com/en/1.9/topics/cache/ ).

I can store the data on cache database as well as regular database. But it will be performance problem.

So, what approach should i follow to do this with out performance issues.

As long as you do not set a timeout to your cache entries, they should not be cleared without you explicitly doing so.

Be careful that by default, caches have a 300 seconds timeout, you need to set your cache's TIMEOUT parameter to None explicitly in your settings file, example:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.db',
        'TIMEOUT': None,
    }
}

However, it seems bad practice to me to use the database cache to store data that you want permanent (for example, clearing the whole cache is only one method call away -- cache.clear() ). Why don't you create a model dedicated to storing your results ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM