Created
May 28, 2009 10:57
-
-
Save mgax/119226 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# TinyCache | |
# simple object cache based on Durus (http://www.mems-exchange.org/software/durus/) | |
# provides multiple named cache pools; no expiration of entries | |
import logging | |
from cStringIO import StringIO | |
durus_log_file = StringIO() | |
durus_logger = logging.getLogger('durus') | |
durus_logger.addHandler(logging.StreamHandler(durus_log_file)) | |
durus_logger.setLevel(logging.WARN) | |
cache_log = logging.getLogger('art17.scrape.cache') | |
from durus.file_storage import FileStorage | |
from durus.connection import Connection | |
from durus.persistent import Persistent | |
from durus.persistent_dict import PersistentDict | |
def open_db(cache_fname): | |
db_connection = Connection(FileStorage(cache_fname)) | |
db_root = db_connection.get_root() | |
return (db_connection, db_root) | |
db_connection, db_root = open_db('/TufaStuff/proiecte/edw-art17/data/cache.db') | |
class Cache(): | |
def __init__(self, data): | |
self.data = data | |
def put(self, key, data): | |
cache_log.debug('saving "%s"' % key) | |
self.data[key] = data | |
db_connection.commit() | |
def get(self, key): | |
if key in self.data: | |
cache_log.debug('cache hit "%s"' % key) | |
return self.data[key] | |
else: | |
cache_log.debug('cache miss "%s"' % key) | |
return None | |
def get_cache(name): | |
if name not in db_root: | |
db_root[name] = PersistentDict() | |
db_connection.commit() | |
return Cache(db_root[name]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment