Kotlin Code Smell 33 - Caches

Kotlin Code Smell 33 - Caches

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

  1. If you have a conclusive benchmark and are willing to accept some coupling, put an object in the middle.

  2. Unit test all your invalidation scenarios. Experience shows that we face them in an incremental way.

  3. 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.

Credits

Did you find this article valuable?

Support Yonatan Karp-Rudin by becoming a sponsor. Any amount is appreciated!