10 import pickle |
10 import pickle |
11 |
11 |
12 class CacheClass(BaseCache): |
12 class CacheClass(BaseCache): |
13 def __init__(self, table, params): |
13 def __init__(self, table, params): |
14 BaseCache.__init__(self, params) |
14 BaseCache.__init__(self, params) |
15 self._table = table |
15 self._table = connection.ops.quote_name(table) |
16 max_entries = params.get('max_entries', 300) |
16 max_entries = params.get('max_entries', 300) |
17 try: |
17 try: |
18 self._max_entries = int(max_entries) |
18 self._max_entries = int(max_entries) |
19 except (ValueError, TypeError): |
19 except (ValueError, TypeError): |
20 self._max_entries = 300 |
20 self._max_entries = 300 |
58 cursor.execute("SELECT cache_key, expires FROM %s WHERE cache_key = %%s" % self._table, [key]) |
58 cursor.execute("SELECT cache_key, expires FROM %s WHERE cache_key = %%s" % self._table, [key]) |
59 try: |
59 try: |
60 result = cursor.fetchone() |
60 result = cursor.fetchone() |
61 if result and (mode == 'set' or |
61 if result and (mode == 'set' or |
62 (mode == 'add' and result[1] < now)): |
62 (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]) |
64 else: |
65 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)]) |
66 except DatabaseError: |
68 except DatabaseError: |
67 # To be threadsafe, updates/inserts are allowed to fail silently |
69 # To be threadsafe, updates/inserts are allowed to fail silently |
68 transaction.rollback() |
70 transaction.rollback_unless_managed() |
69 return False |
71 return False |
70 else: |
72 else: |
71 transaction.commit_unless_managed() |
73 transaction.commit_unless_managed() |
72 return True |
74 return True |
73 |
75 |
77 transaction.commit_unless_managed() |
79 transaction.commit_unless_managed() |
78 |
80 |
79 def has_key(self, key): |
81 def has_key(self, key): |
80 now = datetime.now().replace(microsecond=0) |
82 now = datetime.now().replace(microsecond=0) |
81 cursor = connection.cursor() |
83 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)]) |
83 return cursor.fetchone() is not None |
86 return cursor.fetchone() is not None |
84 |
87 |
85 def _cull(self, cursor, now): |
88 def _cull(self, cursor, now): |
86 if self._cull_frequency == 0: |
89 if self._cull_frequency == 0: |
87 cursor.execute("DELETE FROM %s" % self._table) |
90 self.clear() |
88 else: |
91 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)]) |
90 cursor.execute("SELECT COUNT(*) FROM %s" % self._table) |
94 cursor.execute("SELECT COUNT(*) FROM %s" % self._table) |
91 num = cursor.fetchone()[0] |
95 num = cursor.fetchone()[0] |
92 if num > self._max_entries: |
96 if num > self._max_entries: |
93 cursor.execute("SELECT cache_key FROM %s ORDER BY cache_key LIMIT 1 OFFSET %%s" % self._table, [num / self._cull_frequency]) |
97 cursor.execute("SELECT cache_key FROM %s ORDER BY cache_key LIMIT 1 OFFSET %%s" % self._table, [num / self._cull_frequency]) |
94 cursor.execute("DELETE FROM %s WHERE cache_key < %%s" % self._table, [cursor.fetchone()[0]]) |
98 cursor.execute("DELETE FROM %s WHERE cache_key < %%s" % self._table, [cursor.fetchone()[0]]) |
|
99 |
|
100 def clear(self): |
|
101 cursor = connection.cursor() |
|
102 cursor.execute('DELETE FROM %s' % self._table) |