Skip to main content

Command Palette

Search for a command to run...

Kotlin Code Smell 33 - Caches

Updated
2 min read
Kotlin Code Smell 33 - Caches
Y

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

  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

Kotlin Code Smells

Part 4 of 36

In this series, we will see several symptoms and situations that make us doubt the quality of our development. We will present possible solutions. Most are just clues. They are no hard rules.

Up next

Kotlin Code Smell 32 - Repeated Code

Breaking the Chains of Duplication for Elegant and Maintainable Code

More from this blog

Yonatan Karp-Rudin | kotlin for backend developer skills | java for backend developer skills | SpringBoot | Tutorials

57 posts

Experienced Senior Software Engineer passionate about functional programming & Kotlin. Excels in app development, optimization, and team collaboration. Let's create something amazing!