author | James Levy <jamesalexanderlevy@gmail.com> |
Sun, 28 Jun 2009 14:18:01 +0200 | |
changeset 2431 | 800a020c9bcf |
parent 2070 | 9555be8634a3 |
permissions | -rw-r--r-- |
987
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/python2.5 |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
2 |
# |
1307
091a21cf3627
Update the copyright notice for 2009.
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1029
diff
changeset
|
3 |
# Copyright 2009 the Melange authors. |
987
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
4 |
# |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
6 |
# you may not use this file except in compliance with the License. |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
7 |
# You may obtain a copy of the License at |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
8 |
# |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
10 |
# |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
11 |
# Unless required by applicable law or agreed to in writing, software |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
14 |
# See the License for the specific language governing permissions and |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
15 |
# limitations under the License. |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
16 |
|
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
17 |
"""Module containing some basic caching functions. |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
18 |
""" |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
19 |
|
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
20 |
__authors__ = [ |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
21 |
'"Sverre Rabbelier" <sverre@rabbelier.nl>', |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
22 |
] |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
23 |
|
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
24 |
|
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
25 |
from functools import wraps |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
26 |
|
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
27 |
|
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
28 |
def getCacher(get, put): |
1029
a312e7007811
Style fixes in soc.cache modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1017
diff
changeset
|
29 |
"""Returns a caching decorator that uses get and put. |
987
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
30 |
""" |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
31 |
|
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
32 |
# TODO(SRabbelier) possibly accept 'key' instead, and define |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
33 |
# get and put in terms of key, depends on further usage |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
34 |
|
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
35 |
def cache(func): |
1029
a312e7007811
Style fixes in soc.cache modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1017
diff
changeset
|
36 |
"""Decorator that caches the result from func. |
987
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
37 |
""" |
1829
c9f30de8804f
Fixed whitespace in cache/base.py
Lennard de Rijk <ljvderijk@gmail.com>
parents:
1418
diff
changeset
|
38 |
|
987
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
39 |
@wraps(func) |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
40 |
def wrapper(*args, **kwargs): |
2070
9555be8634a3
Remove unused imports soc.cache.base module and rename key variable to filter_key in soc.views.sitemap modules (access to outer scope names).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1829
diff
changeset
|
41 |
"""Decorator wrapper method. |
9555be8634a3
Remove unused imports soc.cache.base module and rename key variable to filter_key in soc.views.sitemap modules (access to outer scope names).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1829
diff
changeset
|
42 |
""" |
1418
50e989482d1b
Compute the memcache_key only in get()
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1307
diff
changeset
|
43 |
result, key = get(*args, **kwargs) |
987
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
44 |
if result: |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
45 |
return result |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
46 |
|
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
47 |
result = func(*args, **kwargs) |
1418
50e989482d1b
Compute the memcache_key only in get()
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1307
diff
changeset
|
48 |
|
50e989482d1b
Compute the memcache_key only in get()
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1307
diff
changeset
|
49 |
if key: |
50e989482d1b
Compute the memcache_key only in get()
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1307
diff
changeset
|
50 |
put(result, key, *args, **kwargs) |
50e989482d1b
Compute the memcache_key only in get()
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1307
diff
changeset
|
51 |
|
987
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
52 |
return result |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
53 |
|
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
54 |
return wrapper |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
55 |
|
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
56 |
return cache |