# Copyright 2009 Joshua Roesslein
# See LICENSE
-from __future__ import with_statement # need this for py2.5
-
import time
import threading
import os
-import hashlib
import cPickle as pickle
+try:
+ import hashlib
+except ImportError:
+ # python 2.4
+ import md5 as hashlib
+
try:
import fcntl
except ImportError:
return timeout > 0 and (time.time() - entry[0]) >= timeout
def store(self, key, value):
- with self.lock:
- self._entries[key] = (time.time(), value)
+ self.lock.acquire()
+ self._entries[key] = (time.time(), value)
+ self.lock.release()
def get(self, key, timeout=None):
- with self.lock:
+ self.lock.acquire()
+ try:
# check to see if we have this key
entry = self._entries.get(key)
if not entry:
# use provided timeout in arguments if provided
# otherwise use the one provided during init.
- _timeout = self.timeout if timeout is None else timeout
+ if timeout is None:
+ timeout = self.timeout
# make sure entry is not expired
- if self._is_expired(entry, _timeout):
+ if self._is_expired(entry, timeout):
# entry expired, delete and return nothing
del self._entries[key]
return None
# entry found and not expired, return it
return entry[1]
+ finally:
+ self.lock.release()
def count(self):
return len(self._entries)
def cleanup(self):
- with self.lock:
+ self.lock.acquire()
+ try:
for k, v in self._entries.items():
if self._is_expired(v, self.timeout):
del self._entries[k]
+ finally:
+ self.lock.release()
def flush(self):
- with self.lock:
- self._entries.clear()
+ self.lock.acquire()
+ self._entries.clear()
+ self.lock.release()
class FileCache(Cache):
def store(self, key, value):
path = self._get_path(key)
- with self.lock:
+ self.lock.acquire()
+ try:
# acquire lock and open file
f_lock = self._lock_file(path)
datafile = open(path, 'wb')
# close and unlock file
datafile.close()
self._unlock_file(f_lock)
+ finally:
+ self.lock.release()
def get(self, key, timeout=None):
return self._get(self._get_path(key), timeout)
if os.path.exists(path) is False:
# no record
return None
- while self.lock:
+ self.lock.acquire()
+ try:
# acquire lock and open
f_lock = self._lock_file(path, False)
datafile = open(path, 'rb')
datafile.close()
# check if value is expired
- _timeout = self.timeout if timeout is None else timeout
- if _timeout > 0 and (time.time() - created_time) >= _timeout:
+ if timeout is None:
+ timeout = self.timeout
+ if timeout > 0 and (time.time() - created_time) >= timeout:
# expired! delete from cache
value = None
self._delete_file(path)
# unlock and return result
self._unlock_file(f_lock)
return value
+ finally:
+ self.lock.release()
def count(self):
c = 0
created_time, value = obj
# check if value is expired
- _timeout = self.timeout if timeout is None else timeout
- if _timeout > 0 and (time.time() - created_time) >= _timeout:
+ if timeout is None:
+ timeout = self.timeout
+ if timeout > 0 and (time.time() - created_time) >= timeout:
# expired! delete from cache
self.client.delete(key)
return None