Configuring Caffeine parameters in code, is it possible?

Play uses Caffeine as the default cache and Caffeine supports a wide range of parameters. However, not all of the Caffeine parameters are configurable via Play config files. The supported parameters are listed in CaffeineParser.java. Other parameters are supposed to be “configured in code” according to the comment in the file. We would like to use refreshing in combination with expiring in our HTTP response cache to be able to always return cached responses for our most popular routes.

My question is: How should these parameters be configured in the code?

I assume we will need to override something from the relevant module, but I can’t seem to find any documentation or examples of how to do that.

Thank you for reading this far. Any help or suggestions will be much appreciated.

Hey @oehnstro,

Yeah, not all parameters are available to be configured, and that is not accidental. I’m not sure if this refreshing configuration, for example, will work with the expiry policy used by the cache. Maybe you want to use Caffeine directly?

Best.

Thanks for taking the time to answer!

According to the Caffeine documentation this should work well with their expiration policy. If you’re referring to some other expiration policy then I don’t know how that would work with this.

One option for us is to write our own Cached implementation, which should not be too difficult, but this looked like something that might just work out of the box if the cache was configured right. It seems we were a bit optimistic on that assumption.

Anyway, thank you for your answer :)

1 Like

Hey @oehnstro,

I’m not entirely opposed to adding new parameter if they make sense and work well with the existing ones. But in this case, I cannot see how to provide a refreshing function without exposing this feature in Play’s cache API, so yeah, it makes more sense to have this as a custom API.

But, there is nothing preventing you to extend play.api.cache.AsyncCacheApi that supports refreshing and that will work as expected with Cached. Does that sound reasonable to you?

Best.