Skip to content

Commit

Permalink
Apply new pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zhukovgreen committed May 4, 2020
1 parent df3fc57 commit bc85da1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 29 deletions.
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ WAIT_TIME = 2


@cache()
async def some_long_running_view(request: web.Request) -> web.Response:
async def some_long_running_view(
request: web.Request,
) -> web.Response:
await asyncio.sleep(WAIT_TIME)
payload = await request.json()
return web.json_response(payload)
Expand Down Expand Up @@ -63,14 +65,18 @@ WAIT_TIME = 2


@cache()
async def some_long_running_view(request: web.Request) -> web.Response:
async def some_long_running_view(
request: web.Request,
) -> web.Response:
await asyncio.sleep(WAIT_TIME)
payload = await request.json()
return web.json_response(payload)


app = web.Application()
url = yarl.URL(env.str("CACHE_URL", default="redis://localhost:6379/0"))
url = yarl.URL(
env.str("CACHE_URL", default="redis://localhost:6379/0")
)
setup_cache(
app,
cache_type="redis",
Expand Down Expand Up @@ -106,14 +112,20 @@ import asyncio

from aiohttp import web

from aiohttp_cache import setup_cache, cache, AvailableKeys # noqa
from aiohttp_cache import (
setup_cache,
cache,
AvailableKeys,
) # noqa

PAYLOAD = {"hello": "aiohttp_cache"}
WAIT_TIME = 2


@cache()
async def some_long_running_view(request: web.Request) -> web.Response:
async def some_long_running_view(
request: web.Request,
) -> web.Response:
await asyncio.sleep(WAIT_TIME)
payload = await request.json()
return web.json_response(payload)
Expand Down
1 change: 0 additions & 1 deletion aiohttp_cache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from .middleware import cache_middleware
from .setup import setup_cache


__all__ = (
"AvailableKeys",
"MemoryCache",
Expand Down
12 changes: 6 additions & 6 deletions aiohttp_cache/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import enum
import pickle
import time
from _sha256 import sha256 # noqa
from typing import Tuple # noqa
from _sha256 import sha256
from typing import Dict, Tuple

import aiohttp.web
import aioredis
Expand Down Expand Up @@ -31,7 +31,7 @@ class BaseCache(object):
def __init__(
self,
expiration: int = 300,
key_pattern: Tuple[AvailableKeys] = DEFAULT_KEY_PATTERN,
key_pattern: Tuple[AvailableKeys, ...] = DEFAULT_KEY_PATTERN,
encrypt_key=True,
):
self.encrypt_key = encrypt_key
Expand Down Expand Up @@ -109,7 +109,7 @@ def __init__(
*,
loop: asyncio.BaseEventLoop = None,
expiration: int = 300,
key_pattern: Tuple[AvailableKeys] = DEFAULT_KEY_PATTERN,
key_pattern: Tuple[AvailableKeys, ...] = DEFAULT_KEY_PATTERN,
encrypt_key=True,
):
"""
Expand Down Expand Up @@ -203,7 +203,7 @@ def __init__(
self,
*,
expiration=300,
key_pattern: Tuple[AvailableKeys] = DEFAULT_KEY_PATTERN,
key_pattern: Tuple[AvailableKeys, ...] = DEFAULT_KEY_PATTERN,
encrypt_key=True,
):
super().__init__(
Expand All @@ -216,7 +216,7 @@ def __init__(
# Cache format:
# (cached object, expire date)
#
self._cache = {}
self._cache: Dict[str, Tuple[dict, int]] = {}

async def get(self, key: str):
# Update the keys
Expand Down
8 changes: 4 additions & 4 deletions aiohttp_cache/setup.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import logging
from typing import Tuple
from typing import Optional, Tuple, Union

from aiohttp import web

from aiohttp_cache import ( # noqa
AvailableKeys,
cache_middleware,
MemoryCache,
RedisConfig,
RedisCache,
RedisConfig,
cache_middleware,
)
from aiohttp_cache.backends import DEFAULT_KEY_PATTERN
from aiohttp_cache.exceptions import HTTPCache
Expand All @@ -25,7 +25,7 @@ def setup_cache(
):
app.middlewares.append(cache_middleware)

_cache_backend = None
_cache_backend: Optional[Union[MemoryCache, RedisCache]] = None
if cache_type.lower() == "memory":
_cache_backend = MemoryCache(
key_pattern=key_pattern, encrypt_key=encrypt_key
Expand Down
27 changes: 14 additions & 13 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@
from aiohttp.test_utils import TestClient
from envparse import env

from aiohttp_cache import ( # noqa
setup_cache,
cache,
RedisConfig,
)
from aiohttp_cache.backends import ( # noqa
AvailableKeys as K,
DEFAULT_KEY_PATTERN,
)
from aiohttp_cache import RedisConfig, cache, setup_cache
from aiohttp_cache.backends import DEFAULT_KEY_PATTERN, AvailableKeys

PAYLOAD = {"hello": "aiohttp_cache"}
WAIT_TIME = 2
Expand Down Expand Up @@ -87,13 +80,17 @@ def client_redis_cache(

@pytest.fixture
def client_memory_cache_another_key(
loop: asyncio.AbstractEventLoop, aiohttp_client
loop: asyncio.AbstractEventLoop, aiohttp_client,
) -> TestClient:
client_: TestClient = loop.run_until_complete(
aiohttp_client(
build_application(
cache_type="memory",
key_pattern=(K.method, K.path, K.json),
key_pattern=(
AvailableKeys.method,
AvailableKeys.path,
AvailableKeys.json,
),
encrypt_key=False,
)
)
Expand All @@ -108,13 +105,17 @@ def client_memory_cache_another_key(

@pytest.fixture
def client_redis_cache_another_key(
loop: asyncio.AbstractEventLoop, aiohttp_client
loop: asyncio.AbstractEventLoop, aiohttp_client,
) -> TestClient:
client_: TestClient = loop.run_until_complete(
aiohttp_client(
build_application(
cache_type="redis",
key_pattern=(K.method, K.path, K.json),
key_pattern=(
AvailableKeys.method,
AvailableKeys.path,
AvailableKeys.json,
),
encrypt_key=False,
)
)
Expand Down

0 comments on commit bc85da1

Please sign in to comment.