Scogun / ktor-simple-cache

Apache License 2.0
17 stars 1 forks source link

buildKey() behavior in SimpleCachePlugin #21

Open ShiinaKin opened 1 month ago

ShiinaKin commented 1 month ago

I have a question regarding the behavior of the buildKey method in SimpleCachePlugin.

https://github.com/Scogun/ktor-simple-cache/blob/4273e003a2fae93f50a78601607245632f065d50/ktor-simple-cache/src/main/kotlin/com/ucasoft/ktor/simpleCache/SimpleCachePlugin.kt#L37-L45

According to the comment in the README:

Cache key will be built only on listed query keys. Others will be ignored!

However, I noticed that even when there are no query parameters, key is still generated. Is this the expected behavior?

I think that the request should only be cached if all the queryKeys are present in the query parameters. If any of the queryKeys isn't present, the request should not be cached.

sample ex:

cacheOutput(6.hours, listOf("id")) { route("random") { ... } }

cache result:

image

should only cache the one which take the id param

Scogun commented 1 month ago

Hello @ShiinaKin!

Thank you for your request! Yeah, seems the documentation is not so clear.

Cache key will be built only on listed query keys. Others will be ignored!

sentence relates only the case when you specify the query keys. Otherwise, all query keys will be used.

ShiinaKin commented 1 month ago

I hope to add a flag to explicitly decide whether to use all query parameters or none when no specific query keys are listed. This flag could be a boolean that, when set to true, would not build a key construction if no specific query keys are defined.

I'd be glad to take a PR for it. Would you be open to that?

Scogun commented 1 month ago

To be honest, I don't see any reason for a new flag. The current one covers all cases:

Why in this case do we need the additionally turn on/off flag?

ShiinaKin commented 1 month ago

I have a /random endpoint with an optional id parameter to identify the resource for the random image request. The possible return results are either an array of bytes or a 302 redirect to a URL.

I want to return different random images when there is no id parameter, but return the same result for the same resource request with a cache in time.

Scogun commented 1 month ago

I see your point now. Maybe it can be solved via Regex Routing but I'm not 100% sure yet.

Scogun commented 1 month ago

Seems it works only for path and not for query.

ShiinaKin commented 1 month ago

yep, so I want to know your thought about it.

I've writing a implementation for it on https://github.com/ShiinaKin/ktor-simple-cache/tree/issue/21

Scogun commented 1 month ago

yep, so I want to know your thought about it.

I've writing a implementation for it on https://github.com/ShiinaKin/ktor-simple-cache/tree/issue/21

Yeah, it's one of possible solutions I'm thinking about. Let my compare all pros and cons. Should not take a long time.