Skip to content

Instantly share code, notes, and snippets.

@rdeaton
Created January 14, 2012 18:30
Show Gist options
  • Save rdeaton/1612405 to your computer and use it in GitHub Desktop.
Save rdeaton/1612405 to your computer and use it in GitHub Desktop.
Spyral SmartMemoize fix
def __call__(self, *args):
from spyral import director
frame = director.get_tick()
if director.get_scene() is not self.scene:
self.scene = director.get_scene()
self.cache = {}
if frame - self.last_clear > 100:
self.cache = dict((key,value) for key,value in self.cache.items() if frame - value[1] > 250)
self.last_clear = frame
try:
data, oldframe = self.cache[args]
self.cache[args] = (data, frame)
return data
except KeyError:
res = self.func(*args)
self.cache[args] = (res, frame)
return res
except TypeError:
print "WARNING: Unhashable type passed to memoize2. Reconsider using this decorator"
return self.func(*args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment