如何使用django在cacheops中缓存的结果

How to use the cached results in cacheops with django

我正在尝试在我有一个包含我要缓存的变量的视图中使用 cacheops

bo = Bo.objects.all().cache()

我尝试在另一个视图中使用的 chached bo

all = cache.get('bo')

这不起作用 这是回溯

....
  File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\cacheops\simple.py", line 83, in get
    return self._get(get_prefix() + cache_key)
  File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\cacheops\simple.py", line 99, in _get
    raise CacheMiss
cacheops.simple.CacheMiss

我的settings.py

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    },
    'select2': {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/2",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    }
}

CACHEOPS_DEGRADE_ON_FAILURE=True
CACHEOPS_ENABLED = True
CACHEOPS_REDIS = {
    'host': 'localhost',
    'port': 6379,}
CACHEOPS = {
    'libman.*': {'ops': 'all', 'timeout': 60*15},}

我可以通过将它放在 try except 块中来解决它

try:
    bo = cache.get('bo')
except CacheMiss:
    bo = Bo.objects.all().cache()
    cache.set('bo',bo)