示例#1
0
 def __init__(self, servers=None, default_timeout=300, key_prefix=None):
     BaseCache.__init__(self, default_timeout=default_timeout)
     CachelibMemcachedCache.__init__(
         self,
         servers=servers,
         default_timeout=default_timeout,
         key_prefix=key_prefix,
     )
示例#2
0
    def __init__(self):
        if 'MEMCACHED_HOSTS' in os.environ:
            memcahed_hosts = os.environ['MEMCACHED_HOSTS']
            client_hosts = []
            memcahed_hosts = dict(
                item.split(":") for item in memcahed_hosts.split(","))

            for key, value in memcahed_hosts.items():
                client_hosts.append((key, int(value)))
            self.hosts = client_hosts
            self.client = MemcachedCache(self.hosts)
示例#3
0
class Memcached(metaclass=Singleton):
    r"""
    Memcached

    Environments variables:


    * MEMCACHED_HOSTS
        Memcached's hosts

        can be multiple hosts separated by ","

        for example: "10.0.0.1:11211,10,0.0.2:11212"

    Examples usage:

    os.environ['MEMCACHED_HOSTS'] = "127.0.0.1:11211"

    memcached = Memcached()

    value = memcached("some_key")
    """
    client = None
    hosts = None

    def __init__(self):
        if 'MEMCACHED_HOSTS' in os.environ:
            memcahed_hosts = os.environ['MEMCACHED_HOSTS']
            client_hosts = []
            memcahed_hosts = dict(
                item.split(":") for item in memcahed_hosts.split(","))

            for key, value in memcahed_hosts.items():
                client_hosts.append((key, int(value)))
            self.hosts = client_hosts
            self.client = MemcachedCache(self.hosts)

    def get_set(self, key, value, expire=3600):
        if not key or not value:
            return None
        prev_value = self.get(key)
        self.set(key, value, expire=expire)
        return prev_value

    def get(self, key):
        if not key or len(key) > 250:
            return None
        return self.client.get(key)

    def set(self, key, value, expire=3600):
        if len(key) > 250:
            return False
        self.client.set(key, value, timeout=expire)
        return True
示例#4
0
 def _memcache(self, **kwargs):
     """Returns a :class:`MemcachedCache` instance"""
     kwargs.update(dict(
         servers=self._config('MEMCACHED_SERVERS', None),
         key_prefix=self._config('key_prefix', None),
     ))
     return MemcachedCache(**kwargs)
示例#5
0
    def setup_cache(self):
        """
        Setup the cache from configuration.
        If cache_path is set in the config,
        it can be set to file:///some/cache/path
        or memcached://127.0.0.1:port for memcached

        Otherwise, try to use storage.get_cache_path()
        as a cache directory. If cachelib can't be imported,
        no cache will be available.
        """
        cache = None
        cache_url = self.config.get('cache_path')
        if cache_url:
            # cache_url could be memcached://127.0....
            # or file:///some/path or /some/path
            split_url = cache_url.split('://', 1)
            if len(split_url) == 1:
                split_url = ('file://', split_url)
            proto, path = split_url
            if proto == 'memcached':
                from cachelib import MemcachedCache
                from hashlib import sha1
                cache = MemcachedCache(
                    path.split(','),
                    key_prefix=sha1(
                        self.site_id.encode('utf8')).hexdigest()[:8])
            elif proto == 'file':
                from cachelib import FileSystemCache
                cache = FileSystemCache(os.path.abspath(path))
        else:
            try:
                from cachelib import FileSystemCache
            except ImportError:
                pass
            else:
                cache = FileSystemCache(
                    os.path.abspath(self.storage.get_cache_path()))
        return cache
示例#6
0
        port=get_env(
            "SUPERSET_REDIS_RESULTS_BACKEND_PORT", default=6379, cast=int),
        password=get_env("SUPERSET_REDIS_RESULTS_BACKEND_PASSWORD"),
        key_prefix=get_env("SUPERSET_REDIS_RESULTS_BACKEND_KEY_PREFIX",
                           default="superset_results"),
        db=get_env("SUPERSET_REDIS_RESULTS_BACKEND_DB", default=0, cast=int),
        default_timeout=get_env(
            "SUPERSET_REDIS_RESULTS_BACKEND_DEFAULT_TIMEOUT",
            default=300,
            cast=float)),
    "memcached":
    lambda: MemcachedCache(
        servers=get_env("SUPERSET_MEMCACHED_RESULTS_BACKEND_SERVERS",
                        default=[],
                        cast=list),
        default_timeout=get_env(
            "SUPERSET_MEMCACHED_RESULTS_BACKEND_DEFAULT_TIMEOUT",
            default=300,
            cast=float),
        key_prefix=get_env("SUPERSET_MEMCACHED_RESULTS_BACKEND_KEY_PREFIX",
                           default="superset_results")),
    "s3":
    lambda: S3Cache(
        s3_bucket=get_env("SUPERSET_S3_RESULTS_BACKEND_BUCKET_NAME"),
        key_prefix=get_env("SUPERSET_S3_RESULTS_BACKEND_KEY_PREFIX",
                           default="superset_results"))
}

CELERY_RESULT_BACKENDS_URIS = {
    "redis":
    "redis://{password}{host}:{port}/{db}".format(
        password="******".format(get_env("CELERY_REDIS_RESULT_BACKEND_PASSWORD"))
示例#7
0
def memcached(app, config, args, kwargs):
    args.append(config['CACHE_MEMCACHED_SERVERS'])
    kwargs.update(dict(key_prefix=config['CACHE_KEY_PREFIX']))
    return MemcachedCache(*args, **kwargs)
示例#8
0
# (c) 2010 Martin Wendt; see CloudDAV http://clouddav.googlecode.com/
# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
#
# The original source for this module was taken from gaedav:
# (c) 2009 Haoyu Bai (http://gaedav.google.com/).
"""
Implement cache mechanism.
"""
import logging
import threading
from builtins import object

from cachelib import MemcachedCache, RedisCache, SimpleCache

try:
    memcache3 = MemcachedCache()
    # memcache3 = RedisCache()
except Exception as e:
    logging.info(e)
    memcache3 = SimpleCache()

memcache3._stats = {
    "byte_hits": 0,
    "bytes": 0,
    "hits": 0,
    "items": 0,
    "misses": 0,
    "oldest_item_age": 0,
    # keep track of operations other than get (= hits + misses)
    "set": 0,
    "set_multi": 0,
示例#9
0
 def _factory(self, *args, **kwargs):
     mc = MemcachedCache(*args, **kwargs)
     mc._client.flush_all()
     return mc
示例#10
0
# pylint: disable=ungrouped-imports

app = Flask(__name__)

if "WEBFAF_ENVIRON_PRODUCTION" in os.environ:
    app.config.from_object("webfaf.config.ProductionConfig")
elif "WEBFAF_ENVIRON_TEST" in os.environ:
    app.config.from_object("webfaf.config.TestingConfig")
else:
    app.config.from_object("webfaf.config.DevelopmentConfig")

db = SQLAlchemy(app)

if app.config["CACHE_TYPE"].lower() == "memcached":
    flask_cache = MemcachedCache(['{0}:{1}'.format(
        app.config["MEMCACHED_HOST"],
        app.config["MEMCACHED_PORT"])],
                                 key_prefix=app.config["MEMCACHED_KEY_PREFIX"])
elif app.config["CACHE_TYPE"].lower() == "simple":
    flask_cache = SimpleCache()
else:
    flask_cache = NullCache()

if app.config["PROXY_SETUP"]:
    app.wsgi_app = ProxyFix(app.wsgi_app)

if app.config["OPENID_ENABLED"]:
    from flask_openid import OpenID
    from openid_teams import teams
    oid = OpenID(app, safe_roots=[], extension_responses=[teams.TeamsResponse])
    from webfaf.login import login # pylint: disable=cyclic-import
    app.register_blueprint(login)
示例#11
0
def create_cache_list(request, tmpdir):
    mc = MemcachedCache()
    mc._client.flush_all()
    rc = RedisCache(port=6360)
    rc._client.flushdb()
    request.cls.cache_list = [FileSystemCache(tmpdir), mc, rc, SimpleCache()]
示例#12
0
    <script src="https://code.jquery.com/jquery.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="static/js/bootstrap.js"></script>

    <!-- Syntax Highlighting -->
    <script src="static/js/rainbow.js"></script>
    <script src="static/js/language/generic.js"></script>
    <script src="static/js/language/c.js"></script>
    <script src="static/js/language/lispy.js"></script>
  </div>
  </body>
</html>
"""

try:
    cache = MemcachedCache(['127.0.0.1:11211'])
except RuntimeError:

    class FakeCache:
        def get(self, k):
            return None

        def set(self, k, v, **kwargs):
            return None

    cache = FakeCache()

app = Flask(__name__)

handler = logging.FileHandler(
    os.path.join(os.path.split(__file__)[0], 'error.log'))