From f2976a0e440506cde399074e9275234954e26339 Mon Sep 17 00:00:00 2001 From: Josh Roesslein Date: Thu, 15 Oct 2009 17:50:28 -0500 Subject: [PATCH] Port cache system to python 2.4 --- tweepy/cache.py | 54 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/tweepy/cache.py b/tweepy/cache.py index 622b2a9..7740014 100644 --- a/tweepy/cache.py +++ b/tweepy/cache.py @@ -2,14 +2,17 @@ # 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: @@ -78,11 +81,13 @@ class MemoryCache(Cache): 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: @@ -91,29 +96,36 @@ class MemoryCache(Cache): # 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): @@ -186,7 +198,8 @@ 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') @@ -197,6 +210,8 @@ class FileCache(Cache): # 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) @@ -205,7 +220,8 @@ class FileCache(Cache): 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') @@ -215,8 +231,9 @@ class FileCache(Cache): 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) @@ -224,6 +241,8 @@ class FileCache(Cache): # unlock and return result self._unlock_file(f_lock) return value + finally: + self.lock.release() def count(self): c = 0 @@ -263,8 +282,9 @@ class MemCache(Cache): 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 -- 2.25.1