File manager - Edit - /opt/imh-python/lib/python2.7/site-packages/werkzeug/contrib/cache.pyo
Back
� �ْSc @ s[ d Z d d l Z d d l Z d d l Z d d l m Z d d l m Z y d d l Z Wn e k ry d d l Z n Xd d l m Z m Z m Z m Z m Z d d l m Z d � Z d e f d � � YZ d e f d � � YZ d e f d � � YZ e j d � j Z d e f d � � YZ e Z d e f d � � YZ d e f d � � YZ d S( s| werkzeug.contrib.cache ~~~~~~~~~~~~~~~~~~~~~~ The main problem with dynamic Web sites is, well, they're dynamic. Each time a user requests a page, the webserver executes a lot of code, queries the database, renders templates until the visitor gets the page he sees. This is a lot more expensive than just loading a file from the file system and sending it to the visitor. For most Web applications, this overhead isn't a big deal but once it becomes, you will be glad to have a cache system in place. How Caching Works ================= Caching is pretty simple. Basically you have a cache object lurking around somewhere that is connected to a remote cache or the file system or something else. When the request comes in you check if the current page is already in the cache and if so, you're returning it from the cache. Otherwise you generate the page and put it into the cache. (Or a fragment of the page, you don't have to cache the full thing) Here is a simple example of how to cache a sidebar for a template:: def get_sidebar(user): identifier = 'sidebar_for/user%d' % user.id value = cache.get(identifier) if value is not None: return value value = generate_sidebar_for(user=user) cache.set(identifier, value, timeout=60 * 5) return value Creating a Cache Object ======================= To create a cache object you just import the cache system of your choice from the cache module and instantiate it. Then you can start working with that object: >>> from werkzeug.contrib.cache import SimpleCache >>> c = SimpleCache() >>> c.set("foo", "value") >>> c.get("foo") 'value' >>> c.get("missing") is None True Please keep in mind that you have to create the cache and put it somewhere you have access to it (either as a module global you can import or you just put it into your WSGI application). :copyright: (c) 2014 by the Werkzeug Team, see AUTHORS for more details. :license: BSD, see LICENSE for more details. i����N( t md5( t time( t iteritemst string_typest text_typet integer_typest to_bytes( t renamec C s6 t | d � r | j � St | d � r2 | j � S| S( s Wrapper for efficient iteration over mappings represented by dicts or sequences:: >>> for k, v in _items((i, i*i) for i in xrange(5)): ... assert k*k == v >>> for k, v in _items(dict((i, i*i) for i in xrange(5))): ... assert k*k == v R t items( t hasattrR R ( t mappingorseq( ( sE /opt/imh-python/lib/python2.7/site-packages/werkzeug/contrib/cache.pyt _itemsJ s t BaseCachec B s� e Z d Z d d � Z d � Z d � Z d � Z d � Z d d � Z d d � Z d d � Z d � Z d � Z d d � Z d d � Z RS( s� Baseclass for the cache systems. All the cache systems implement this API or a superset of it. :param default_timeout: the default timeout that is used if no timeout is specified on :meth:`set`. i, c C s | | _ d S( N( t default_timeout( t selfR ( ( sE /opt/imh-python/lib/python2.7/site-packages/werkzeug/contrib/cache.pyt __init__d s c C s d S( s� Looks up key in the cache and returns the value for it. If the key does not exist `None` is returned instead. :param key: the key to be looked up. N( t None( R t key( ( sE /opt/imh-python/lib/python2.7/site-packages/werkzeug/contrib/cache.pyt getg s c C s d S( s� Deletes `key` from the cache. If it does not exist in the cache nothing happens. :param key: the key to delete. N( ( R R ( ( sE /opt/imh-python/lib/python2.7/site-packages/werkzeug/contrib/cache.pyt deleteo s c G s t | j | � S( sf Returns a list of values for the given keys. For each key a item in the list is created. Example:: foo, bar = cache.get_many("foo", "bar") If a key can't be looked up `None` is returned for that key instead. :param keys: The function accepts multiple keys as positional arguments. ( t mapR ( R t keys( ( sE /opt/imh-python/lib/python2.7/site-packages/werkzeug/contrib/cache.pyt get_manyw s c G s t t | | j | � � � S( s Works like :meth:`get_many` but returns a dict:: d = cache.get_dict("foo", "bar") foo = d["foo"] bar = d["bar"] :param keys: The function accepts multiple keys as positional arguments. ( t dictt zipR ( R R ( ( sE /opt/imh-python/lib/python2.7/site-packages/werkzeug/contrib/cache.pyt get_dict� s c C s d S( s9 Adds a new key/value to the cache (overwrites value, if key already exists in the cache). :param key: the key to set :param value: the value for the key :param timeout: the cache timeout for the key (if not specified, it uses the default timeout). N( ( R R t valuet timeout( ( sE /opt/imh-python/lib/python2.7/site-packages/werkzeug/contrib/cache.pyt set� s c C s d S( s* Works like :meth:`set` but does not overwrite the values of already existing keys. :param key: the key to set :param value: the value for the key :param timeout: the cache timeout for the key or the default timeout if not specified. N( ( R R R R ( ( sE /opt/imh-python/lib/python2.7/site-packages/werkzeug/contrib/cache.pyt add� s c C s4 x- t | � D] \ } } | j | | | � q Wd S( s� Sets multiple keys and values from a mapping. :param mapping: a mapping with the keys/values to set. :param timeout: the cache timeout for the key (if not specified, it uses the default timeout). N( R R ( R t mappingR R R ( ( sE /opt/imh-python/lib/python2.7/site-packages/werkzeug/contrib/cache.pyt set_many� s c G s"