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:
1033
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 contains sidebar memcaching 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 google.appengine.api import memcache |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
26 |
from google.appengine.api import users |
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 |
import soc.cache.base |
1033
71ff49dbb4b6
Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1029
diff
changeset
|
29 |
import soc.cache.rights |
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 |
|
1017
6ad4fdb48840
Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
987
diff
changeset
|
32 |
def key(id): |
1029
a312e7007811
Style fixes in soc.cache modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1020
diff
changeset
|
33 |
"""Returns the memcache key for the user's sidebar. |
987
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 |
|
1017
6ad4fdb48840
Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
987
diff
changeset
|
36 |
return 'sidebar_for_%s' % repr(id) |
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 |
|
1017
6ad4fdb48840
Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
987
diff
changeset
|
39 |
def get(id, user): |
1029
a312e7007811
Style fixes in soc.cache modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1020
diff
changeset
|
40 |
"""Retrieves the sidebar for the specified user from the memcache. |
987
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
41 |
""" |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
42 |
|
1017
6ad4fdb48840
Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
987
diff
changeset
|
43 |
memcache_key = key(id) |
6ad4fdb48840
Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
987
diff
changeset
|
44 |
return memcache.get(memcache_key) |
987
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
45 |
|
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
46 |
|
1017
6ad4fdb48840
Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
987
diff
changeset
|
47 |
def put(sidebar, id, user): |
1029
a312e7007811
Style fixes in soc.cache modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1020
diff
changeset
|
48 |
"""Sets the sidebar for the specified user in the memcache. |
987
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
49 |
|
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
50 |
Args: |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
51 |
sidebar: the sidebar to be cached |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
52 |
""" |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
53 |
|
1017
6ad4fdb48840
Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
987
diff
changeset
|
54 |
# Store sidebar for ten minutes since new programs might get added |
1020
846fe3d821fb
Reduce caching time of sidebar to 3 minutes
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1017
diff
changeset
|
55 |
retention = 3*60 |
987
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
56 |
|
1017
6ad4fdb48840
Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
987
diff
changeset
|
57 |
memcache_key = key(id) |
6ad4fdb48840
Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
987
diff
changeset
|
58 |
memcache.add(memcache_key, sidebar, retention) |
987
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
59 |
|
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
60 |
|
1033
71ff49dbb4b6
Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1029
diff
changeset
|
61 |
def flush(id=None): |
1029
a312e7007811
Style fixes in soc.cache modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
1020
diff
changeset
|
62 |
"""Removes the sidebar for the current user from the memcache. |
987
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
63 |
|
1033
71ff49dbb4b6
Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1029
diff
changeset
|
64 |
Also calls soc.cache.rights.flush for the specified user. |
71ff49dbb4b6
Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1029
diff
changeset
|
65 |
|
987
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
66 |
Args: |
1033
71ff49dbb4b6
Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1029
diff
changeset
|
67 |
id: defaults to the current account if not set |
987
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
68 |
""" |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
69 |
|
1033
71ff49dbb4b6
Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1029
diff
changeset
|
70 |
if not id: |
71ff49dbb4b6
Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1029
diff
changeset
|
71 |
id = users.get_current_user() |
987
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
72 |
|
1033
71ff49dbb4b6
Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1029
diff
changeset
|
73 |
memcache_key = key(id) |
1017
6ad4fdb48840
Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents:
987
diff
changeset
|
74 |
memcache.delete(memcache_key) |
1033
71ff49dbb4b6
Also flush the user's ACL's when flushing the sidebar
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1029
diff
changeset
|
75 |
soc.cache.rights.flush(id) |
987
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
76 |
|
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
77 |
|
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
78 |
# define the cache function |
6fd5c561b446
Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
79 |
cache = soc.cache.base.getCacher(get, put) |