Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Multiple caches #775

Open
Sagaryal opened this issue Nov 1, 2023 · 3 comments
Open

Create Multiple caches #775

Sagaryal opened this issue Nov 1, 2023 · 3 comments

Comments

@Sagaryal
Copy link

Sagaryal commented Nov 1, 2023

In cachetools, we can create multiple cache instance like below

from cachetools import TTLCache, cached

A_cache = TTLCache(maxsize=100, ttl=60)
B_cache = TTLCache(maxsize=100, ttl=60)

@cached(cache=A_cache)
def a():
    # some code

@cached(cache=B_cache)
def b():
    # some code

A_cache.clear()
B_cache.clear()

I couldn't find a way to replicate the above code using aiocache?

@Dreamsorcerer
Copy link
Member

If you look through the milestone 1.0 issues, you'll see that the plan for v1 is to pass the cache instance, just as you've done in your example. But, for the 0.x releases, you might need to use the config thing to define the caches, or just make sure to use different arguments to namespace the caches.

@Sagaryal
Copy link
Author

Sagaryal commented Nov 4, 2023

If you look through the milestone 1.0 issues, you'll see that the plan for v1 is to pass the cache instance, just as you've done in your example. But, for the 0.x releases, you might need to use the config thing to define the caches, or just make sure to use different arguments to namespace the caches.

I suppose you are talking about this

caches.set_config({
    'default': {
        'cache': "aiocache.SimpleMemoryCache",
        'serializer': {
            'class': "aiocache.serializers.StringSerializer"
        }
    },
    'redis_alt': {
        'cache': "aiocache.RedisCache",
        "host": "127.0.0.1",
        'port': 6379,
        "socket_connect_timeout": 1,
        'serializer': {
            'class': "aiocache.serializers.PickleSerializer"
        },
        'plugins': [
            {'class': "aiocache.plugins.HitMissRatioPlugin"},
            {'class': "aiocache.plugins.TimingPlugin"}
        ]
    }
})

But how would you reference individual ones in decorator then? I don't want to do below later on except clear(). Also I am not sure if I have to create a cache first.

cache = caches.create("redis_alt")
await cache.set("key", "value")

or just make sure to use different arguments to namespace the caches.

Could you please clarify a bit more?

@Dreamsorcerer
Copy link
Member

Appears to be an alias parameter?
https://github.com/aio-libs/aiocache/blob/master/aiocache/decorators.py#L52-L54

We seem to be missing a reference section in the docs...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants