Kotlin Code Smell 33 - Caches

I've started to work as a software engineer at 2014, however, I started to write code at high-school.
My first language was Assembly, but still, I fall in love with the possibilities to make the computer to do as you wish, shortly after that I started to write in C.
Later on I studied a practical engineering in electricity, and during this time discovered that I preferred much more writing code than design electrical components.
As a result of this understanding I decided to switch and study bachelor degree in computer science in Reichman university, where the focus was of the Java language.
Today I'm working at SumUp using Kotlin, SpringBoot & Micronaut, Cassandra and Kafka
TL;DR - Caches discusses the problems with caching, such as coupling and maintainability, and proposes solutions like using an object mediator, testing invalidation scenarios, and modeling real-world cache metaphors. It concludes that caches should be functional, and intelligent, and domain objects shouldn't be cached.
Problem
Coupling
Testability
Cache Invalidation
Maintainability
Premature Optimization
Erratic Behavior
Lack of Transparency
Non-Deterministic Behavior
Solution
If you have a conclusive benchmark and are willing to accept some coupling, put an object in the middle.
Unit test all your invalidation scenarios. Experience shows that we face them in an incremental way.
Look for a real-world cache metaphor and model it.
Sample Code
Wrong
typealias Cache<T> = MutableMap<String, List<T>>
class Book(
private val cachedBooks: Cache<Book> = mutableMapOf()
) {
fun getBooks(title: String): List<Book> {
return if (cachedBooks.containsKey(title)) {
cachedBooks[title]!!
} else {
val booksFromDatabase = getBooksFromDatabase(title)
cachedBooks[title] = booksFromDatabase
booksFromDatabase
}
}
private fun getBooksFromDatabase(title: String): List<Book> =
globalDatabase().selectFrom("Books", "WHERE TITLE = $title")
}
Right
typealias Cache<T> = MutableMap<String, T>
interface BookRetriever {
fun bookByTitle(title: String): Book?
}
class Book {
// Just Book-related Stuff
}
class DatabaseLibrarian : BookRetriever {
// Go to the database (not global hopefully)
override fun bookByTitle(title: String): Book? { ... }
}
// We always look for real-life metaphors
class HotSpotLibrarian(
private val inbox: Inbox,
private val realRetriever: BookRetriever
) : BookRetriever {
override fun bookByTitle(title: String): Book? {
return if (inbox.includesTitle(title)) {
// We are lucky. Someone has just returned the book copy.
inbox.retrieveAndRemove(title)
} else {
realRetriever.bookByTitle(title)
}
}
}
class Inbox(private val books: Cache<Book> = mutableMapOf()) {
fun includesTitle(title: String) { ... }
fun retrieveAndRemove(title: String): Book? { ... }
fun addBook(title: String, book: Book) { ... }
}
Conclusion
Caches should be functional and intelligent, allowing for effective management of invalidation. General-purpose caches are best suited for low-level objects like operating systems, files, and streams, while domain objects should not be cached.




