-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved storage out of the main class and into a more extendable struct…
…ure allowing other backends to be used.
- Loading branch information
1 parent
8bd746a
commit 99a9eeb
Showing
5 changed files
with
84 additions
and
49 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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
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) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__all__ = ['storage', 'sqlite'] |
This file contains 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
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(),)) |
This file contains 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
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 |