示例#1
0
async def test_clear_cache():
    """Test clearing a cache."""

    test_cache = aiocache.SimpleMemoryCache(namespace='test')

    # first, put a value in the cache and ensure it is there.
    await test_cache.set('key', 'value')
    val = await test_cache.get('key')
    assert val == 'value'

    # clear the cache
    await cache.clear_cache('test')

    val = await test_cache.get('key')
    assert val is None
示例#2
0
async def test_clear_all_meta_caches():
    """Clear all meta-info caches."""

    meta = aiocache.SimpleMemoryCache(namespace=cache.NS_DEVICE_INFO)
    scan = aiocache.SimpleMemoryCache(namespace=cache.NS_SCAN)
    info = aiocache.SimpleMemoryCache(namespace=cache.NS_INFO)
    other = aiocache.SimpleMemoryCache(namespace='other')

    # first, populate the test caches
    for c in [meta, scan, info, other]:
        ok = await c.set('key', 'value')
        assert ok

    # clear the meta caches
    await cache.clear_all_meta_caches()

    # now, the meta caches should be empty, but the other cache
    # should not be affected.
    for c in [meta, scan, info]:
        val = await c.get('key')
        assert val is None

    val = await other.get('key')
    assert val == 'value'
示例#3
0
 def _build_cache(
         self) -> Union[aiocache.RedisCache, aiocache.SimpleMemoryCache]:
     if (self.stage and self.stage == "prod"
             and self.redis_url):  # TODO: Refactor to util?
         logger.info("Initializing RedisCloud Cache...")
         return aiocache.RedisCache(
             endpoint=self.redis_url.hostname,
             port=self.redis_url.port,
             password=self.redis_url.password,
             create_connection_timeout=5,
             serializer=CacheCompressionSerializer(),
         )
     else:
         logger.info("Initializing SimpleMemoryCache...")
         return aiocache.SimpleMemoryCache()
示例#4
0
NS_DEVICE_INFO = 'devices'
NS_PLUGINS = 'plugins'
NS_SCAN = 'scan'
NS_INFO = 'info'
NS_CAPABILITIES = 'capabilities'

# Internal keys into the caches for the data (e.g. dictionaries)
# being cached.
DEVICE_INFO_CACHE_KEY = 'meta_cache_key'
PLUGINS_CACHE_KEY = 'plugins_cache_key'
SCAN_CACHE_KEY = 'scan_cache_key'
INFO_CACHE_KEY = 'info_cache_key'
CAPABILITIES_CACHE_KEY = 'capabilities_cache_key'

# Create caches
transaction_cache = aiocache.SimpleMemoryCache(namespace=NS_TRANSACTION)
_device_info_cache = aiocache.SimpleMemoryCache(namespace=NS_DEVICE_INFO)
_plugins_cache = aiocache.SimpleMemoryCache(namespace=NS_PLUGINS)
_scan_cache = aiocache.SimpleMemoryCache(namespace=NS_SCAN)
_info_cache = aiocache.SimpleMemoryCache(namespace=NS_INFO)
_capabilities_cache = aiocache.SimpleMemoryCache(namespace=NS_CAPABILITIES)


def configure_cache():
    """Set the configuration for the caches used by Synse Server."""
    logger.debug(_('Setting cache configuration: {}').format(AIOCACHE))
    aiocache.caches.set_config(AIOCACHE)


async def clear_cache(namespace):
    """Clear the cache with the given namespace.
示例#5
0
from synse_grpc import api

from synse_server import loop, plugin
from synse_server.metrics import Monitor

logger = get_logger()

# The in-memory cache implementation stores data in a class member variable,
# so all instance of the in memory cache will reference that data structure.
# In order to separate transactions from devices, we need each cache to define
# its own namespace.
NS_TRANSACTION = 'synse.txn.'
NS_DEVICE = 'synse.dev.'
NS_ALIAS = 'synse.alias.'

transaction_cache = aiocache.SimpleMemoryCache(namespace=NS_TRANSACTION, )

device_cache = aiocache.SimpleMemoryCache(namespace=NS_DEVICE, )

alias_cache = aiocache.SimpleMemoryCache(namespace=NS_ALIAS, )

device_cache_lock = asyncio.Lock(loop=loop.synse_loop)
alias_cache_lock = asyncio.Lock(loop=loop.synse_loop)


async def get_transaction(transaction_id: str) -> dict:
    """Get the cached transaction information with the provided ID.

    If the provided transaction ID does not correspond to a known transaction,
    None is returned.
示例#6
0
# Synse Server cache namespaces
NS_TRANSACTION = 'transaction'
NS_META = 'meta'
NS_PLUGINS = 'plugins'
NS_SCAN = 'scan'
NS_INFO = 'info'

# Internal keys into the caches for the data (e.g. dictionaries)
# being cached.
META_CACHE_KEY = 'meta_cache_key'
PLUGINS_CACHE_KEY = 'plugins_cache_key'
SCAN_CACHE_KEY = 'scan_cache_key'
INFO_CACHE_KEY = 'info_cache_key'

# Create caches
transaction_cache = aiocache.SimpleMemoryCache(namespace=NS_TRANSACTION)
_meta_cache = aiocache.SimpleMemoryCache(namespace=NS_META)
_plugins_cache = aiocache.SimpleMemoryCache(namespace=NS_PLUGINS)
_scan_cache = aiocache.SimpleMemoryCache(namespace=NS_SCAN)
_info_cache = aiocache.SimpleMemoryCache(namespace=NS_INFO)


def configure_cache():
    """Set the configuration for the caches used by Synse Server."""
    logger.debug(_('Setting cache configuration: {}').format(AIOCACHE))
    aiocache.caches.set_config(AIOCACHE)


async def clear_cache(namespace):
    """Clear the cache with the given namespace.
示例#7
0
import asyncio
import argparse
import logging
import uuid
import aiocache

from aiohttp import web

logging.getLogger("aiohttp.access").propagate = False

AIOCACHE_BACKENDS = {
    "memory": aiocache.SimpleMemoryCache(),
    "redis": aiocache.RedisCache(pool_max_size=1),
    "memcached": aiocache.MemcachedCache(pool_size=1),
}


class CacheManager:
    def __init__(self, backend):
        self.cache = AIOCACHE_BACKENDS.get(backend)

    async def get(self, key):
        return await self.cache.get(key, timeout=0.1)

    async def set(self, key, value):
        return await self.cache.set(key, value, timeout=0.1)


async def handler_get(req):
    try:
        data = await req.app["cache"].get("testkey")