Exemple #1
0
 async def test_set_and_get_using_namespace(self, memory_cache):
     memory_cache.plugins = [HitMissRatioPlugin()]
     key = "A"
     namespace = "test"
     value = 1
     await memory_cache.set(key, value, namespace=namespace)
     result = await memory_cache.get(key, namespace=namespace)
     assert result == value
Exemple #2
0
    async def test_multi_get_hit_miss_ratio(self, memory_cache, data, ratio):
        keys = ["a", "b", "c", "d", "e", "f"]
        memory_cache.plugins = [HitMissRatioPlugin()]
        SimpleMemoryBackend._cache = data

        for key in keys:
            await memory_cache.multi_get([key])

        hits = [x for x in keys if 'test' + x in data]
        assert memory_cache.hit_miss_ratio["hits"] == len(hits)
        assert memory_cache.hit_miss_ratio["hit_ratio"] == \
            len(hits)/memory_cache.hit_miss_ratio["total"]
Exemple #3
0
async def init(loop):
    conf = load_config(PROJ_ROOT / 'config' / 'config.yml')

    app = web.Application(loop=loop)

    app.router.add_route('GET', "/api/v2/{item}", method)
    app.router.add_route('GET', "/api/{item}", method)
    cache = Cache(plugins=[HitMissRatioPlugin(), TimingPlugin()])

    print(conf['allowed_items'])

    if 'host' in conf:
        host = conf['host']
    else:
        host = '127.0.0.1'

    if 'port' in conf:
        port = conf['port']
    else:
        port = '443'

    if 'access_log_format' in conf:
        access_log_format = conf['access_log_format']
    else:
        access_log_format = '%a %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"'

    if 'scheme' in conf:
        if conf['scheme'] == 'https':
            if 'sslcertchain' and 'sslkey' in conf:
                ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
                ssl_context.load_verify_locations(conf['sslcertchain'],
                                                  conf['sslkey'])
                ssl_context.load_cert_chain(conf['sslcertchain'],
                                            conf['sslkey'])
            else:
                raise NameError(
                    'sslcertchain / sslkey missing in the configuration')
        else:
            ssl_context = None
    else:
        ssl_context = None

    app['config'] = conf

    user, password, realm = conf['authentication']['user'], conf[
        'authentication']['password'], conf['authentication']['realm']
    await setup(app, AllowedHosts(conf['allowed_hosts']),
                BasicAuth(user, password, realm))
    app['cache'] = cache
    return app, cache, host, port, access_log_format, ssl_context
async def cache():
    conf = load_config(PROJ_ROOT / 'config' / 'config-gunicorn.yml')

    logging.basicConfig(level=logging.DEBUG)
    app = web.Application()

    app.router.add_route('GET', "/api/v2/{item}", method)
    app.router.add_route('GET', "/api/v2/{item}/{domain}", method)
    app.router.add_route('GET', "/api/{item}", method)
    app.router.add_route('GET', "/api/{item}/{domain}", method)
    memcached_host = conf['cache']['memcached_host']
    memcached_port = conf['cache']['memcached_port']
    #cache = Cache(plugins=[HitMissRatioPlugin(), TimingPlugin()])
    lookup_type = {}
    cache = Cache(Cache.MEMCACHED,
                  endpoint=memcached_host,
                  port=memcached_port,
                  serializer=JsonSerializer(),
                  plugins=[HitMissRatioPlugin(),
                           TimingPlugin()])

    if 'statsd' in conf:
        if conf['statsd']['enable']:
            hostname = socket.gethostname().split('.', 1)[0]
            c = statsd.StatsClient(conf['statsd']['host'],
                                   conf['statsd']['port'],
                                   prefix=conf['statsd']['prefix'])
            t = MetricsTimer(conf['statsd']['interval'], cache_metrics, cache,
                             lookup_type, c, hostname)

    app['config'] = conf

    user, password, realm = conf['authentication']['user'], conf[
        'authentication']['password'], conf['authentication']['realm']
    await setup(app, AllowedHosts(conf['allowed_hosts']),
                BasicAuth(user, password, realm))
    app['cache'] = cache
    app['lookup_type'] = lookup_type
    return app
Exemple #5
0
from aiocache.plugins import HitMissRatioPlugin, TimingPlugin, BasePlugin

logger = logging.getLogger(__name__)


class MyCustomPlugin(BasePlugin):
    async def pre_set(self, *args, **kwargs):
        logger.info("I'm the pre_set hook being called with %s %s" %
                    (args, kwargs))

    async def post_set(self, *args, **kwargs):
        logger.info("I'm the post_set hook being called with %s %s" %
                    (args, kwargs))


cache = Cache(plugins=[HitMissRatioPlugin(),
                       TimingPlugin(),
                       MyCustomPlugin()],
              namespace="main")


async def run():
    await cache.set("a", "1")
    await cache.set("b", "2")
    await cache.set("c", "3")
    await cache.set("d", "4")

    possible_keys = ["a", "b", "c", "d", "e", "f"]

    for t in range(1000):
        await cache.get(random.choice(possible_keys))
Exemple #6
0
 def plugin(self):
     return HitMissRatioPlugin()
Exemple #7
0
    '''Generate a playlist'''
    mf = await helpers.get_filter(request)
    musics = await db.musics(mf)
    if len(musics) == 0:
        return response.text('Empty playlist')
    name = request.args.get('name', 'archive')
    return helpers.zip(musics, name)


async def gen_playlist(request):
    mf = await helpers.get_filter(request)
    musics = await db.musics(mf)
    return await helpers.template('player.html', musics=musics, mf=mf)


@cached(cache=SimpleMemoryCache,
        serializer=PickleSerializer(),
        plugins=[HitMissRatioPlugin(), TimingPlugin()])
async def cached_call(f, request):
    return await f(request)


@collection.route('/player')
@helpers.basicauth
async def player(request):
    '''Play a playlist in browser'''
    if request.args.get('shuffle', False):
        debug('Shuffled playlist, not using cache')
        return await gen_playlist(request)
    return await cached_call(gen_playlist, request)
Exemple #8
0

logger = logging.getLogger(__name__)


class MyCustomPlugin(BasePlugin):

    async def pre_set(self, *args, **kwargs):
        logger.info("I'm the pre_set hook being called with %s %s" % (args, kwargs))

    async def post_set(self, *args, **kwargs):
        logger.info("I'm the post_set hook being called with %s %s" % (args, kwargs))


cache = SimpleMemoryCache(
    plugins=[HitMissRatioPlugin(), TimingPlugin(), MyCustomPlugin()],
    namespace="main")


async def run():
    await cache.set("a", "1")
    await cache.set("b", "2")
    await cache.set("c", "3")
    await cache.set("d", "4")

    possible_keys = ["a", "b", "c", "d", "e", "f"]

    for t in range(1000):
        await cache.get(random.choice(possible_keys))

    assert cache.hit_miss_ratio["hit_ratio"] > 0.5
Exemple #9
0
  '1103': 'HCGH',
  '1104': 'Sibley',
  '1105': 'Suburban',
  '1107': 'KKI',
}

locations = {
  'JHH' : 'Johns Hopkins Hospital',
  'BMC' : 'Bayview Medical Center',
  'HCGH' : 'Howard County General Hospital'
}


##############################
# Globals: cache and monitor.
pat_cache = LRUMemoryCache(plugins=[HitMissRatioPlugin()], max_size=5000)
api_monitor = query.api_monitor

# Data structures for order placement in TREWS.
# 1. pending_orders: a dict of pending orders by session.
#    Pending orders are represented as a list of order type and order placement time
#    as initiated by the user through the TREWS page.
#
# 2. order_processing_tasks: a dict of background order checking tasks by session.
#    Tasks are represented as a list of asyncio Tasks.
#
# Entries in both of these datastructures are deleted at the end of a session.
#
pending_orders = {}
order_processing_tasks = {}
Exemple #10
0
def dbkey(m, f, query, args):
    j = orjson.dumps(args,
                     option=orjson.OPT_OMIT_MICROSECONDS,
                     default=default).decode()
    dbkey = f"{query}{j}"
    h = hash(dbkey)
    # logger.debug(f"dbkey: {dbkey} h: {h}")
    return h


cache_config = {
    "key_builder": dbkey,
    "cache": SimpleMemoryCache,
    "noself": True,
    "plugins": [
        HitMissRatioPlugin(),
        TimingPlugin(),
    ],
}


async def db_pool(pool):
    if pool is None:
        pool = await asyncpg.create_pool(
            settings.DATABASE_URL,
            command_timeout=14,
            max_inactive_connection_lifetime=15,
            min_size=1,
            max_size=10,
        )
    return pool