Пример #1
0
 def __init__(self, config):
     db = Gino(model_classes=tuple(gtfs_model.tables))
     app = web.Application(middlewares=[db])
     # I don't quite get why aiohttp says I shouldn't just use self.config
     # for this, but whatever
     app["config"] = config
     app["aiocache"] = SimpleMemoryCache()
     app["ansiconv"] = ansi2html.converter.Ansi2HTMLConverter(
         linkify=True, title="curlbus", font_size='16px')
     app['siriclient'] = SIRIClient(config["mot"]["url"],
                                    config["mot"]["user_id"])
     db.init_app(app)
     app.router.add_static(
         "/static/", os.path.join(os.path.dirname(__file__), '..',
                                  "static"))
     app.add_routes([
         web.get('/{stop_code:\d+}{tail:/*}', self.handle_station),
         web.get('/operators{tail:/*}', self.handle_operator_index),
         web.get('/nearby', self.handle_nearby),
         web.get('/{operator:\w+}{tail:/*}', self.handle_operator),
         web.get('/rail/stations{tail:/*}', self.handle_rail_stations),
         web.get('/rail/map{tail:/*}', self.handle_rail_map),
         web.get('/operators/{operator}/{route_number}{tail:/*}',
                 self.handle_route),
         web.get(
             '/operators/{operator}/{route_number}/{alternative}{tail:/*}',
             self.handle_route),
         web.get('/operators/{operator}{tail:/*}', self.handle_operator),
         web.get('/{operator}/{route_number}{tail:/*}', self.handle_route),
         web.get('/{operator}/{route_number}/{alternative}{tail:/*}',
                 self.handle_route),
         web.get('/{tail:/*}', self.handle_index)
     ])
     self._app = app
Пример #2
0
async def test_invalid_json_retry_failure(aresponses):
    """Test a failed retry after getting a failed JSON error.

    Note that we have to bust the cache before executing this test since all tests use
    the same event loop.
    """
    cache = SimpleMemoryCache()
    await cache.delete(DEFAULT_CACHE_KEY)

    aresponses.add(
        "wwlln.net",
        "/new/map/data/current.json",
        "get",
        aresponses.Response(text="This isn't JSON", status=200),
    )
    aresponses.add(
        "wwlln.net",
        "/new/map/data/current.json",
        "get",
        aresponses.Response(text="This isn't JSON", status=200),
    )

    with pytest.raises(RequestError):
        async with aiohttp.ClientSession() as session:
            client = Client(session=session)
            await client.dump()
Пример #3
0
async def test_invalid_json_retry_successful(aresponses, dump_response):
    """Test a successful retry after getting a failed JSON error.

    Note that we have to bust the cache before executing this test since all tests use
    the same event loop.
    """
    cache = SimpleMemoryCache()
    await cache.delete(DEFAULT_CACHE_KEY)

    aresponses.add(
        "wwlln.net",
        "/new/map/data/current.json",
        "get",
        aresponses.Response(text="This isn't JSON", status=200),
    )
    aresponses.add(
        "wwlln.net",
        "/new/map/data/current.json",
        "get",
        aresponses.Response(text=json.dumps(dump_response), status=200),
    )

    async with aiohttp.ClientSession() as session:
        client = Client(session=session)
        data = await client.dump()
        assert len(data) == 6
Пример #4
0
async def test_caching(aresponses, dump_response):
    """Test that the caching mechanism works properly.

    Note that we have to bust the cache before executing this test since all tests use
    the same event loop.
    """
    aresponses.add(
        "wwlln.net",
        "/new/map/data/current.json",
        "get",
        aresponses.Response(text=json.dumps(dump_response), status=200),
    )

    cache = SimpleMemoryCache()
    await cache.delete(DEFAULT_CACHE_KEY)

    async with aiohttp.ClientSession() as session:
        client = Client(session=session)

        cache_exists = await cache.exists(DEFAULT_CACHE_KEY)
        assert not cache_exists

        await client.within_radius(TEST_LATITUDE, TEST_LONGITUDE,
                                   TEST_RADIUS_METRIC)

        cache_exists = await cache.exists(DEFAULT_CACHE_KEY)
        assert cache_exists
Пример #5
0
 def __init__(self, url: str, user_id: str, cache: BaseCache=None,
              cache_ttl: int=60, verbose: bool=False):
     self.url = url
     self.user_id = user_id
     self.verbose = verbose
     self.cache_ttl = cache_ttl
     self._cache = cache if cache is not None else SimpleMemoryCache()
     self._connector = None
Пример #6
0
def memory_mock_cache(mocker):
    cache = SimpleMemoryCache()
    mocker.spy(cache, 'multi_set')
    mocker.spy(cache, 'multi_get')
    mocker.spy(cache, 'get')
    mocker.spy(cache, 'exists')
    mocker.spy(cache, 'set')
    yield cache
    SimpleMemoryBackend._cache = {}
Пример #7
0
def init_cache(app, loop):
    '''
    初始化操作,对一些参数进行配置
    :param app:
    :return
    '''
    app.config['monkey_config'] = Config
    app.cache = SimpleMemoryCache()
    app.mongo_db = MotorBase(loop=loop).get_db()
Пример #8
0
def init_cache(app, loop):
    """
    初始化操作 对一些参数进行配置
    :param app:
    :param loop:
    :return:
    """
    app.config["monkey_config"] = Config
    app.cache = SimpleMemoryCache()
    app.mongo_db = MotorBase(loop=loop).get_db()
Пример #9
0
def memory_cache(event_loop):
    cache = SimpleMemoryCache(namespace="test")
    cache.set_policy(DefaultPolicy)
    yield cache

    event_loop.run_until_complete(cache.delete(pytest.KEY))
    event_loop.run_until_complete(cache.delete(pytest.KEY_1))
Пример #10
0
async def cache_from_registry(beacons, response):
    """Cache Beacon URLs that were received from Registry's update message."""
    LOG.debug('Caching Beacons from Registry\'s update message.')

    try:
        cache = SimpleMemoryCache()
        await cache.set('beacon_urls', beacons)
        LOG.debug('Cache was set.')
    except Exception as e:
        response = 500
        LOG.error(f'Couldn\'t set cache: {e}.')

    return response
Пример #11
0
def memory_cache(event_loop):
    cache = SimpleMemoryCache(namespace="test")
    yield cache

    event_loop.run_until_complete(cache.delete(pytest.KEY))
    event_loop.run_until_complete(cache.delete(pytest.KEY_1))
    event_loop.run_until_complete(cache.delete(pytest.KEY + '-lock'))
    event_loop.run_until_complete(cache.close())
Пример #12
0
async def clear_cache():
    """Clear cache of Beacons."""
    LOG.debug('Check if cache of Beacons exists.')

    try:
        cache = SimpleMemoryCache()
        if await cache.exists("beacon_urls"):
            LOG.debug('Found old cache.')
            await cache.delete("beacon_urls")
            LOG.debug('Cache has been cleared.')
        else:
            LOG.debug('No old cache found.')
        await cache.close()
    except Exception as e:
        LOG.error(f'Error at clearing cache: {e}.')
Пример #13
0
async def test_no_explicit_session(aresponses, dump_response):
    """Test an API call with no explicitly-provided aiohttp ClientSession.

    Note that we have to bust the cache before executing this test since all tests use
    the same event loop.
    """
    cache = SimpleMemoryCache()
    await cache.delete(DEFAULT_CACHE_KEY)

    aresponses.add(
        "wwlln.net",
        "/new/map/data/current.json",
        "get",
        aresponses.Response(text=json.dumps(dump_response), status=200),
    )

    client = Client()
    data = await client.dump()
    assert len(data) == 6
Пример #14
0
async def new_search(request):
    upload_image = request.files.get("image")
    if not upload_image:
        raise NotFound(message='not image file')
    image_types = ['image/jpeg', 'image/jpg', 'image/png']
    if upload_image.type not in image_types:
        raise NotFound(message='not image file')
    upload_image_type = upload_image.type.split('/')[-1]
    file_name = str(time.time())[:10] + '.' + upload_image_type
    file_path = upload_image_path + file_name
    with open(file_path, "wb") as f:
        f.write(request.files["image"][0].body)
    search_results = image_search(file_path)[:5]
    cache = SimpleMemoryCache(serializer=JsonSerializer())
    response_dict = {
        'site_name': site_name,
        'upload_image': file_name,
        'search_results': search_results
    }
    await cache.set("response_dict", response_dict)
    return response_dict
Пример #15
0
async def test_within_window(aresponses, dump_response):
    """Test retrieving the nearest strikes within a 10-minute window."""
    # Bust the cache since we're parametrizing the input data:
    cache = SimpleMemoryCache()
    await cache.delete(DEFAULT_CACHE_KEY)

    aresponses.add(
        "wwlln.net",
        "/new/map/data/current.json",
        "get",
        aresponses.Response(text=json.dumps(dump_response), status=200),
    )

    async with aiohttp.ClientSession() as session:
        client = Client(session=session)
        data = await client.within_radius(
            TEST_LATITUDE,
            TEST_LONGITUDE,
            TEST_RADIUS_METRIC,
            window=timedelta(minutes=10),
        )
        assert len(data) == 1
Пример #16
0
"""
Copyright 2017-2020 Government of Canada - Public Services and Procurement Canada - buyandsell.gc.ca

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

from aiocache import SimpleMemoryCache

MEM_CACHE = SimpleMemoryCache()
Пример #17
0
 def test_default_serializer(self):
     assert isinstance(SimpleMemoryCache().serializer, NullSerializer)
Пример #18
0
 def test_inheritance(self):
     assert isinstance(SimpleMemoryCache(), BaseCache)
Пример #19
0
 async def test_accept_explicit_args(self):
     with pytest.raises(TypeError):
         SimpleMemoryCache(random_attr="wtf")
Пример #20
0
def setup_cache():
    cache = SimpleMemoryCache(ttl=10)
    return cache
Пример #21
0
 def test_parse_uri_path(self):
     assert SimpleMemoryCache().parse_uri_path("/1/2/3") == {}
Пример #22
0
import asyncio
import logging
from random import randint

import aiohttp
import discord
from aiocache import cached, SimpleMemoryCache

from api.expected_errors import ExpectedCommandError
from . import boorusources
from .CONSTANTS import FILTERS, NSFW_FILTERS, BOARDS, HEADERS, NSFW_BOARDS, tags_to_board

cache = SimpleMemoryCache()

log = logging.getLogger("BooruCore")
log.setLevel(logging.DEBUG)


class BooruCore:
    async def generic_booru(self, ctx, tag):

        tag = await self.filter_tags(ctx, tag)

        # If it returns nothing, something is wrong
        if tag is None:
            return

        log.debug(tag)

        boards = tags_to_board(tag)
        if not ctx.message.channel.is_nsfw():
Пример #23
0
import os

import asyncio

from aiocache import SimpleMemoryCache
from aiohttp import web, client_exceptions, ClientSession

BASE_URL = "https://demo.calendar42.com/api/v2"
TOKEN = os.environ.get('CALENDAR42_TOKEN')

# NOTE: use redis in production
cache = SimpleMemoryCache(timeout=260)  # 4.2 min


async def fetch(url, session):
    """
    Returns JSON content of the page.
    """
    cached = await cache.get(url)
    if cached:
        return cached

    headers = {
        'Content-type': 'application/json',
        'Authorization': 'Token {token}'.format(token=TOKEN),
    }
    async with session.get(url, headers=headers) as response:
        data = await response.json()
        if response.status == 200:
            await cache.set(url, data)
        else:
Пример #24
0
 def __init__(self, db, feed_url: str = TELAVIV_FEED_URL):
     self.feed_url: str = feed_url
     self.db = db
     self.cache = SimpleMemoryCache()
Пример #25
0
async def reuse_data():
    cache = SimpleMemoryCache(
        serializer=JsonSerializer())  # Not ideal to define here
    data = await cache.get("my_custom_key"
                           )  # Note the key is defined in `cached` decorator
    return data
from aiocache import SimpleMemoryCache

'''
https://aiocache.readthedocs.io/en/latest/
pip install aiocache
pip install aiocache[memcached]
'''

ch = SimpleMemoryCache()

if __name__ == '__main__':
	ch.multi_set([[1, {1: "1"}], [2, {2: "2"}]])
	d = ch.multi_get([1, 2, 3])
	print(d)
Пример #27
0
from aiocache import SimpleMemoryCache
from aiocache.serializers import JsonSerializer
from aiohttp import web
from aiohttp_apispec import docs, response_schema, use_kwargs
from marshmallow import ValidationError

from {{cookiecutter.project_slug}}.entities.requests import RequestPostDummy
from {{cookiecutter.project_slug}}.entities.responses import ResponseGetDummyData, ResponseGetDummy, \
    ResponseInternalError
from {{cookiecutter.project_slug}}.services.dummy import get_dummy, post_dummy
from {{cookiecutter.project_slug}}.log_manager import log_manager

LOGGER = log_manager.getLogger(module_name=__name__)

cache = SimpleMemoryCache(serializer=JsonSerializer())


class DummyHandler(web.View):
    @docs(tags=['dummy'],
          summary='Get dummy',
          description='''Dummy resource''')
    @response_schema(ResponseGetDummyData.Schema(), 200, description="Single dummy", required=True)
    @response_schema(ResponseInternalError.Schema(), 400, description="Error description", required=True)
    async def get(self):
        dummy = await get_dummy()
        try:
            payload = dummy.make_dump()
        except ValidationError as err:
            return web.json_response({"error": err.messages}, status=400)
        return web.json_response({"data": payload})
        # dumps returns (data, errors), we just want to save data
        return super().dumps(*args, **kwargs).data

    def loads(self, *args, **kwargs):
        # dumps returns (data, errors), we just want to return data
        return super().loads(*args, **kwargs).data

    @post_load
    def build_my_type(self, data):
        return RandomModel(**data)

    class Meta:
        strict = True


cache = SimpleMemoryCache(serializer=MarshmallowSerializer(), namespace="main")


async def serializer():
    model = RandomModel()
    await cache.set("key", model)

    result = await cache.get("key")

    assert result.int_type == model.int_type
    assert result.str_type == model.str_type
    assert result.dict_type == model.dict_type
    assert result.list_type == model.list_type


def test_serializer():
Пример #29
0
async def view_search_result(request):
    cache = SimpleMemoryCache(serializer=JsonSerializer())
    response_dict = await cache.get("response_dict")
    if not response_dict:
        response_dict = {}
    return response_dict
Пример #30
0
from datetime import datetime
from time import mktime

import aiohttp
from aiocache import cached, SimpleMemoryCache
from aiocache.serializers import PickleSerializer
from aiohttp import web
from lru_plugin import LRUPlugin

steps_cache = SimpleMemoryCache(serializer=PickleSerializer(),
                                plugins=[LRUPlugin(max_keys=1000)])


class HTTPForbidden(Exception):
    pass


async def get_steps(lesson_id, lsteps, update_date):
    """Works with the cache, where steps are stored.

    Return True if step is theoretical and False if not. If access if forbidden, raise HTTPForbidden error.

    :param lesson_id: str lesson id
    :param lsteps: list of lesson's steps
    :param update_date: int timestamp when lesson was updated
    """
    result = []
    if await steps_cache.exists(lesson_id):
        cached_steps = await steps_cache.get(lesson_id)
        # If data wasn't updated before, then return from cache
        if cached_steps[0] >= update_date: