I would like to load a TfidfVectorizer fitted model using a Django command and then reuse it on view. So in the command
from django.core.cache import cache
from sklearn.feature_extraction.text import TfidfVectorizer
class Command(BaseCommand):
....
model = TfidfVectorizer()
modelfitted = model.fit(data)
cache.set('model',modelfitted)
Then in views.py I would like to call it:
def test_function(request):
mod = cache.get('model')
the 'mod' object is None. Any idea?
The problem is that you are using Local-memory caching
. As stated in the docs this cache is per-process :
Note that each process will have its own private cache instance, which means no cross-process caching is possible. This obviously also means the local memory cache isn't particularly memory-efficient, so it's probably not a good choice for production environments. It's nice for development.
So can't write to the cache in a managemant command and then call this value in your runserver
process.
You should change you cache backend for example to Filesystem caching
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
'LOCATION': '/var/tmp/django_cache',
}
}
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.