如何在 Django 中用 PyMemcacheCache 替换 MemcachedCache?

How do I replace MemcachedCache with PyMemcacheCache in Django?

我 运行 我的网站在 Django 3.2 上。我在 Django’s cache framework 中读到 MemcachedCachepython-memcached 已弃用。我在临时服务器上安装了 pymemcache==3.5.0,并在 env.ini 中更改为 CACHE_URL=pymemcache://127.0.0.1:11211。但是,如果我使用 pip 卸载 python-memcached,我会收到一条错误消息,表明我的代码仍在使用 MemcachedCache,并且在 import memcache.

上失败

我的代码使用了以下导入:

from django.core.cache import cache
from django.core.cache.backends.base import DEFAULT_TIMEOUT

如何将 MemcachedCache 替换为 PyMemcacheCache 以便 MemcachedCache 不会在我的代码中使用?

我正在使用 django-environ==0.8.1 并且 CACHES 在我的设置中定义:

CACHES = {
    'default': env.cache()
}

env 定义为 environ.Env().

我的回溯:

Environment:


Request Method: GET
Request URL: https://<...>

Django Version: 3.2.10
Python Version: 3.8.12
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'crispy_forms',
 'friendship',
 'rules.apps.AutodiscoverRulesConfig',
 'sorl.thumbnail',
 'speedy.core.base',
 'speedy.core.accounts',
 'speedy.core.blocks',
 'speedy.core.uploads',
 'speedy.core.messages',
 'speedy.core.profiles',
 'speedy.core.friends',
 'speedy.core.about',
 'speedy.core.privacy',
 'speedy.core.terms',
 'speedy.net.accounts',
 'speedy.match.accounts',
 'speedy.match.likes',
 'speedy.composer.accounts',
 'speedy.mail.accounts',
 'speedy.core.contact_by_form',
 'speedy.match.profiles',
 'speedy.match.matches']
Installed Middleware:
['speedy.core.base.middleware.SessionCookieDomainMiddleware',
 'speedy.core.base.middleware.RemoveExtraSlashesMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.contrib.sites.middleware.CurrentSiteMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'speedy.core.base.middleware.LocaleDomainMiddleware',
 'speedy.core.base.middleware.EnsureCachesMiddleware',
 'speedy.core.base.middleware.UpdateSessionAuthHashMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'speedy.core.accounts.middleware.SiteProfileMiddleware']



Traceback (most recent call last):
  File "<...>/env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "<...>/./speedy/core/base/middleware.py", line 139, in __call__
    block_managers.ensure_caches(user=request.user)
  File "<...>/./speedy/core/blocks/managers.py", line 46, in ensure_caches
    blocked_entities = cache_manager.cache_get(blocked_key, sliding_timeout=DEFAULT_TIMEOUT)
  File "<...>/./speedy/core/base/cache_manager.py", line 21, in cache_get
    wrapped_value = cache.get(key, default=DEFAULT_VALUE, version=version)
  File "<...>/env/lib/python3.8/site-packages/django/utils/connection.py", line 15, in __getattr__
    return getattr(self._connections[self._alias], item)
  File "<...>/env/lib/python3.8/site-packages/django/utils/connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
  File "<...>/env/lib/python3.8/site-packages/django/core/cache/__init__.py", line 44, in create_connection
    return backend_cls(location, params)
  File "<...>/env/lib/python3.8/site-packages/django/core/cache/backends/memcached.py", line 181, in __init__
    import memcache

Exception Type: ModuleNotFoundError at /matches/
Exception Value: No module named 'memcache'

我的存储库:https://github.com/speedy-net/speedy-net

更新: 我检查了收到的用于调试的电子邮件消息,我看到设置中的 CACHES 等于 {'default': {'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': '127.0.0.1:11211'}}.

我在 https://github.com/joke2k/django-environ/issues/359

上提交了一个问题(可能是一个错误)

我不知道为什么,但我在部署期间重新启动服务器的方式没有刷新 env.ini,并且服务器记住了旧设置。如果我用 sudo reboot 重新启动我的服务器,设置相同,那么 CACHES 等于 {'default': {'BACKEND': 'django.core.cache.backends.memcached.PyMemcacheCache', 'LOCATION': '127.0.0.1:11211'}} 并且站点正常工作。而且我确认,如果我在 env.ini 中放置虚拟值,那么该网站根本无法正常工作。但前提是我用 sudo reboot.

重启我的服务器