Caching with Kotlin Exposed?

Viewed 1723

I'm wondering how to add caching when using the Kotlin-Exposed library for SQL access.

For experimentation I've written a small application using both Spring Boot + Hibernate, and KTOR + Exposed.

I did some load-testing and when POSTing to both versions of the application, performance is quite similar with the KTOR + Exposed version having the edge.

However when GETting an existing record from both versions the difference is shocking especially when the database is getting larger - and all time is in Postgres. My conclusion is that the difference can only be in Hibernate second-level caching that Spring Boot configures.

Seeing the value of caching for items that are repeatedly queried in multiple transactions / sessions, I'm wondering how to configure this in into the low level Exposed framework?

1 Answers

At the moment Exposed supports only per-transaction level.

Also, there are ImmutableCachedEntityClass which allows you to define some entities (mostly dictionary-like) as cached and share them among application. You have to manage cache invalidation manually with expireCache() function or actualize entities with forceUpdateEntity.

Proper caching in the age of distributed systems is not so easy to implement. You may use any caching library (e.g. caffeine) and invalidate a cache if you know when your data changes (maybe with help of Exposed StatementInterceptors).

If you'll be able to implement a good caching solution feel free to send PR to the project.

Related