Example #1
0
 def __init__(self, session_class=None):
     super(Store, self).__init__(session_class)
     if settings.DATABASE_ENGINE == 'gae':
         self.cache = GAEMemcachedCache(default_timeout=0)
     else:
         server = settings.SESSION_OPTIONS.get('memcached_servers', [])
         self.cache = MemcachedCache(servers, default_timeout=0)
Example #2
0
# coding: UTF-8

from flask import g
from flask import redirect
from flask import url_for

from functools import wraps

from werkzeug.contrib.cache import GAEMemcachedCache
cache = GAEMemcachedCache()

def login_required(f):
    """
    redirects to the index page if the user has no session
    """
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if g.user is None:
            return redirect(url_for('index'))
        return f(*args, **kwargs)
    return decorated_function

def cache_page(timeout=5 * 60, key='view/%s'):
    """
    caches a full page in memcache, takes a timeout in seconds
    which specifies how long the cache should be valid.
    also allows a formatstring to be used as memcache key prefix.

    source:
    http://flask.pocoo.org/docs/patterns/viewdecorators/#caching-decorator
    """
Example #3
0
def gaememcached(app, config, args, kwargs):
    kwargs.update(dict(key_prefix=config['CACHE_KEY_PREFIX']))
    return GAEMemcachedCache(*args, **kwargs)
Example #4
0
    'null':
    lambda: NullCache(),
    'simple':
    lambda: SimpleCache(ctx.cfg['caching.timeout']),
    'memcached':
    lambda: MemcachedCache([
        x.strip() for x in ctx.cfg['caching.memcached_servers'].split(',')
    ], ctx.cfg['caching.timeout']),
    'filesystem':
    lambda: FileSystemCache(join(ctx.cfg['caching.filesystem_cache_path']),
                            threshold=500,
                            default_timeout=ctx.cfg['caching.timeout']),
    'database':
    lambda: DatabaseCache(ctx.cfg['caching.timeout']),
    'gaememcached':
    lambda: GAEMemcachedCache(ctx.cfg['caching.timeout'])
}


def set_cache():
    """Set and return the cache for the application.  This is called during
    the application setup.  No need to call that afterwards.
    """
    global cache
    cache = CACHE_SYSTEMS[ctx.cfg['caching.system']]()
    return cache


# enable the caching system
set_cache()