Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added memoizing functionality

  • Loading branch information...
commit 55ef78a91f39d3b19ee9b6b75b0545845e5691b9 1 parent f255159
@bmuller authored
Showing with 65 additions and 5 deletions.
  1. +16 −4 README.markdown
  2. +1 −1  setup.py
  3. +48 −0 txyam/utils.py
View
20 README.markdown
@@ -10,9 +10,7 @@ This project is specifically designed for asynchronous [Python Twisted](http://t
## Installation
- git clone https://github.com/bmuller/txyam
- cd txyam
- sudo python setup.py install
+ pip install txyam
## Usage
@@ -37,5 +35,19 @@ This project is specifically designed for asynchronous [Python Twisted](http://t
print host, statlist['bytes']
client.stats().addCallback(printStats)
+## Memoizing
+You can use txyam to memoize functions/methods.
+
+ # assuming "client" is already defined and is a YamClient
+ @memoize(client)
+ def mayTakeAWhile(arg, argtwo):
+ return takesForever(arg, argtwo)
+
+ mayTakeAWhile('blah', 'blah two')
+
+After the first time 'mayTakeAWhile' is called, the results are stored in memcache. All future
+calls just pull the results from memcache. The function will be memoized based on the function
+name and arguments. The function being memoized can return an object, which will be picked before saving.
+
## Errors / Bugs / Contact
-See [github](http://github.com/bmuller/txyam).
+See [github](http://github.com/bmuller/txyam).
View
2  setup.py
@@ -3,7 +3,7 @@
setup(
name="txyam",
- version="0.3",
+ version="0.4",
description="Yet Another Memcached (YAM) client for Twisted.",
author="Brian Muller",
author_email="bamuller@gmail.com",
View
48 txyam/utils.py
@@ -42,3 +42,51 @@ def handle(results, names):
dl = defer.DeferredList(d.values())
return dl.addCallback(handle, d.keys())
+
+
+class Memoizer:
+ """
+ Class to handle memoizing functions. Not meant to be instantiated
+ by any code other than the C{memoize} function.
+ """
+ def __init__(self, client):
+ self.client = client
+
+
+ def memoize(self, func):
+ self.func = func
+ return self.caller
+
+
+ def caller(self, *args, **kwargs):
+ self.key = hashlib.sha1(repr(self.client) + repr(args) + repr(kwargs)).hexdigest()
+ d = self.client.getPickled(self.key)
+ return d.addCallback(self.handleResult, args, kwargs)
+
+
+ def saveResult(self, result):
+ d = self.client.setPickled(self.key, result)
+ return d.addCallback(lambda _: result)
+
+
+ def handleResult(self, result, args, kwargs):
+ if result[1] is None:
+ d = defer.maybeDeferred(self.func, *args, **kwargs)
+ return d.addCallback(self.saveResult)
+ return defer.succeed(result[1])
+
+
+def memoize(client):
+ """
+ Memoize a function. Used like this:
+
+ @memoize(yamclient)
+ def rememberable(one, two):
+ return takesForever(one, two)
+
+ Where C{yamclient} is an instance of C{txyam.YamClient} in the
+ decorator. The function will be memoized based on the function
+ name and arguments. The function being memoized can return an
+ object, which will be picked before saving.
+ """
+ return Memoizer(client).memoize
Please sign in to comment.
Something went wrong with that request. Please try again.