pythondjangopython-memcacheddjango-environ

How do I replace MemcachedCache with PyMemcacheCache in Django?


I'm running my website on Django 3.2. I read in Django’s cache framework that MemcachedCache and python-memcached are deprecated. I installed pymemcache==3.5.0 on my staging server and changed to CACHE_URL=pymemcache://127.0.0.1:11211 in env.ini. But if I uninstall python-memcached with pip I receive an error message, that indicates that MemcachedCache is still used by my code, and it fails on import memcache.

My code uses the following imports:

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

How do I replace MemcachedCache with PyMemcacheCache so that MemcachedCache will not be used in my code?

I'm using django-environ==0.8.1 and CACHES is defined in my settings:

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

env is defined as environ.Env().

My Traceback:

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'

My repository: https://github.com/speedy-net/speedy-net

Update: I checked the email message I received for debugging and I see that the CACHES in settings is equal to {'default': {'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': '127.0.0.1:11211'}}.

I submitted an issue (possibly a bug) on https://github.com/joke2k/django-environ/issues/359


Solution

  • I don't know why, but the way I restarted my server during deploy doesn't refresh env.ini, and the server remembers the old settings. If I restart my server with sudo reboot, with the same settings, then CACHES is equal to {'default': {'BACKEND': 'django.core.cache.backends.memcached.PyMemcacheCache', 'LOCATION': '127.0.0.1:11211'}} and the site works properly. And I confirmed that if I put dummy values in env.ini then the site doesn't work at all. But only if I restart my server with sudo reboot.