Mercurial > kallithea
comparison rhodecode/model/caching_query.py @ 609:c1c1cf772337
moved out sqlalchemy cache from meta to the config files.
added caching query for permissions.
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Tue, 12 Oct 2010 16:39:53 +0200 |
parents | 1e757ac98988 |
children | b0a411f5ec70 |
comparison
equal
deleted
inserted
replaced
608:101e07f82f22 | 609:c1c1cf772337 |
---|---|
16 | 16 |
17 The rest of what's here are standard SQLAlchemy and | 17 The rest of what's here are standard SQLAlchemy and |
18 Beaker constructs. | 18 Beaker constructs. |
19 | 19 |
20 """ | 20 """ |
21 from beaker.exceptions import BeakerException | |
21 from sqlalchemy.orm.interfaces import MapperOption | 22 from sqlalchemy.orm.interfaces import MapperOption |
22 from sqlalchemy.orm.query import Query | 23 from sqlalchemy.orm.query import Query |
23 from sqlalchemy.sql import visitors | 24 from sqlalchemy.sql import visitors |
25 import beaker | |
24 | 26 |
25 class CachingQuery(Query): | 27 class CachingQuery(Query): |
26 """A Query subclass which optionally loads full results from a Beaker | 28 """A Query subclass which optionally loads full results from a Beaker |
27 cache region. | 29 cache region. |
28 | 30 |
103 | 105 |
104 def query_callable(manager): | 106 def query_callable(manager): |
105 def query(*arg, **kw): | 107 def query(*arg, **kw): |
106 return CachingQuery(manager, *arg, **kw) | 108 return CachingQuery(manager, *arg, **kw) |
107 return query | 109 return query |
108 | 110 |
111 def get_cache_region(name, region): | |
112 if region not in beaker.cache.cache_regions: | |
113 raise BeakerException('Cache region not configured: %s' % region) | |
114 kw = beaker.cache.cache_regions[region] | |
115 return beaker.cache.Cache._get_cache(name, kw) | |
116 | |
109 def _get_cache_parameters(query): | 117 def _get_cache_parameters(query): |
110 """For a query with cache_region and cache_namespace configured, | 118 """For a query with cache_region and cache_namespace configured, |
111 return the correspoinding Cache instance and cache key, based | 119 return the correspoinding Cache instance and cache key, based |
112 on this query's current criterion and parameter values. | 120 on this query's current criterion and parameter values. |
113 | 121 |
123 # cache key - the value arguments from this query's parameters. | 131 # cache key - the value arguments from this query's parameters. |
124 args = _params_from_query(query) | 132 args = _params_from_query(query) |
125 cache_key = " ".join([str(x) for x in args]) | 133 cache_key = " ".join([str(x) for x in args]) |
126 | 134 |
127 # get cache | 135 # get cache |
128 cache = query.cache_manager.get_cache_region(namespace, region) | 136 #cache = query.cache_manager.get_cache_region(namespace, region) |
129 | 137 cache = get_cache_region(namespace, region) |
130 # optional - hash the cache_key too for consistent length | 138 # optional - hash the cache_key too for consistent length |
131 # import uuid | 139 # import uuid |
132 # cache_key= str(uuid.uuid5(uuid.NAMESPACE_DNS, cache_key)) | 140 # cache_key= str(uuid.uuid5(uuid.NAMESPACE_DNS, cache_key)) |
133 | 141 |
134 return cache, cache_key | 142 return cache, cache_key |