Ejemplo n.º 1
0
 def __init__(self,
              *,
              namespace="dramatiq-results",
              pool=None,
              pool_size=8,
              **parameters):
     self.namespace = namespace
     self.pool = pool or ClientPool(Client(**parameters), pool_size)
Ejemplo n.º 2
0
 def __init__(self,
              *,
              namespace="dramatiq-results",
              encoder=None,
              pool=None,
              pool_size=8,
              **parameters):
     super().__init__(namespace=namespace, encoder=encoder)
     self.pool = pool or ClientPool(Client(**parameters), pool_size)
Ejemplo n.º 3
0
def getmc():
    """
    @return: Memcached ClientPool
    """
    try:
        from pylibmc import Client, ClientPool
        from config import cacheServers, MC_POOL_SIZE
        adv_setting = {"cas": True, "tcp_nodelay": True, "ketama": True}
        mc = Client(cacheServers, binary=True, behaviors=adv_setting)
        return ClientPool(mc, MC_POOL_SIZE)
    except ImportError:
        return ClientPoolEmptyOnWIndows()
Ejemplo n.º 4
0
    def __init__(self, *, pool=None, pool_size=8, **parameters):
        behaviors = parameters.setdefault("behaviors", {})
        behaviors["cas"] = True

        self.pool = pool or ClientPool(Client(**parameters), pool_size)
Ejemplo n.º 5
0
import HTMLParser
import helpers

app = Flask(__name__)

if os.path.exists(app.config.root_path + '/config.cfg') is False:
    print "copy config.default.cfg to config.cfg and add your settings"
    app.config.from_pyfile(app.config.root_path + '/config.default.cfg')
else:
    app.config.from_pyfile(app.config.root_path + '/config.cfg')

babel = Babel(app)
Compress(app)

client = Client(app.config['MEMCACHED'])
mc_pool = ClientPool(client, app.config['POOL_SIZE'])


def cache_name(pagename, lang=''):
    if not lang:
        lang = 'sv' if 'sv' in request.url_rule.rule else 'en'
    return '%s_%s' % (pagename, lang)


def check_cache(page, lang=''):
    # If the cache should not be used, return None
    if app.config['TEST']:
        return None
    try:
        with mc_pool.reserve() as client:
            # Look for the page, return if found
Ejemplo n.º 6
0
class MemcachedBackend(RateLimiterBackend):
    """A rate limiter backend for Memcached_.

    Examples:

      >>> from dramatiq.rate_limits.backends import MemcachedBackend
      >>> backend = MemcachedBackend(servers=["127.0.0.1"], binary=True)

    Parameters:
      pool_size(int): The size of the connection pool to use.
      \**parameters(dict): Connection parameters are passed directly
        to :class:`pylibmc.Client`.

    .. _memcached: https://memcached.org
    """
    def __init__(self, *, pool_size=8, **parameters):
        behaviors = parameters["behaviors"] = parameters.get("behaviors", {})
        behaviors["cas"] = True

        self.client = client = Client(**parameters)
        self.pool = ClientPool(client, pool_size)

    def add(self, key, value, ttl):
        with self.pool.reserve(block=True) as client:
            return client.add(key, value, time=int(ttl / 1000))

    def incr(self, key, amount, maximum, ttl):
        ttl = int(ttl / 1000)
        with self.pool.reserve(block=True) as client:
            while True:
                value, cid = client.gets(key)
                if cid is None:
                    return False

                value += amount
                if value > maximum:
                    return False

                try:
                    swapped = client.cas(key, value, cid, ttl)
                    if swapped:
                        return True
                except NotFound:  # pragma: no cover
                    continue

    def decr(self, key, amount, minimum, ttl):
        ttl = int(ttl / 1000)
        with self.pool.reserve(block=True) as client:
            while True:
                value, cid = client.gets(key)
                if cid is None:
                    return False

                value -= amount
                if value < minimum:
                    return False

                try:
                    swapped = client.cas(key, value, cid, ttl)
                    if swapped:
                        return True
                except NotFound:  # pragma: no cover
                    continue

    def incr_and_sum(self, key, keys, amount, maximum, ttl):
        ttl = int(ttl / 1000)
        with self.pool.reserve(block=True) as client:
            while True:
                value, cid = client.gets(key)
                if cid is None:
                    return False

                value += amount
                if value > maximum:
                    return False

                mapping = client.get_multi(keys)
                total = amount + sum(mapping.values())
                if total > maximum:
                    return False

                try:
                    swapped = client.cas(key, value, cid, ttl)
                    if swapped:
                        return True
                except NotFound:  # pragma: no cover
                    continue
Ejemplo n.º 7
0
    def __init__(self, *, pool_size=8, **parameters):
        behaviors = parameters["behaviors"] = parameters.get("behaviors", {})
        behaviors["cas"] = True

        self.client = client = Client(**parameters)
        self.pool = ClientPool(client, pool_size)
Ejemplo n.º 8
0
 def func():
     g.babel = Babel
     g.language = get_locale()
     g.config = app.config
     g.mc_pool = ClientPool(client, app.config['POOL_SIZE'])