author | Sverre Rabbelier <srabbelier@gmail.com> |
Fri, 20 Mar 2009 21:02:21 +0000 | |
changeset 1958 | 8e8f45dd46e3 |
parent 1795 | 336c35c82b87 |
child 2070 | 9555be8634a3 |
permissions | -rw-r--r-- |
1795 | 1 |
#!/usr/bin/python2.5 |
2 |
# |
|
3 |
# Copyright 2009 the Melange authors. |
|
4 |
# |
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 |
# you may not use this file except in compliance with the License. |
|
7 |
# You may obtain a copy of the License at |
|
8 |
# |
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
|
10 |
# |
|
11 |
# Unless required by applicable law or agreed to in writing, software |
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 |
# See the License for the specific language governing permissions and |
|
15 |
# limitations under the License. |
|
16 |
||
17 |
"""Module contains logic memcaching functions. |
|
18 |
""" |
|
19 |
||
20 |
__authors__ = [ |
|
21 |
'"Sverre Rabbelier" <sverre@rabbelier.nl>', |
|
22 |
] |
|
23 |
||
24 |
||
25 |
from google.appengine.api import memcache |
|
1958
8e8f45dd46e3
Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1795
diff
changeset
|
26 |
from google.appengine.ext import db |
1795 | 27 |
|
28 |
import soc.cache.base |
|
29 |
||
30 |
||
1958
8e8f45dd46e3
Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1795
diff
changeset
|
31 |
def key(model, filter, order): |
1795 | 32 |
"""Returns the memcache key for this query. |
33 |
""" |
|
34 |
||
1958
8e8f45dd46e3
Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1795
diff
changeset
|
35 |
new_filter = {} |
8e8f45dd46e3
Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1795
diff
changeset
|
36 |
|
8e8f45dd46e3
Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1795
diff
changeset
|
37 |
for key, value in filter.iteritems(): |
8e8f45dd46e3
Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1795
diff
changeset
|
38 |
new_value = value.key().name() if isinstance(value, db.Model) else value |
8e8f45dd46e3
Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1795
diff
changeset
|
39 |
new_filter[key] = new_value |
8e8f45dd46e3
Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1795
diff
changeset
|
40 |
|
8e8f45dd46e3
Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1795
diff
changeset
|
41 |
return 'query_for_%(kind)s_%(filter)s_%(order)s' % { |
8e8f45dd46e3
Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1795
diff
changeset
|
42 |
'kind': repr(model.kind()), |
8e8f45dd46e3
Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1795
diff
changeset
|
43 |
'filter': repr(new_filter), |
8e8f45dd46e3
Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1795
diff
changeset
|
44 |
'order': repr(order), |
8e8f45dd46e3
Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1795
diff
changeset
|
45 |
} |
1795 | 46 |
|
47 |
||
1958
8e8f45dd46e3
Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1795
diff
changeset
|
48 |
def get(model, filter, order, *args, **kwargs): |
1795 | 49 |
"""Retrieves the data for the specified query from the memcache. |
50 |
""" |
|
51 |
||
1958
8e8f45dd46e3
Made _getPickData more generic by supporting ordening
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1795
diff
changeset
|
52 |
memcache_key = key(model, filter, order) |
1795 | 53 |
return memcache.get(memcache_key), memcache_key |
54 |
||
55 |
||
56 |
def put(data, memcache_key, *args, **kwargs): |
|
57 |
"""Sets the data for the specified query in the memcache. |
|
58 |
||
59 |
Args: |
|
60 |
data: the data to be cached |
|
61 |
""" |
|
62 |
||
63 |
# Store data for fifteen minutes to force a refresh every so often |
|
64 |
retention = 15*60 |
|
65 |
memcache.add(memcache_key, data, retention) |
|
66 |
||
67 |
||
68 |
def flush(model, filter): |
|
69 |
"""Removes the data for the current user from the memcache. |
|
70 |
""" |
|
71 |
||
72 |
memcache_key = key(model, filter) |
|
73 |
memcache.delete(memcache_key) |
|
74 |
||
75 |
||
76 |
# define the cache function |
|
77 |
cache = soc.cache.base.getCacher(get, put) |