author | Lennard de Rijk <ljvderijk@gmail.com> |
Sun, 19 Apr 2009 22:05:25 +0000 | |
changeset 2251 | 2d286b2419d1 |
parent 2160 | 3f9dd37d98a8 |
child 2365 | a66e1dd8ced7 |
permissions | -rw-r--r-- |
1419
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/python2.5 |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
2 |
# |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
3 |
# Copyright 2009 the Melange authors. |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
4 |
# |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
6 |
# you may not use this file except in compliance with the License. |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
7 |
# You may obtain a copy of the License at |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
8 |
# |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
10 |
# |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
11 |
# Unless required by applicable law or agreed to in writing, software |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
14 |
# See the License for the specific language governing permissions and |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
15 |
# limitations under the License. |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
16 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
17 |
"""Module contains sidebar memcaching functions. |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
18 |
""" |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
19 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
20 |
__authors__ = [ |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
21 |
'"Sverre Rabbelier" <sverre@rabbelier.nl>', |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
22 |
] |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
23 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
24 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
25 |
import logging |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
26 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
27 |
from google.appengine.api import memcache |
1600
0aa3de1b2acc
Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1419
diff
changeset
|
28 |
|
0aa3de1b2acc
Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1419
diff
changeset
|
29 |
from soc.logic import accounts |
1419
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
30 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
31 |
import soc.cache.base |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
32 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
33 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
34 |
def key(entity): |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
35 |
"""Returns the memcache key for the user's sidebar. |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
36 |
""" |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
37 |
|
2160
3f9dd37d98a8
Use key().id_or_name() instead of key().name()
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1600
diff
changeset
|
38 |
return 'homepage_for_%s_%s' % (entity.kind(), entity.key().id_or_name()) |
1419
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
39 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
40 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
41 |
def get(self, *args, **kwargs): |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
42 |
"""Retrieves the sidebar for the specified user from the memcache. |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
43 |
""" |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
44 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
45 |
# only cache the page for non-logged-in users |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
46 |
# TODO: figure out how to cache everything but the sidebar |
1600
0aa3de1b2acc
Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1419
diff
changeset
|
47 |
# also, no need to normalize as we don't use it anyway |
0aa3de1b2acc
Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1419
diff
changeset
|
48 |
if accounts.getCurrentAccount(normalize=False): |
1419
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
49 |
return (None, None) |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
50 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
51 |
entity = self._logic.getFromKeyFields(kwargs) |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
52 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
53 |
# if we can't retrieve the entity, leave it to the actual method |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
54 |
if not entity: |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
55 |
return (None, None) |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
56 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
57 |
memcache_key = key(entity) |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
58 |
logging.info("Retrieving %s" % memcache_key) |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
59 |
return memcache.get(memcache_key), memcache_key |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
60 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
61 |
def put(result, memcache_key, *args, **kwargs): |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
62 |
"""Sets the sidebar for the specified user in the memcache. |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
63 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
64 |
Args: |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
65 |
sidebar: the sidebar to be cached |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
66 |
""" |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
67 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
68 |
# no sense in storing anything if we won't query it later on |
1600
0aa3de1b2acc
Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1419
diff
changeset
|
69 |
# also, no need to normalize as we don't use it anyway |
0aa3de1b2acc
Fix access bug due to forgotten normalization
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1419
diff
changeset
|
70 |
if accounts.getCurrentAccount(normalize=False): |
1419
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
71 |
return |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
72 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
73 |
# Store sidebar for just ten minutes to force a refresh every so often |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
74 |
retention = 10*60 |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
75 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
76 |
logging.info("Setting %s" % memcache_key) |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
77 |
memcache.add(memcache_key, result, retention) |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
78 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
79 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
80 |
def flush(entity): |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
81 |
"""Removes the sidebar for the current user from the memcache. |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
82 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
83 |
Also calls soc.cache.rights.flush for the specified user. |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
84 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
85 |
Args: |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
86 |
id: defaults to the current account if not set |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
87 |
""" |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
88 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
89 |
memcache_key = key(entity) |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
90 |
logging.info("Flushing %s" % memcache_key) |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
91 |
memcache.delete(memcache_key) |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
92 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
93 |
|
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
94 |
# define the cache function |
5d0f80ad7b9f
Add caching to the homepage
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff
changeset
|
95 |
cache = soc.cache.base.getCacher(get, put) |