aiocache: Block writing cache in `cached` decorator if custom condition is not satisfied

I m using the  aiocache.cached decorator with wraps the certain processing function. The setup is as following:

import aiocache


aiocache.caches.add(
    "cache-alias",
    {
        "cache": "aiocache.RedisCache",
        {
            <cache-config-data>
        }
    },
)


@aiocache.cached(
    alias="cache-alias",
    key_builder=some_key_builder,
    ttl=ttl,
)
async def processing(...) -> int:
    ...

The problem is – can i pass any argument to cached decorator in order to avoid cache writing for processing(...) return? It seems like the only way to perform it is to throw exception inside the processing(...) function if the result is not appropriate one. I m looking for more generic way to do the same stuff.

For example, pass some Callable object to cached.__call__() returning the bool var meaning caching the results. Something similar to:

def checker(value: Any) -> bool:
    ...

@aiocache.cached(
    alias="cache-alias",
    key_builder=some_key_builder,
    value_checker=checker,
    ttl=ttl,
)
async def processing(...) -> int:
    ...

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 15 (13 by maintainers)

Most upvoted comments

@VitalyPetrov if you are looking for something that already support that kind of functionality in advanced way, check here. Sorry for advertisement, by I don’t get why aiocache can’t be as much flexible as it possible.