Redis - Invalid input of type: 'CacheKey'. Convert to a byte, string or number first.
💻 coding

Redis - Invalid input of type: 'CacheKey'. Convert to a byte, string or number first.

2 min read 349 words
2 min read
ShareWhatsAppPost on X
  • 1Django crashes with an 'Invalid input of type: CacheKey' error when using the latest Redis module.
  • 2The error indicates that the CacheKey must be converted to a byte, string, or number before use.
  • 3The issue occurs during the cache key retrieval process in the Django middleware.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"Django crashes with an 'Invalid input of type: CacheKey' error when using the latest Redis module."

Redis - Invalid input of type: 'CacheKey'. Convert to a byte, string or number first.

When I am trying to run Django with the latest version of the Redis module, it crashes:

Invalid input of type: 'CacheKey'. Convert to a byte, string or number first.

It's this message. Full stack trace:

Environment:

Request Method: GET
Request URL: http://localhost:8000/

Django Version: 2.1.3
Python Version: 3.6.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize',
 'django.contrib.postgres',
 'django.contrib.sites',
 'django.contrib.flatpages',
 'django.contrib.sitemaps',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'rest_framework',
 'rest_framework_docs',
 'rest_framework.authtoken',
 'drf_yasg',
 'django_bleach',
 'cspreports',
 'captcha',
 'django_markdown2',
 'crispy_forms',
 'api2',
 'headers',
 'worker',
 'cookies',
 'sslyze.apps.CoreConfig',
 'flash',
 'rss',
 'seo',
 'news',
 'contact',
 'browser_extension',
 'adstxt',
 'vuln',
 'busint',
 'csp_analytics',
 'reporting_api',
 'register_site.apps.CoreConfig',
 'utils',
 'stats',
 'phantomjs',
 'malware',
 'paypal.standard.ipn',
 'payments.apps.PaymentsConfig')
Installed Middleware:
['django.middleware.cache.UpdateCacheMiddleware',
 'django.middleware.gzip.GZipMiddleware',
 'django.middleware.http.ConditionalGetMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'middleware.dnt.DoNotTrackMiddleware',
 'middleware.preload.ResourceHintsMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.cache.FetchFromCacheMiddleware']

Traceback:

File "/home/sumit/wps2/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
 34. response = get_response(request)

File "/home/sumit/wps2/lib/python3.6/site-packages/django/utils/deprecation.py" in __call__
 90. response = self.process_request(request)

File "/home/sumit/wps2/lib/python3.6/site-packages/django/middleware/cache.py" in process_request
 137. cache_key = get_cache_key(request, self.key_prefix, 'GET', cache=self.cache)

File "/home/sumit/wps2/lib/python3.6/site-packages/django/utils/cache.py" in get_cache_key
 335. headerlist = cache.get(cache_key)

File "/home/sumit/wps2/lib/python3.6/site-packages/django_redis/cache.py" in _decorator
 32. return method(self, *args, **kwargs)

File "/home/sumit/wps2/lib/python3.6/site-packages/django_redis/cache.py" in get
 81. client=client)

File "/home/sumit/wps2/lib/python3.6/site-packages/django_redis/client/default.py" in get
 202. value = client.get(key)

File "/home/sumit/wps2/lib/python3.6/site-packages/redis/client.py" in get
 1207. return self.execute_command('GET', name)

File "/home/sumit/wps2/lib/python3.6/site-packages/redis/client.py" in execute_command
 754. connection.send_command(*args)

File "/home/sumit/wps2/lib/python3.6/site-packages/redis/connection.py" in send_command
 619. self.send_packed_command(self.pack_command(*args))

File "/home/sumit/wps2/lib/python3.6/site-packages/redis/connection.py" in pack_command
 659. for arg in imap(self.encoder.encode, args):

File "/home/sumit/wps2/lib/python3.6/site-packages/redis/connection.py" in encode
 124. "byte, string or number first." % typename)

Exception Type: DataError at /
Exception Value: Invalid input of type: 'CacheKey'. Convert to a byte, string or number first.

Solution:

This issue needs to be fixed in django-redis. I think this is not-a-bug as far as redis-py is concerned.

redis-py 3.0 only accepts keys and values as bytes, strings or numbers. This is a backwards incompatible change listed in the release notes.

I suspect django-redis needs to be updated to convert the CacheKey type into a string before sending it into redis-py.

django-redis isn't constraining it's dependency on py-redis to version 2.

 install_requires=[
 "Django>=1.11",
 "redis>=2.10.0",
 ],

I imagine a lot of people are going to get stung by this so expect django-redis to get fixed soon.
If you can't wait for the fix, you could pin the last v2 of py-redis in your requirements.txt file
redis==2.10.6

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 8 December 2018 · 2 min read · 349 words

Part of AskGif Blog · coding

You might also like