source: qesdi/wms_ddc_vis/trunk/lib/wms_ddc_vis/lib/modified_beaker_cache_decorator.py @ 5849

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/qesdi/wms_ddc_vis/trunk/lib/wms_ddc_vis/lib/modified_beaker_cache_decorator.py@5849
Revision 5849, 2.8 KB checked in by pnorton, 10 years ago (diff)

Added some code to try and pass the axis config xml to the UI.

Also included a modified version of the beaker cache decorator that I've been using to cache the coastline map.

Line 
1'''
2Created on 28 Sep 2009
3
4@author: pnorton
5
6This is a modified version of the beaker_cache decorator found in pylons.decorators.cache,
7I've customised it to only use the GET parameters to generate the key and to round
8the BBOX arguments.
9'''
10
11import inspect
12import logging
13
14from decorator import decorator
15from paste.deploy.converters import asbool
16
17import pylons
18
19log = logging.getLogger(__name__)
20
21def beaker_cache(type='dbm', expire='never'):
22   
23    def wrapper(func, *args, **kwargs):
24        """Decorator wrapper"""
25#        log.debug("Wrapped with key: %s, expire: %s, type: %s, query_args: %s",
26#                  key, expire, type, query_args)
27        enabled = pylons.config.get("cache_enabled", "True")
28        if not asbool(enabled):
29            log.debug("Caching disabled, skipping cache lookup")
30            return func(*args, **kwargs)
31       
32        my_cache = pylons.cache.get_cache('%s.%s' % (func.__module__, func.__name__), type=type)
33        cache_key = _make_key()
34       
35        if expire == "never":
36            cache_expire = None
37        else:
38            cache_expire = expire
39       
40        def create_func():
41            log.debug("generating !")
42            result = func(*args, **kwargs)
43           
44            glob_response = pylons.response._current_obj()
45           
46            full_response = dict(headers=glob_response.headers,
47                                 status=glob_response.status_code,
48                                 cookies=glob_response.cookies,
49                                 r_content=glob_response.content,
50                                 content=result)
51           
52            return full_response
53       
54        response = my_cache.get_value(cache_key, createfunc=create_func, 
55                                      expiretime=cache_expire,)
56
57        log.debug("Cached response with key: %s" % (cache_key,))
58           
59        glob_response = pylons.response._current_obj()
60        glob_response.headers = response['headers']
61        glob_response.status_code = response['status']
62        #glob_response.cookies = response['cookies']
63        glob_response.content = response['r_content']
64        return response['content']
65   
66    return decorator(wrapper)
67
68def _make_key():
69    """Helps make unique key from largs, kwargs and request.GET"""
70         
71    dic = {}
72       
73    for k,v in pylons.request.GET.items():
74        if k.lower() == 'bbox':
75            try:
76                floatValues = tuple([float(x) for x in v.split(',') ])
77                fmtString = "%.5f,"*4
78                fmtString = fmtString[:-1]
79                newValues = fmtString % floatValues
80                dic[k] = newValues
81            except Exception, e:
82                log.info("Exception occurred trying to re-format %s=%s: %s" % (k,v,e) )
83            break;
84        else:
85            dic[k] = str(v)
86
87    cache_key = repr(dic)
88    return cache_key
Note: See TracBrowser for help on using the repository browser.