From 2e7638e28818ac6d14a0dbe1e4041678ff36923c Mon Sep 17 00:00:00 2001 From: Wladston Viana Ferreira Filho Date: Thu, 15 Nov 2012 02:07:30 -0200 Subject: [PATCH] MongoDB cache backend added. --- tweepy/cache.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tweepy/cache.py b/tweepy/cache.py index 9a31f58..25564a3 100644 --- a/tweepy/cache.py +++ b/tweepy/cache.py @@ -3,6 +3,7 @@ # See LICENSE for details. import time +import datetime import threading import os @@ -381,3 +382,43 @@ class RedisCache(Cache): keys = self.client.smembers(self.keys_container) for key in keys: self.delete_entry(key) + + +class MongodbCache(Cache): + """A simple pickle-based MongoDB cache sytem.""" + + def __init__(self, db, timeout=3600, collection='tweepy_cache'): + """Should receive a "database" cursor from pymongo.""" + Cache.__init__(self, timeout) + self.timeout = timeout + self.col = db[collection] + self.col.create_index('created', expireAfterSeconds=timeout) + + def store(self, key, value): + from bson.binary import Binary + + now = datetime.datetime.utcnow() + blob = Binary(pickle.dumps(value)) + + self.col.insert({'created': now, '_id': key, 'value': blob}) + + def get(self, key, timeout=None): + if timeout: + raise NotImplementedError + obj = self.col.find_one({'_id': key}) + if obj: + return pickle.loads(obj['value']) + + def count(self): + return self.col.find({}).count() + + def delete_entry(self, key): + return self.col.remove({'_id': key}) + + def cleanup(self): + """MongoDB will automatically clear expired keys.""" + pass + + def flush(self): + self.col.drop() + self.col.create_index('created', expireAfterSeconds=self.timeout) -- 2.25.1