简体   繁体   English

我可以在python Django上使用数据库缓存永久缓存数据吗

[英]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. 我在python中有一个方法,它对我发送的输入进行一些处理,处理完成后它返回一个值。

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. 因此,我现在正在为类似的输入使用django数据库缓存来缓存返回值。 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/ ). 在Django网站上,提到“不应该将Django缓存后端用于永久存储”( 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: 请注意,默认情况下,缓存的超时时间为300秒,您需要在设置文件中将缓存的TIMEOUT参数显式设置为None ,例如:

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() ). 但是,对于我来说,使用数据库高速缓存存储要永久保存的数据似乎是一种不好的做法(例如,清除整个高速缓存只是一种方法调用cache.clear() )。 Why don't you create a model dedicated to storing your results ? 您为什么不创建一个专用于存储结果的模型?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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