Hi all, our kotlin app reads a lot of data from da...
# announcements
n
Hi all, our kotlin app reads a lot of data from database but does this in very inefficient way (no caching at all). Is there any library for kotlin which could be used as intelligent/smart cache between kotlin and DB ? By intelligent/smart I mean I could specify how much memory it can use at max and it will never go over this limit but if there are more data than cache can store it will purge some of them to not break the limit ? I'd like to avoid OOM-ing my app (and it will work in docker as well ...)
c
If its a Kotlin/JVM app, then Caffeine is a great choice for a high-performance cache https://github.com/ben-manes/caffeine
e
Not sure about pure kotlin implementations, but you could wrap your DB calls with Guava's
Cache
, should do the trick. Size-based eviction is supported and Guava has been around a long time. https://github.com/google/guava/wiki/CachesExplained
n
@Casey Brooks thanks ! How do I create SQL with caffeine ?
found some examples ...
c
Both Caffeine and Guava doesn’t work directly with SQL, you’d just wrap your DB calls with the cache
n
make sense ...
did anybody use both guava and caffeine ? any area where one is better than the other ?
c
Guava is a big library, with a lot of stuff you probably don’t need. However, it might already be on the classpath for your server framework, so you might check if you can just use it directly without adding the dependency. Caffeine is just the cache, so is a much smaller dependency if that’s all you need. I wouldn’t bring in all of Guava just for its cache I haven’t ever used the Guava cache to actually know for sure how it compares, but I would guess that Caffeine is all-around more performant and more configurable (since that is its single focus)
n
Ah thanks @Casey Brooks - really appreciate !
@Casey Brooks one more question: does caffeine cache one type of query ? Or can we configure caching over all queries?
c
A cache works mostly like a
HashMap
, which maps some “cache key” to a value, computing it if necessary. So you’d cache different queries using different cache keys for each query. If you want to configure the cache itself differently for each query, you’d probably have to create separate instances of the cache for each query.
The cache key could be the name of the method running the query, a hash of the SQL statement, some other identifier for the query you’re running etc. But you’d also want to include the parameters of the query in the cache key, so that if you run the query with different parameters, you don’t get cached results returned from a query with different parameters. Kotlin data classes make great cache keys, as opposed to Strings that are more common in a Map