Skip to content

SanctumLabs/cachey

Repository files navigation

cachey

codecov Lint, Test & Build Codacy Badge Kotlin Version

A feather weight cache implementation in Kotlin

Usage

implementation("com.sanctumlabs:cachey:1.0.0")

with gradle

<dependency>
    <groupId>com.sanctumlabs</groupId>
    <artifactId>cachey</artifactId>
    <version>1.0.0</version>
</dependency>

with maven

Background

Caching is a critical technology in many high performance scalable applications. There are many choices in caching framework, including (but not limited to) Memcache , cache2k etc.

LRU Cache

LRU cache uses Least Recently Used strategy to evict the least recently used items. This keeps only a certain number of entries that are recently used and removes others.

We only keep maximumSize entries at most. Here we leverage the class java.util.LinkedHashMap to trace the usage of entries, and oldestKeyToRemove is the one to be removed, which is the eldest entry ordered by used frequency. Method cycleKeyMap is responsible for removing entries that are too old and less used. Simple and straightforward.

Expirable Cache

This uses a flushInterval, and will clear the cache every flushInterval milliseconds. It's typically a ** scheduled task**, we can use a background thread to do the task, but again, to make it simple, we just recycle before every operation in our cache.

Other Implementations

Besides the three implementations above, here are several implementations such as FIFOCache, SoftCache and WeakCache, implemented with First-in-first-out algorithm , Soft Reference, and Weak Reference respectively.