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

Make it possible to use single cache for different methods #83

Closed
grundic opened this issue Jul 4, 2016 · 4 comments
Closed

Make it possible to use single cache for different methods #83

grundic opened this issue Jul 4, 2016 · 4 comments

Comments

@grundic
Copy link

grundic commented Jul 4, 2016

Would it be possible to implement cache separation of different methods with same signature for cachedmethod decorator? Here is small example with use of boltons and cachetools:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import operator
from boltons import cacheutils
import cachetools


class BoltonsCalculator(object):
    def __init__(self):
        self.cache = cacheutils.LRU()

    @cacheutils.cachedmethod('cache')
    def square(self, number):
        print("Calculating square of {}".format(number))
        return number * number

    @cacheutils.cachedmethod('cache')
    def cube(self, number):
        print("Calculating cube of {}".format(number))
        return number * number * number


class CachetoolsCalculator(object):
    def __init__(self):
        self.cache = cachetools.LRUCache(128)

    @cachetools.cachedmethod(operator.attrgetter('cache'))
    def square(self, number):
        print("Calculating square of {}".format(number))
        return number * number

    @cachetools.cachedmethod(operator.attrgetter('cache'))
    def cube(self, number):
        print("Calculating cube of {}".format(number))
        return number * number * number


if __name__ == '__main__':
    boltons_calc = BoltonsCalculator()
    print(boltons_calc.square(2))
    print(boltons_calc.square(2))
    print(boltons_calc.cube(2))

    print('-' * 80)

    cachetools_calc = CachetoolsCalculator()
    print(cachetools_calc.square(2))
    print(cachetools_calc.square(2))
    print(cachetools_calc.cube(2))

Here is output:

Calculating square of 2
4
4
4 <== I expect to get 8 here
--------------------------------------------------------------------------------
Calculating square of 2
4
4
Calculating cube of 2
8

Process finished with exit code 0

As you can see, third print gives us 4, though I expect to get 8.
Maybe this could help you better understand how this is implemented in cachetools.

@mahmoud
Copy link
Owner

mahmoud commented Jul 5, 2016

Certainly possible! I'll run it past a couple other contributors and see what everyone thinks. Shouldn't be too much of a problem. Also, cachetools looks like a neat package, but I think the implementation will look a bit different (while having the same effect) :)

@grundic
Copy link
Author

grundic commented Jul 5, 2016

@mahmoud, thank you for your answer!
The implementation details is absolutely up to you, I've added a link to cachetools just to make my request more clear.

Personally I don't need this feature, but I think it would be nice to have: make usual functions (not bound methods) to follow same behaviour.

@mahmoud
Copy link
Owner

mahmoud commented Jul 19, 2016

Hey @grundic! I updated @cached and @cachedmethod to behave as you describe. After a long think and some discussion, I agree that this would minimize surprising behavior for this particular corner of boltons. The critical change is the addition of the scoped argument to cached/cachedmethod. The docs are already updated with the details, and I'm putting a release together as soon as I submit this comment.

@grundic
Copy link
Author

grundic commented Jul 25, 2016

@mahmoud sorry for late reply, as I was on vacation.

The change looks good for me, I've tested it with my code -- works good.
Agree, this scope parameter gives flexibility to the user to decide which behaviour he would like.

Thank you very much!

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