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

Calling 'clear' on namespaced SimpleMemoryCache instance clears entire underlying cache #479

Closed
edaniszewski opened this issue Feb 21, 2020 · 0 comments · Fixed by #562
Closed

Comments

@edaniszewski
Copy link

When calling clear on an instance of a SimpleMemoryCache with a namespace assigned, it will clear the entire underlying cache instead of using the instance namespace. An example:

import aiocache
import asyncio

# Define namespace constants
NS_ONE = 'ns.one'
NS_TWO = 'ns.two'

# Define cache references
cache_one = aiocache.SimpleMemoryCache(namespace=NS_ONE)
cache_two = aiocache.SimpleMemoryCache(namespace=NS_TWO)


async def do_something():
    # Add some data to CACHE_ONE, then get it and print it out
    # to verify it is there.
    await cache_one.set('foo', 'bar')
    val = await cache_one.get('foo')
    print(f'cache_one    k=foo v={val}')
    print(f'underlying cache: {cache_one._cache}')
    print('')

    # Call `clear` on cache_two. Since the cache reference is differentiated
    # by namespace, one would expect the clear to apply to the namespace of
    # the referenced cache.
    await cache_two.clear()

    # Get the value from cache_one again. It is not there because the
    # entire cache was cleared, not just the data under the cache_two namespace.
    val = await cache_one.get('foo')
    print(f'cache_one    k=foo v={val}')
    print(f'underlying cache: {cache_one._cache}')


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(do_something())

Where the output is:

cache_one    k=foo v=bar
underlying cache: {'ns.onefoo': 'bar'}

cache_one    k=foo v=None
underlying cache: {}

Specifying the namespace explicitly for the call to clear behaves as expected though

- await cache_two.clear()
+ await cache_two.clear(NS_TWO)

leading to the output

cache_one    k=foo v=bar
underlying cache: {'ns.onefoo': 'bar'}

cache_one    k=foo v=bar
underlying cache: {'ns.onefoo': 'bar'}

I believe this is related to this bit of code:
https://github.com/argaen/aiocache/blob/f811995e0632b46a4597cc07007c26f755d2ffa3/aiocache/base.py#L417-L431

Where the namespace is only ever passed in via parameter, never checking to see if the instance's namespace member is set.

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

Successfully merging a pull request may close this issue.

2 participants