Skip to content

Instantly share code, notes, and snippets.

@aseering
Created November 24, 2012 02:48
Show Gist options
  • Save aseering/4138163 to your computer and use it in GitHub Desktop.
Save aseering/4138163 to your computer and use it in GitHub Desktop.
Django spymemcached Jython cache backend.
"""
A memcached library for Jython. Wraps spymemcached.jar.
Very lightly tested; use at your own risk!
Depends on spymemcached (<http://code.google.com/p/spymemcached/>).
No fancy features, but it shouldn't be hard to extend.
"""
from java.net import InetSocketAddress
from net.spy.memcached import MemcachedClient, BinaryConnectionFactory
class SpyMemcachedClient(object):
def __init__(self, host, port):
addr = InetSocketAddress(host,port)
self.client = MemcachedClient(BinaryConnectionFactory(), [addr])
def add(self, key, value, timeout=0):
return self.client.add(key, timeout, value)
def get(self, key, default=None):
ret = self.client.get(key)
return ret if ret is not None else default
def set(self, key, value, timeout=0):
return bool(self.client.set(key, timeout, value))
def delete(self, key):
return bool(self.client.delete(key))
def get_many(self, keys):
return self.client.getBulk(keys)
def incr(self, key, delta=1):
return bool(self.client.incr(key, delta))
def decr(self, key, delta=1):
return bool(self.client.decr(key, delta))
def close(self, **kwargs):
self.client.flush()
self.client.shutdown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment