Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #19273 -- Fixed DB cache backend on pg 9.0+ and py3

There was a problem caused by Postgres 9.0+ having bytea_output default
value of 'hex' and cache backend inserting the content as 'bytes' into
a column of type TEXT. Fixed by converting the bytes value to a string
before insert.
  • Loading branch information...
commit cc0ac26f4a3947be8a3fc55d4784d3474b640c23 1 parent 04a7ea3
Anssi Kääriäinen authored November 10, 2012

Showing 1 changed file with 8 additions and 4 deletions. Show diff stats Hide diff stats

  1. 12  django/core/cache/backends/db.py
12  django/core/cache/backends/db.py
@@ -11,7 +11,7 @@
11 11
 from django.conf import settings
12 12
 from django.core.cache.backends.base import BaseCache
13 13
 from django.db import connections, router, transaction, DatabaseError
14  
-from django.utils import timezone
  14
+from django.utils import timezone, six
15 15
 from django.utils.encoding import force_bytes
16 16
 
17 17
 
@@ -104,7 +104,11 @@ def _base_set(self, mode, key, value, timeout=None):
104 104
         if num > self._max_entries:
105 105
             self._cull(db, cursor, now)
106 106
         pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
107  
-        encoded = base64.b64encode(pickled).strip()
  107
+        b64encoded = base64.b64encode(pickled)
  108
+        # The DB column is expecting a string, so make sure the value is a
  109
+        # string, not bytes. Refs #19274.
  110
+        if six.PY3:
  111
+            b64encoded = b64encoded.decode('latin1')
108 112
         cursor.execute("SELECT cache_key, expires FROM %s "
109 113
                        "WHERE cache_key = %%s" % table, [key])
110 114
         try:
@@ -113,11 +117,11 @@ def _base_set(self, mode, key, value, timeout=None):
113 117
                     (mode == 'add' and result[1] < now)):
114 118
                 cursor.execute("UPDATE %s SET value = %%s, expires = %%s "
115 119
                                "WHERE cache_key = %%s" % table,
116  
-                               [encoded, connections[db].ops.value_to_db_datetime(exp), key])
  120
+                               [b64encoded, connections[db].ops.value_to_db_datetime(exp), key])
117 121
             else:
118 122
                 cursor.execute("INSERT INTO %s (cache_key, value, expires) "
119 123
                                "VALUES (%%s, %%s, %%s)" % table,
120  
-                               [key, encoded, connections[db].ops.value_to_db_datetime(exp)])
  124
+                               [key, b64encoded, connections[db].ops.value_to_db_datetime(exp)])
121 125
         except DatabaseError:
122 126
             # To be threadsafe, updates/inserts are allowed to fail silently
123 127
             transaction.rollback_unless_managed(using=db)

0 notes on commit cc0ac26

Please sign in to comment.
Something went wrong with that request. Please try again.