Lightweight, framework agnostic caching library with sweet API
NB! API is expected to change!
from datetime import datetime, timedelta
import sweetcache
import sweetcache_redis
cache = sweetcache.Cache(sweetcache_redis.RedisBackend)
cache.set("foo", 42)
assert cache.get("foo") == 42
user = {
"id": 1,
"username": "daGrevis",
}
cache.set(["users.v1", user["id"]], user)
cache.set("foo", 42, expires=timedelta(minutes=5))
cache.set("foo", 42, expires=datetime(2015, 9, 28))
cache.set("foo", 42, expires=60 * 5)
try:
foo = cache.get("foo")
except sweetcache.NotFoundError:
foo = None
foo = cache.get("foo", None)
@cache.it("charts.v2", expires=timedelta(hours=2))
def get_charts():
charts = calculate_charts()
return charts
nosetests -s sweetcache
nosetests -s sweetcache --with-coverage --cover-html --cover-package sweetcache