projects
/
twitter-api-cdsw
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Merge pull request #3 from guyrt/master
[twitter-api-cdsw]
/
tweepy
/
cache.py
diff --git
a/tweepy/cache.py
b/tweepy/cache.py
index a50a349891e9a2f5f7a63f45039183131e109c0f..1d6cb562f945abed175fbffd352d627bd8664df2 100644
(file)
--- a/
tweepy/cache.py
+++ b/
tweepy/cache.py
@@
-2,6
+2,8
@@
# Copyright 2009-2010 Joshua Roesslein
# See LICENSE for details.
# Copyright 2009-2010 Joshua Roesslein
# See LICENSE for details.
+from __future__ import print_function
+
import time
import datetime
import threading
import time
import datetime
import threading
@@
-119,7
+121,7
@@
class MemoryCache(Cache):
def cleanup(self):
self.lock.acquire()
try:
def cleanup(self):
self.lock.acquire()
try:
- for k, v in
self._entries
.items():
+ for k, v in
dict(self._entries)
.items():
if self._is_expired(v, self.timeout):
del self._entries[k]
finally:
if self._is_expired(v, self.timeout):
del self._entries[k]
finally:
@@
-161,7
+163,7
@@
class FileCache(Cache):
def _get_path(self, key):
md5 = hashlib.md5()
def _get_path(self, key):
md5 = hashlib.md5()
- md5.update(key)
+ md5.update(key
.encode('utf-8')
)
return os.path.join(self.cache_dir, md5.hexdigest())
def _lock_file_dummy(self, path, exclusive=True):
return os.path.join(self.cache_dir, md5.hexdigest())
def _lock_file_dummy(self, path, exclusive=True):
@@
-236,10
+238,11
@@
class FileCache(Cache):
# check if value is expired
if timeout is None:
timeout = self.timeout
# check if value is expired
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)
+ if timeout > 0:
+ if (time.time() - created_time) >= timeout:
+ # expired! delete from cache
+ value = None
+ self._delete_file(path)
# unlock and return result
self._unlock_file(f_lock)
# unlock and return result
self._unlock_file(f_lock)
@@
-267,6
+270,7
@@
class FileCache(Cache):
continue
self._delete_file(os.path.join(self.cache_dir, entry))
continue
self._delete_file(os.path.join(self.cache_dir, entry))
+
class MemCacheCache(Cache):
"""Cache interface"""
class MemCacheCache(Cache):
"""Cache interface"""
@@
-288,7
+292,8
@@
class MemCacheCache(Cache):
def get(self, key, timeout=None):
"""Get cached entry if exists and not expired
key: which entry to get
def get(self, key, timeout=None):
"""Get cached entry if exists and not expired
key: which entry to get
- timeout: override timeout with this value [optional]. DOES NOT WORK HERE
+ timeout: override timeout with this value [optional].
+ DOES NOT WORK HERE
"""
return self.client.get(key)
"""
return self.client.get(key)
@@
-304,10
+309,14
@@
class MemCacheCache(Cache):
"""Delete all cached entries. NO-OP"""
raise NotImplementedError
"""Delete all cached entries. NO-OP"""
raise NotImplementedError
+
class RedisCache(Cache):
class RedisCache(Cache):
- '''Cache running in a redis server'''
+ """Cache running in a redis server"""
- def __init__(self, client, timeout=60, keys_container = 'tweepy:keys', pre_identifier = 'tweepy:'):
+ def __init__(self, client,
+ timeout=60,
+ keys_container='tweepy:keys',
+ pre_identifier='tweepy:'):
Cache.__init__(self, timeout)
self.client = client
self.keys_container = keys_container
Cache.__init__(self, timeout)
self.client = client
self.keys_container = keys_container
@@
-318,8
+327,9
@@
class RedisCache(Cache):
return timeout > 0 and (time.time() - entry[0]) >= timeout
def store(self, key, value):
return timeout > 0 and (time.time() - entry[0]) >= timeout
def store(self, key, value):
- '''Store the key, value pair in our redis server'''
- # Prepend tweepy to our key, this makes it easier to identify tweepy keys in our redis server
+ """Store the key, value pair in our redis server"""
+ # Prepend tweepy to our key,
+ # this makes it easier to identify tweepy keys in our redis server
key = self.pre_identifier + key
# Get a pipe (to execute several redis commands in one step)
pipe = self.client.pipeline()
key = self.pre_identifier + key
# Get a pipe (to execute several redis commands in one step)
pipe = self.client.pipeline()
@@
-333,7
+343,7
@@
class RedisCache(Cache):
pipe.execute()
def get(self, key, timeout=None):
pipe.execute()
def get(self, key, timeout=None):
- '''Given a key, returns an element from the redis table'''
+ """Given a key, returns an element from the redis table"""
key = self.pre_identifier + key
# Check to see if we have this key
unpickled_entry = self.client.get(key)
key = self.pre_identifier + key
# Check to see if we have this key
unpickled_entry = self.client.get(key)
@@
-356,19
+366,20
@@
class RedisCache(Cache):
return entry[1]
def count(self):
return entry[1]
def count(self):
- '''Note: This is not very efficient, since it retreives all the keys from the redis
- server to know how many keys we have'''
+ """Note: This is not very efficient,
+ since it retreives all the keys from the redis
+ server to know how many keys we have"""
return len(self.client.smembers(self.keys_container))
def delete_entry(self, key):
return len(self.client.smembers(self.keys_container))
def delete_entry(self, key):
- '''Delete an object from the redis table'''
+ """Delete an object from the redis table"""
pipe = self.client.pipeline()
pipe.srem(self.keys_container, key)
pipe.delete(key)
pipe.execute()
def cleanup(self):
pipe = self.client.pipeline()
pipe.srem(self.keys_container, key)
pipe.delete(key)
pipe.execute()
def cleanup(self):
- '''Cleanup all the expired keys'''
+ """Cleanup all the expired keys"""
keys = self.client.smembers(self.keys_container)
for key in keys:
entry = self.client.get(key)
keys = self.client.smembers(self.keys_container)
for key in keys:
entry = self.client.get(key)
@@
-378,7
+389,7
@@
class RedisCache(Cache):
self.delete_entry(key)
def flush(self):
self.delete_entry(key)
def flush(self):
- '''Delete all entries from the cache'''
+ """Delete all entries from the cache"""
keys = self.client.smembers(self.keys_container)
for key in keys:
self.delete_entry(key)
keys = self.client.smembers(self.keys_container)
for key in keys:
self.delete_entry(key)
Benjamin Mako Hill
||
Want to submit a patch?