Commit 15057958 authored by Sean Bleier's avatar Sean Bleier

Intial commit

parents
Copyright (c) 2009 Sean Bleier
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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>'
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)
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