Improve file cache locks.
authorJosh Roesslein <jroesslein@gmail.com>
Sun, 9 Aug 2009 18:56:17 +0000 (13:56 -0500)
committerJosh Roesslein <jroesslein@gmail.com>
Sun, 9 Aug 2009 18:56:17 +0000 (13:56 -0500)
tweepy/cache.py

index 3cb91bb3399889803e403c5833278de7981a59cc..98e821ad056302b3a052675da8ed9d80d37174b3 100644 (file)
@@ -9,6 +9,8 @@ import hashlib
 import fcntl
 import cPickle as pickle
 
+from error import TweepError
+
 """Cache interface"""
 class Cache(object):
 
@@ -99,12 +101,19 @@ class MemoryCache(Cache):
 """File-based cache"""
 class FileCache(Cache):
 
+  # locks used to make cache thread-safe
+  cache_locks = {}
+
   def __init__(self, cache_dir, timeout=60):
     Cache.__init__(self, timeout)
     if os.path.exists(cache_dir) is False:
       os.mkdir(cache_dir)
     self.cache_dir = cache_dir
-    self.lock = threading.Lock()
+    if cache_dir in FileCache.cache_locks:
+      self.lock = FileCache.cache_locks[cache_dir]
+    else:
+      self.lock = threading.Lock()
+      FileCache.cache_locks[cache_dir] = self.lock
 
   def _get_path(self, key):
     md5 = hashlib.md5()