示例#1
0
def configure_cache(app):
    if app.config['CACHE_TYPE'] == 'memcached':
        app.cache = cache.MemcachedCache(
            app.config['CACHE_MEMCACHED_SERVERS'],
            key_prefix=app.config.get('CACHE_KEY_PREFIX') or '',
        )

    elif app.config['CACHE_TYPE'] == 'redis':
        kwargs = {
            'host': app.config['CACHE_REDIS_HOST'],
            'port': app.config['CACHE_REDIS_PORT'],
        }

        if app.config['CACHE_REDIS_PASSWORD']:
            kwargs['password'] = app.config['CACHE_REDIS_PASSWORD']

        if app.config['CACHE_REDIS_DB'] is not None:
            kwargs['db'] = app.config['CACHE_REDIS_DB']

        if app.config['CACHE_KEY_PREFIX']:
            kwargs['key_prefix'] = app.config['CACHE_KEY_PREFIX']

        app.cache = cache.RedisCache(**kwargs)

    elif app.config['CACHE_TYPE'] == 'filesystem':
        app.cache = cache.FileSystemCache(app.config['CACHE_DIR'])

    elif app.config['CACHE_TYPE'] == 'null':
        app.cache = cache.NullCache()

    else:
        raise ValueError('Unknown cache type: {}'.format(
            app.config['CACHE_TYPE']))
示例#2
0
        def make_cache(self, xprocess, request):
            def preparefunc(cwd):
                return 'server is now ready', ['redis-server']

            xprocess.ensure('redis_server', preparefunc)
            args, kwargs = request.param
            c = cache.RedisCache(*args, key_prefix='werkzeug-test-case:',
                                 **kwargs)
            request.addfinalizer(c.clear)
            return lambda: c
示例#3
0
    def make_cache(self, request):
        if request.param is None:
            host = "localhost"
        elif request.param:
            host = redis.StrictRedis()
        else:
            host = redis.Redis()

        c = cache.RedisCache(host=host, key_prefix="werkzeug-test-case:")
        yield lambda: c
        c.clear()
示例#4
0
 def make_cache(self):
     return cache.RedisCache(key_prefix='werkzeug-test-case:')
示例#5
0
    :license: BSD, see LICENSE for more details.
"""
import os
import time
import unittest
import tempfile
import shutil

from werkzeug.testsuite import WerkzeugTestCase
from werkzeug.contrib import cache

try:
    import redis
    try:
        from redis.exceptions import ConnectionError as RedisConnectionError
        cache.RedisCache(key_prefix='werkzeug-test-case:')._client.set('test','connection')
    except RedisConnectionError:
        redis = None
except ImportError:
    redis = None


class SimpleCacheTestCase(WerkzeugTestCase):

    def test_get_dict(self):
        c = cache.SimpleCache()
        c.set('a', 'a')
        c.set('b', 'b')
        d = c.get_dict('a', 'b')
        assert 'a' in d
        assert 'a' == d['a']
示例#6
0
 def test_empty_host(self):
     with pytest.raises(ValueError) as exc_info:
         cache.RedisCache(host=None)
     assert text_type(exc_info.value) == 'RedisCache host parameter may not be None'
示例#7
0
    None: __cache_module.NullCache,
    'memcached': __cache_module.MemcachedCache,
    'redis': __cache_module.RedisCache
}

__cache_uri = os.environ.get('CACHE_SERVICE')

if __cache_uri:
    try:
        # example __cache_uri is 'redis:dev_redis_1:6379'
        [__cache_type, __url, __port] = __cache_uri.split(':')
    except ValueError as e:
        raise ImproperlyConfigured(
            'CACHE_SERVICE is wrongly formatted. Use "redis:dev_redis_1:6379" as example.'
        )
    if __cache_type == 'redis':
        cache = __cache_module.RedisCache(
            host=__url,
            port=__port,
            default_timeout=os.environ.get('CACHE_TIMEOUT'))
    elif __cache_type == 'memcached':
        cache = __cache_module.MemcachedCache(
            servers=["{url}:{port}".format(url=__url, port=__port)],
            default_timeout=os.environ.get('CACHE_TIMEOUT'))
    else:
        raise ImproperlyConfigured(
            'Unknown cache service, only Memcached and Redis are supported at the moment.'
        )
else:
    cache = __cache_module.NullCache
示例#8
0
import re
import time
import redis
import common
import config
import inspect
import logging
from functools import wraps

from werkzeug.contrib import cache

client = redis.Redis(connection_pool=common.cache_pool)

backend = cache.RedisCache(
        host=client, \
        default_timeout=config.CACHE_DEFAULT_TIMEOUT, \
        key_prefix=config.CACHE_PREFIX)

ONE_HOUR = 3600
ONE_DAY = 86400
ONE_WEEK = 604800

logger = logging.getLogger(__name__)

old_pattern = re.compile(r'%\w')
new_pattern = re.compile(r'\{(\w+(\.\w+|\[\w+\])?)\}')

__formaters = {}


def gen_key(key_pattern, arg_names, defaults, *a, **kw):