Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kravietz opened this issue Nov 15, 2018 · 6 comments
Closed

Comments

@kravietz
Copy link

Django run with the latest version of the redis module 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/kravietz/wps2/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

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

File "/home/kravietz/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/kravietz/wps2/lib/python3.6/site-packages/django/utils/cache.py" in get_cache_key
  335.     headerlist = cache.get(cache_key)

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

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

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

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

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

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

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

File "/home/kravietz/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.

@andymccurdy
Copy link
Contributor

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.

@mikemonteith
Copy link

Related django-redis issue: jazzband/django-redis#342

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

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

https://github.com/niwinz/django-redis/blob/master/setup.py#L25

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

@jdufresne
Copy link
Contributor

jdufresne commented Nov 15, 2018

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

@andymccurdy
Copy link
Contributor

@jdufresne is correct. django-redis needs to fix this in their code base.

@dimittal
Copy link

Was this issue resolved in a future version of django-redis? I use 4.10.0 and seems its still not fixed.

@wmarshall484
Copy link

Also have gotten stung by this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants
@andymccurdy @mikemonteith @jdufresne @kravietz @dimittal @wmarshall484 and others