~ubuntu-branches/ubuntu/saucy/python-django/saucy-updates

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Luke Faraone, Jakub Wilk, Luke Faraone
  • Date: 2013-05-09 15:10:47 UTC
  • mfrom: (1.1.21) (4.4.27 sid)
  • Revision ID: package-import@ubuntu.com-20130509151047-aqv8d71oj9wvcv8c
Tags: 1.5.1-2
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Luke Faraone ]
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
import shutil
6
6
import time
7
7
try:
8
 
    import cPickle as pickle
 
8
    from django.utils.six.moves import cPickle as pickle
9
9
except ImportError:
10
10
    import pickle
11
11
 
12
12
from django.core.cache.backends.base import BaseCache
 
13
from django.utils.encoding import force_bytes
13
14
 
14
15
class FileBasedCache(BaseCache):
15
16
    def __init__(self, dir, params):
31
32
 
32
33
        fname = self._key_to_file(key)
33
34
        try:
34
 
            f = open(fname, 'rb')
35
 
            try:
 
35
            with open(fname, 'rb') as f:
36
36
                exp = pickle.load(f)
37
37
                now = time.time()
38
38
                if exp < now:
39
39
                    self._delete(fname)
40
40
                else:
41
41
                    return pickle.load(f)
42
 
            finally:
43
 
                f.close()
44
42
        except (IOError, OSError, EOFError, pickle.PickleError):
45
43
            pass
46
44
        return default
61
59
            if not os.path.exists(dirname):
62
60
                os.makedirs(dirname)
63
61
 
64
 
            f = open(fname, 'wb')
65
 
            try:
 
62
            with open(fname, 'wb') as f:
66
63
                now = time.time()
67
64
                pickle.dump(now + timeout, f, pickle.HIGHEST_PROTOCOL)
68
65
                pickle.dump(value, f, pickle.HIGHEST_PROTOCOL)
69
 
            finally:
70
 
                f.close()
71
66
        except (IOError, OSError):
72
67
            pass
73
68
 
94
89
        self.validate_key(key)
95
90
        fname = self._key_to_file(key)
96
91
        try:
97
 
            f = open(fname, 'rb')
98
 
            try:
 
92
            with open(fname, 'rb') as f:
99
93
                exp = pickle.load(f)
100
 
                now = time.time()
101
 
                if exp < now:
102
 
                    self._delete(fname)
103
 
                    return False
104
 
                else:
105
 
                    return True
106
 
            finally:
107
 
                f.close()
 
94
            now = time.time()
 
95
            if exp < now:
 
96
                self._delete(fname)
 
97
                return False
 
98
            else:
 
99
                return True
108
100
        except (IOError, OSError, EOFError, pickle.PickleError):
109
101
            return False
110
102
 
145
137
        Thus, a cache key of "foo" gets turnned into a file named
146
138
        ``{cache-dir}ac/bd/18db4cc2f85cedef654fccc4a4d8``.
147
139
        """
148
 
        path = hashlib.md5(key).hexdigest()
 
140
        path = hashlib.md5(force_bytes(key)).hexdigest()
149
141
        path = os.path.join(path[:2], path[2:4], path[4:])
150
142
        return os.path.join(self._dir, path)
151
143