-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·33 lines (29 loc) · 1.02 KB
/
utils.py
File metadata and controls
executable file
·33 lines (29 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This is SELFICIOUS by Yuuta
# UPDATED: 2010-12-22 22:28:40
import logging
from google.appengine.api import memcache
def keygen(format, *args, **kwargs):
"""generates a key from args and kwargs using format"""
allargs = args+tuple(kwargs[key] for key in sorted(kwargs.keys()))
key = format % allargs[0:format.count('%')]
return key
def memoize(keyformat, time=600, cache_null=False):
"""Decorator to memoize functions using memcache."""
def decorator(fxn):
def wrapper(self, *args, **kwargs):
key = keygen(keyformat, *args, **kwargs)
data = memcache.get(key)
if data is not None:
logging.info('From memcache: %s' % key)
return data
data = fxn(self, *args, **kwargs)
if data or cache_null:
memcache.set(key, data, time)
return data
return wrapper
return decorator
def unmemoize(keys_list):
memcache.delete_multi(keys_list)