Krukov / cashews

Cache with async power
MIT License
371 stars 22 forks source link

Feature: Get or set #230

Open alex-way opened 2 months ago

alex-way commented 2 months ago

It would be really cool if there was a helper function on the cache object which would take a cache key and a callable. Returning either the current value if it exists, or call the given function to set the value and return it. Django has the same feature implemented: https://docs.djangoproject.com/en/5.0/topics/cache/#django.core.cache.cache.get_or_set

I think it should also be possible to type the return value of the function as the return value of the given function:


...
T = TypeVar("T")
    def get_or_set(self, key: Key, default: Callable[..., T]) -> T:
        ...