# Copyright 2009-2010 Joshua Roesslein
# See LICENSE for details.
+from __future__ import print_function
+
import time
import datetime
import threading
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:
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):
# 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)
continue
self._delete_file(os.path.join(self.cache_dir, entry))
+
class MemCacheCache(Cache):
"""Cache interface"""
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)
"""Delete all cached entries. NO-OP"""
raise NotImplementedError
+
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
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()
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)
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):
- '''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):
- '''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)
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)