author | Sverre Rabbelier <srabbelier@gmail.com> |
Sun, 15 Feb 2009 16:11:23 +0000 | |
changeset 1347 | 88dac688b673 |
parent 1307 | 091a21cf3627 |
child 1418 | 50e989482d1b |
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 |
""" |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
38 |
|
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): |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
41 |
result = get(*args, **kwargs) |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
42 |
if result: |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
43 |
return result |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
44 |
|
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
45 |
result = func(*args, **kwargs) |
1017
6ad4fdb48840
Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
987
diff
changeset
|
46 |
put(result, *args, **kwargs) |
987
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
47 |
return result |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
48 |
|
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
49 |
return wrapper |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
50 |
|
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
51 |
return cache |