comparison rhodecode/model/caching_query.py @ 784:30d3161c6683 beta

Implemented fancier top menu for logged and anonymous users little error fix for cached query
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 28 Nov 2010 05:26:26 +0100
parents b0a411f5ec70
children 9e93cad9357b
comparison
equal deleted inserted replaced
783:71113f64b2d8 784:30d3161c6683
53 53
54 The FromCache and RelationshipCache mapper options below represent 54 The FromCache and RelationshipCache mapper options below represent
55 the "public" method of configuring this state upon the CachingQuery. 55 the "public" method of configuring this state upon the CachingQuery.
56 56
57 """ 57 """
58 58
59 def __init__(self, manager, *args, **kw): 59 def __init__(self, manager, *args, **kw):
60 self.cache_manager = manager 60 self.cache_manager = manager
61 Query.__init__(self, *args, **kw) 61 Query.__init__(self, *args, **kw)
62 62
63 def __iter__(self): 63 def __iter__(self):
64 """override __iter__ to pull results from Beaker 64 """override __iter__ to pull results from Beaker
65 if particular attributes have been configured. 65 if particular attributes have been configured.
66 66
67 Note that this approach does *not* detach the loaded objects from 67 Note that this approach does *not* detach the loaded objects from
99 99
100 def set_value(self, value): 100 def set_value(self, value):
101 """Set the value in the cache for this query.""" 101 """Set the value in the cache for this query."""
102 102
103 cache, cache_key = _get_cache_parameters(self) 103 cache, cache_key = _get_cache_parameters(self)
104 cache.put(cache_key, value) 104 cache.put(cache_key, value)
105 105
106 def query_callable(manager): 106 def query_callable(manager):
107 def query(*arg, **kw): 107 def query(*arg, **kw):
108 return CachingQuery(manager, *arg, **kw) 108 return CachingQuery(manager, *arg, **kw)
109 return query 109 return query
110 110
111 def get_cache_region(name, region): 111 def get_cache_region(name, region):
112 if region not in beaker.cache.cache_regions: 112 if region not in beaker.cache.cache_regions:
113 raise BeakerException('Cache region not configured: %s' 113 raise BeakerException('Cache region `%s` not configured '
114 'Check if proper cache settings are in the .ini files' % region) 114 'Check if proper cache settings are in the .ini files' % region)
115 kw = beaker.cache.cache_regions[region] 115 kw = beaker.cache.cache_regions[region]
116 return beaker.cache.Cache._get_cache(name, kw) 116 return beaker.cache.Cache._get_cache(name, kw)
117 117
118 def _get_cache_parameters(query): 118 def _get_cache_parameters(query):
119 """For a query with cache_region and cache_namespace configured, 119 """For a query with cache_region and cache_namespace configured,
120 return the correspoinding Cache instance and cache key, based 120 return the correspoinding Cache instance and cache key, based
121 on this query's current criterion and parameter values. 121 on this query's current criterion and parameter values.
122 122
123 """ 123 """
124 if not hasattr(query, '_cache_parameters'): 124 if not hasattr(query, '_cache_parameters'):
125 raise ValueError("This Query does not have caching parameters configured.") 125 raise ValueError("This Query does not have caching parameters configured.")
126 126
127 region, namespace, cache_key = query._cache_parameters 127 region, namespace, cache_key = query._cache_parameters
128 128
129 namespace = _namespace_from_query(namespace, query) 129 namespace = _namespace_from_query(namespace, query)
130 130
131 if cache_key is None: 131 if cache_key is None:
132 # cache key - the value arguments from this query's parameters. 132 # cache key - the value arguments from this query's parameters.
133 args = _params_from_query(query) 133 args = _params_from_query(query)
151 namespace = namespace.replace(' ', '_') 151 namespace = namespace.replace(' ', '_')
152 152
153 return namespace 153 return namespace
154 154
155 def _set_cache_parameters(query, region, namespace, cache_key): 155 def _set_cache_parameters(query, region, namespace, cache_key):
156 156
157 if hasattr(query, '_cache_parameters'): 157 if hasattr(query, '_cache_parameters'):
158 region, namespace, cache_key = query._cache_parameters 158 region, namespace, cache_key = query._cache_parameters
159 raise ValueError("This query is already configured " 159 raise ValueError("This query is already configured "
160 "for region %r namespace %r" % 160 "for region %r namespace %r" %
161 (region, namespace) 161 (region, namespace)
162 ) 162 )
163 query._cache_parameters = region, namespace, cache_key 163 query._cache_parameters = region, namespace, cache_key
164 164
165 class FromCache(MapperOption): 165 class FromCache(MapperOption):
166 """Specifies that a Query should load results from a cache.""" 166 """Specifies that a Query should load results from a cache."""
167 167
168 propagate_to_loaders = False 168 propagate_to_loaders = False
169 169
185 185
186 """ 186 """
187 self.region = region 187 self.region = region
188 self.namespace = namespace 188 self.namespace = namespace
189 self.cache_key = cache_key 189 self.cache_key = cache_key
190 190
191 def process_query(self, query): 191 def process_query(self, query):
192 """Process a Query during normal loading operation.""" 192 """Process a Query during normal loading operation."""
193 193
194 _set_cache_parameters(query, self.region, self.namespace, self.cache_key) 194 _set_cache_parameters(query, self.region, self.namespace, self.cache_key)
195 195
196 class RelationshipCache(MapperOption): 196 class RelationshipCache(MapperOption):
197 """Specifies that a Query as called within a "lazy load" 197 """Specifies that a Query as called within a "lazy load"
198 should load results from a cache.""" 198 should load results from a cache."""
261 261
262 """ 262 """
263 v = [] 263 v = []
264 def visit_bindparam(bind): 264 def visit_bindparam(bind):
265 value = query._params.get(bind.key, bind.value) 265 value = query._params.get(bind.key, bind.value)
266 266
267 # lazyloader may dig a callable in here, intended 267 # lazyloader may dig a callable in here, intended
268 # to late-evaluate params after autoflush is called. 268 # to late-evaluate params after autoflush is called.
269 # convert to a scalar value. 269 # convert to a scalar value.
270 if callable(value): 270 if callable(value):
271 value = value() 271 value = value()
272 272
273 v.append(value) 273 v.append(value)
274 if query._criterion is not None: 274 if query._criterion is not None:
275 visitors.traverse(query._criterion, {}, {'bindparam':visit_bindparam}) 275 visitors.traverse(query._criterion, {}, {'bindparam':visit_bindparam})
276 return v 276 return v