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

Cached kotlin suspend function drops last cache key parameter #518

Open
tPeltola opened this issue Oct 14, 2022 · 4 comments
Open

Cached kotlin suspend function drops last cache key parameter #518

tPeltola opened this issue Oct 14, 2022 · 4 comments
Labels
lang: kotlin Issues or features specific to Kotlin

Comments

@tPeltola
Copy link

Expected Behavior

When caching a kotlin suspend function with cache parameters explicitly defined, the result should be cached by all those parameters.

Actual Behaviour

The last parameter is dropped from cache key. (Note that it works if the parameters are not explicitly defined).

Steps To Reproduce

  1. Cached service method:
    @Cacheable(cacheNames = ["suspended"], parameters = ["first"])
    open suspend fun suspended(first: Int): Int {
            return first
    }
  1. Test for cache:
    "suspended service caches results by parameter" {
        service.suspended(1) shouldBe 1
        service.suspended(2) shouldBe 2
    }
  1. The second call returns 1 as the last (and only) parameter is dropped from the key

Environment Information

No response

Example Application

https://github.com/tPeltola/mncache

Version

3.7.1

@tPeltola
Copy link
Author

It seems to me that the issue is that KotlinSuspendFunCacheKeyGenerator drops the last parameter, but when they are explicitly defined in @Cacheable annotations the correct parameters are already set and it drops the last one of those.

I don't think KotlinSuspendFunCacheKeyGenerator has easy way to detect this situation. One option would be to check for the presence of Continuation parameter as the last parameter.

@tPeltola
Copy link
Author

Something like this commit seems to do the trick to make my test pass. I can create a PR, if this seems like a good approach to you.

@einsweniger
Copy link

Just because I stumbled into this right now: I suspect this was previously required to drop the last parameter of a kotlin suspension function, which is usually the "Continuation" parameter. That parameter in particular should not be counting towards the cache-key, but I guess something in core or the annotation-processing changed, such that the parameter is not included.
I'd try to fix this by checking if the last parameter is a continuation, and drop it, if that is the case. otherwise continue with all of the parameters.

@vargaslucas
Copy link

Hi team,
I was wondering if there's any effort to change this behavior of dropping the last key parameter.
Is this a recurring issue that you are currently facing, or is it considered an edge case and not a priority for the team at the moment?
It would be preferable not to have my own implementation of the cache module and to keep using the official one.

@sdelamo sdelamo added the lang: kotlin Issues or features specific to Kotlin label Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lang: kotlin Issues or features specific to Kotlin
Projects
Status: No status
Development

No branches or pull requests

4 participants