Skip to content

Commit

Permalink
Moved storage out of the main class and into a more extendable struct…
Browse files Browse the repository at this point in the history
…ure allowing other backends to be used.
  • Loading branch information
JakeWharton committed Jan 7, 2009
1 parent 8bd746a commit 99a9eeb
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 49 deletions.
49 changes: 0 additions & 49 deletions pycache.py

This file was deleted.

39 changes: 39 additions & 0 deletions pycache/pycache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from time import time

class PyCache:
TABLE = 'PyCache'
KEY = 'PyCache_key'
VALUE = 'PyCache_value'
EXPIRES = 'PyCache_expires'

def __init__(self, **kwargs):
if 'engine' in kwargs:
self.storage = kwargs['engine'](**kwargs)

#One hour default expiration
self.default_length = 3600

def create(self):
self.storage.create()

def gc(self):
self.storage.gc()

def store(self, key, value, expires):
self.storage.store(key, value, expires)

def expire(self, key):
self.storage.expire(key)

def get(self, key):
result = self.storage.fetch(key)
if not result or result[PyCache.EXPIRES] < time():
return False
return result[PyCache.VALUE]

def __setitem__(self, key, value):
self.store(key, value, time() + self.default_length)
def __getitem__(self, key):
return self.get(key)
def __delitem__(self, key):
return self.storage.expire(key)
1 change: 1 addition & 0 deletions pycache/storage/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__all__ = ['storage', 'sqlite']
38 changes: 38 additions & 0 deletions pycache/storage/sqlite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import sqlite3
from pycache import PyCache
from storage import storage
from time import time

class SQLite(storage):
def __init__(self, **kwargs):
if 'file' in kwargs:
self.file = kwargs['file']
else:
self.file = 'PyCache.db'
self.conn = sqlite3.connect(self.file)
self.curs = self.conn.cursor()

if 'table' in kwargs:
self.table = kwargs['table'] + '_' + PyCache.TABLE
else:
self.table = PyCache.TABLE

def create(self):
self.curs.execute("CREATE TABLE %s (%s VARCHAR(32) PRIMARY KEY, %s TEXT, %s INTEGER)" % (self.table, PyCache.KEY, PyCache.VALUE, PyCache.EXPIRES))
self.curs.execute("CREATE INDEX %s_%s ON %s (%s)" % (self.table, PyCache.EXPIRES, self.table, PyCache.EXPIRES))

def store(self, key, value, expires):
self.curs.execute("REPLACE INTO %s (%s, %s, %s) VALUES (?, ?, ?)" % (self.table, PyCache.KEY, PyCache.VALUE, PyCache.EXPIRES), (key, value, expires))

def fetch(self, key):
self.curs.execute("SELECT %s, %s FROM %s WHERE %s=?" % (PyCache.VALUE, PyCache.EXPIRES, self.table, PyCache.KEY), (key,))
result = self.curs.fetchone()
if not result:
return False
return {PyCache.VALUE: result[0], PyCache.EXPIRES: int(result[1])}

def expire(self, key):
self.curs.execute("REPLACE INTO %s (%s, %s) VALUES (?, ?)" % (self.table, PyCache.KEY, PyCache.EXPIRES), (key, time()))

def gc(self):
self.curs.execute("DELETE FROM %s WHERE %s < ?" % (self.table, PyCache.EXPIRES), (time(),))
6 changes: 6 additions & 0 deletions pycache/storage/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class storage:
def create(self): pass
def store(self, key, value, expires): pass
def fetch(self, key): pass
def expire(self, key): pass
def gc(self): pass

0 comments on commit 99a9eeb

Please sign in to comment.