Permalink
Browse files

Add support for memcached

  • Loading branch information...
harryf committed Jun 25, 2008
1 parent 0803dc2 commit 900d2199b6547a9eaad5d3961ca8dd3101467dac
Showing with 65 additions and 1 deletion.
  1. +54 −0 dammit/mcwrapper.py
  2. +10 −0 dammit/uri.py
  3. +1 −1 view.py
View
@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+class MemcachedWrapper(object):
+ """
+ Wrapper around an memcached.Client object to implement
+ parts of a Python map used for caching in urldammit
+
+ http://www.tummy.com/Community/software/python-memcached/
+
+ >>> import memcache
+ >>> mc = memcache.Client(['127.0.0.1:11211'])
+ >>> mcw = MemcachedWrapper(mc)
+ >>> 'foo' in mcw
+ False
+ >>> mcw['foo'] = 'bar'
+ >>> 'foo' in mcw
+ True
+ >>> mcw['foo'] == 'bar'
+ True
+ >>> del mcw['foo']
+ >>> 'foo' in mcw
+ False
+ """
+ def __init__(self, mc):
+ self.mc = mc
+
+ def __len__(self):
+ raise Exception("Not implemented")
+
+ def __contains__(self, key):
+ return self.mc.get(key) != None
+
+ def __setitem__(self, key, val):
+ self.mc.set(key, val)
+
+ def __getitem__(self, key):
+ val = self.mc.get(key)
+ if val == None:
+ raise KeyError(key)
+ return val
+
+ def __delitem__(self, key):
+ if self.mc.delete(key) == 0:
+ raise KeyError(key)
+
+ def __repr__(self):
+ return "MemcachedWrapper"
+
+def _test():
+ import doctest
+ doctest.testmod()
+
+if __name__ == '__main__':
+ _test()
View
@@ -515,6 +515,16 @@ def __str__(self):
data[item[1:]] = getattr(self, item)
return str(data)
+ def __getstate__(self):
+ data = {}
+ for s in self.__slots__:
+ data[s] = getattr(self, s)
+ return data
+
+ def __setstate__(self, data):
+ for k in data:
+ setattr(self, k, data[k])
+
class GuardedURI(URI):
"""
Prevents setting status 301 or 404 or the location. Intended for use
View
@@ -2,7 +2,7 @@
import web
import config
-render = web.template.render('templates/', cache=config.cache)
+render = web.template.render('templates/', cache=config.cache_templates)
def tools(**k):
return render.tools()

0 comments on commit 900d219

Please sign in to comment.