~kkubasik/django/aggregation-branch

« back to all changes in this revision

Viewing changes to django/core/cache/backends/db.py

  • Committer: adrian
  • Date: 2006-05-02 01:31:56 UTC
  • Revision ID: vcs-imports@canonical.com-20060502013156-2941fcd40d080649
MERGED MAGIC-REMOVAL BRANCH TO TRUNK. This change is highly backwards-incompatible. Please read http://code.djangoproject.com/wiki/RemovingTheMagic for upgrade instructions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"Database cache backend."
2
2
 
3
3
from django.core.cache.backends.base import BaseCache
4
 
from django.core.db import db, DatabaseError
 
4
from django.db import connection, transaction
5
5
import base64, time
6
6
from datetime import datetime
7
7
try:
25
25
            self._cull_frequency = 3
26
26
 
27
27
    def get(self, key, default=None):
28
 
        cursor = db.cursor()
 
28
        cursor = connection.cursor()
29
29
        cursor.execute("SELECT cache_key, value, expires FROM %s WHERE cache_key = %%s" % self._table, [key])
30
30
        row = cursor.fetchone()
31
31
        if row is None:
33
33
        now = datetime.now()
34
34
        if row[2] < now:
35
35
            cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key])
36
 
            db.commit()
 
36
            transaction.commit_unless_managed()
37
37
            return default
38
38
        return pickle.loads(base64.decodestring(row[1]))
39
39
 
40
40
    def set(self, key, value, timeout=None):
41
41
        if timeout is None:
42
42
            timeout = self.default_timeout
43
 
        cursor = db.cursor()
 
43
        cursor = connection.cursor()
44
44
        cursor.execute("SELECT COUNT(*) FROM %s" % self._table)
45
45
        num = cursor.fetchone()[0]
46
46
        now = datetime.now().replace(microsecond=0)
58
58
            # To be threadsafe, updates/inserts are allowed to fail silently
59
59
            pass
60
60
        else:
61
 
            db.commit()
 
61
            transaction.commit_unless_managed()
62
62
 
63
63
    def delete(self, key):
64
 
        cursor = db.cursor()
 
64
        cursor = connection.cursor()
65
65
        cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key])
66
 
        db.commit()
 
66
        transaction.commit_unless_managed()
67
67
 
68
68
    def has_key(self, key):
69
 
        cursor = db.cursor()
 
69
        cursor = connection.cursor()
70
70
        cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key])
71
71
        return cursor.fetchone() is not None
72
72