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

Pass cache result into ttl function #149

Closed
Rebzzel opened this issue Jun 5, 2023 · 5 comments
Closed

Pass cache result into ttl function #149

Rebzzel opened this issue Jun 5, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@Rebzzel
Copy link
Contributor

Rebzzel commented Jun 5, 2023

With new exceptions api (#143), this feature may be super useful.

def my_ttl_calculation(a: int, prev_cache_result: Any | None):
    if isinstance(prev_cache_result, MyException):
        return "5m"
    return "10s"
    

@cache(ttl=my_ttl_calculation)    
def my_func(a: int):
    ...
@Krukov Krukov added the enhancement New feature or request label Jun 9, 2023
@Krukov
Copy link
Owner

Krukov commented Jun 9, 2023

Thanks , I'll think about this feature

Am I right that prev_cache_result is a last result in cache ? in this case it is only possible with early ( https://github.com/Krukov/cashews#early ) or hit strategy (a cached value should be still in a storage)

@Rebzzel
Copy link
Contributor Author

Rebzzel commented Jun 10, 2023

Yes, that's right. But I have another better idea - we can calculate ttl after receiving result. Then this becomes really powerful.

@Rebzzel Rebzzel changed the title Pass previous cache result into ttl function Pass cache result into ttl function Jun 10, 2023
@Rebzzel
Copy link
Contributor Author

Rebzzel commented Jun 10, 2023

So this becomes:

def my_ttl_calculation(a: int, result: Any):
    if isinstance(result, MyException):
        return "5m"
    return "10s"
    

@cache(ttl=my_ttl_calculation)    
def my_func(a: int):
    ...

@Krukov
Copy link
Owner

Krukov commented Jun 10, 2023

yep I like it more then passing prev result. I'll definitely implement it

Krukov pushed a commit that referenced this issue Jul 21, 2023
Krukov added a commit that referenced this issue Jul 21, 2023
use a result of call in cache ttl def #149
@Krukov
Copy link
Owner

Krukov commented Sep 17, 2023

released 6.3.0

@Krukov Krukov closed this as completed Sep 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants