Ticket #11483: dbcachebackend.patch

File dbcachebackend.patch, 2.2 KB (added by leosoto, 6 years ago)
  • django/core/cache/backends/db.py

    diff -r d7a5c9caa5dd django/core/cache/backends/db.py
    a b  
    6060            result = cursor.fetchone()
    6161            if result and (mode == 'set' or
    6262                    (mode == 'add' and result[1] < now)):
    63                 cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE cache_key = %%s" % self._table, [encoded, str(exp), key])
     63                cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE cache_key = %%s" % self._table,
     64                               [encoded, connection.ops.value_to_db_datetime(exp), key])
    6465            else:
    65                 cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)])
     66                cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table,
     67                               [key, encoded, connection.ops.value_to_db_datetime(exp)])
    6668        except DatabaseError:
    6769            # To be threadsafe, updates/inserts are allowed to fail silently
    6870            transaction.rollback()
     
    7981    def has_key(self, key):
    8082        now = datetime.now().replace(microsecond=0)
    8183        cursor = connection.cursor()
    82         cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s and expires > %%s" % self._table, [key, now])
     84        cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s and expires > %%s" % self._table,
     85                       [key, connection.ops.value_to_db_datetime(now)])
    8386        return cursor.fetchone() is not None
    8487
    8588    def _cull(self, cursor, now):
    8689        if self._cull_frequency == 0:
    8790            cursor.execute("DELETE FROM %s" % self._table)
    8891        else:
    89             cursor.execute("DELETE FROM %s WHERE expires < %%s" % self._table, [str(now)])
     92            cursor.execute("DELETE FROM %s WHERE expires < %%s" % self._table,
     93                           [connection.ops.value_to_db_datetime(now)])
    9094            cursor.execute("SELECT COUNT(*) FROM %s" % self._table)
    9195            num = cursor.fetchone()[0]
    9296            if num > self._max_entries:
Back to Top