Commit a2afc3cb authored by blackbrrr's avatar blackbrrr

initial import

parent 15057958
*.py[c|o]
.DS_Store
*.sql
*.bz2
*~
*.log
*.json
*.wsgi
local_settings.py
development_settings.py
\ No newline at end of file
Sean Bleier <http://github.com/sebleier>
Matt Dennewitz <http://github.com/blackbrrr>
Forked from http://github.com/sebleier/django-redis-cache
\ No newline at end of file
This is a simple Redis cache backend for django. It is fast, but compared to memcached, it performs a little more slowly. However, the cache is persistent so if that is what you need more than speed, this is the way to go.
Note: Redis writes to disk asynchronously so there is a slight chance of losing some data, but for most purposes this is acceptable.
I included the python client library that comes with Redis:
http://github.com/antirez/redis/tree/master
Installation:
1. Put redis.py and client.py somewhere on you python path
2. In your django settings file put: CACHE_BACKEND = 'path.to.redis://<host>:<port>'
==========================
Redis Django Cache Backend
==========================
A simple Redis cache backend for Django.
Notes
-----
The Python wrapper required by this library is distributed with Redis,
and can be found in your local installation at ::
[redis dir]/client-libraries/python/redis.py
or at `Redis's GitHub Repo`_.
Redis writes to disk asynchronously so there is a slight chance
of losing some data, but for most purposes this is acceptable.
Usage
-----
1. Place ``redis_cache.py`` on your python path.
2. Modify your Django settings to use ``redis_cache`` ::
CACHE_BACKEND = 'path.to.redis_cache://<host>:<port>'
.. _Redis's Github Repo: http://github.com/antirez/redis/tree/master/client-libraries/python/
\ No newline at end of file
This diff is collapsed.
"Redis cache backend"
import client
try:
import cPickle as pickle
except ImportError:
import pickle
from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError
from django.utils.encoding import smart_unicode, smart_str
class CacheClass(BaseCache):
def __init__(self, server, params):
BaseCache.__init__(self, params)
self._cache = redis_client.Redis(server.split(':')[0], db=1)
def add(self, key, value, timeout=0):
if isinstance(value, unicode):
value = value.encode('utf-8')
success = self._cache.set(smart_str(key), pickle.dumps(value)) == "OK"
return success and timeout and self._cache.expire(smart_str(key), timeout or self.default_timeout) == 1
def get(self, key, default=None):
val = self._cache.get(smart_str(key))
if val is None:
return default
else:
val = pickle.loads(val)
if isinstance(val, basestring):
return smart_unicode(val)
else:
return val
def set(self, key, value, timeout=0):
return self.add(key, value, timeout)
def delete(self, key):
self._cache.delete(smart_str(key))
def get_many(self, keys):
return self._cache.mget(map(smart_str, keys))
def close(self, **kwargs):
self._cache.disconnect()
def incr(self, key, delta=1):
return self._cache.incr(key, delta)
def decr(self, key, delta=1):
return self._cache.decr(key, delta)
try:
import cPickle as pickle
except ImportError:
import pickle
import redis
from django.core.cache.backends.base import BaseCache
from django.utils.encoding import smart_unicode, smart_str
class CacheClass(BaseCache):
def __init__(self, server, params):
"Connect to Redis, and set up cache backend."
BaseCache.__init__(self, params)
self._cache = redis.Redis(server.split(':')[0], db=1)
def _prepare_key(self, raw_key):
"``smart_str``-encode the key."
return smart_str(raw_key)
def _prepare_value(self, value):
if isinstance(value, unicode):
value = value.encode('utf-8')
return value
def add(self, key, value, timeout=0):
"""Add a value to the cache, failing if the key already exists.
Returns ``True`` if the object was added, ``False`` if not.
"""
_key = self._prepare_key(key)
_value = self._prepare_key(value)
if self._cache.exists(_key):
return False
return self.set(key, value, timeout)
def get(self, key, default=None):
"""Retrieve a value from the cache.
Returns unpicked value if key is found, ``None`` if not.
"""
key = self._prepare_key(key)
value = self._cache.get(key)
if value is None:
return default
else:
value = pickle.loads(value)
if isinstance(val, basestring):
return smart_unicode(value)
else:
return value
def set(self, key, value, timeout=0):
"Persist a value to the cache, and set an optional expiration time."
key = self._prepare_key(key)
value = self._prepare_value(value)
# store the key/value pair
result = self._cache.set(key, value)
# set content expiration, if necessary
if timeout > 0:
self._cache.expire(key, timeout)
if result == "OK":
return True
else:
return False
def delete(self, key):
"Remove a key from the cache."
key = self._prepare_key(key)
self._cache.delete(key)
def get_many(self, keys):
"Retrieve many keys."
return self._cache.mget(map(self._prepare_key, keys))
def incr(self, key, delta=1):
"Atomically increment ``key`` by ``delta``."
key = self._prepare_key(key)
return self._cache.incr(key, delta)
def decr(self, key, delta=1):
"Atomically decrement ``key`` by ``delta``."
key = self._prepare_key(key)
return self._cache.decr(key, delta)
def close(self, **kwargs):
"Disconnect from the cache."
self._cache.disconnect()
#!/usr/bin/env python
from distutils.core import setup
version = "0.1"
classifiers = [
"Programming Language :: Python",
"Operating System :: OS Independent",
"Topic :: Software Development :: Libraries",
"Topic :: Utilities",
"Environment :: Web Environment",
"Framework :: Django",
]
setup(
name = "django-redis-cache",
version = version,
url = "http://github.com/blackbrrr/django-redis-cache/",
author = "Matt Dennewitz",
author_email = "mattdennewitz@gmail.com",
packages = ["redis_cache"],
description = "Redis Cache Backend for Django",
classifiers = classifiers
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment