Skip to content

Commit

Permalink
Merge pull request #23 from Selutario/fix-22-custom-middlewares
Browse files Browse the repository at this point in the history
Search cache attributes recursively.
  • Loading branch information
zhukovgreen committed Apr 8, 2020
2 parents d7f6241 + e1c1acb commit f909f53
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions aiohttp_cache/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@
from aiohttp.web_response import Response


def get_original_handler(handler):
if hasattr(handler, 'cache_enable'):
return handler
elif hasattr(handler, 'keywords'):
return get_original_handler(handler.keywords['handler'])


@web.middleware
async def cache_middleware(request, handler):
if getattr(handler, "cache_enable", False):

original_handler = get_original_handler(handler)

if getattr(original_handler, "cache_enable", False):
#
# Cache is disabled?
#
if getattr(handler, "cache_unless", False) is True:
if getattr(original_handler, "cache_unless", False) is True:
return await handler(request)

cache_backend = request.app["cache"]
Expand All @@ -30,7 +40,7 @@ async def cache_middleware(request, handler):
"body": original_response.body,
}

expires = getattr(handler, "cache_expires", 300)
expires = getattr(original_handler, "cache_expires", 300)

await cache_backend.set(key, data, expires)

Expand Down

0 comments on commit f909f53

Please sign in to comment.