Skip to content

Instantly share code, notes, and snippets.

@itasyurt
Created April 11, 2020 17:53
Show Gist options
  • Save itasyurt/d1d575c0daa24a4afe061bebdbb30842 to your computer and use it in GitHub Desktop.
Save itasyurt/d1d575c0daa24a4afe061bebdbb30842 to your computer and use it in GitHub Desktop.
class ListCache(private val cacheName: String) : Cache {
override fun getName() = this.cacheName
private val cacheItems = mutableListOf<Pair<String, Any?>>()
override fun get(key: Any): Cache.ValueWrapper? {
val value = cacheItems.firstOrNull { it.first == key }?.second
return if (value != null) SimpleValueWrapper(value) else null
}
override fun <T : Any?> get(key: Any, type: Class<T>?): T? {
val value = cacheItems.firstOrNull { it.first == key }?.second
return value as T?
}
override fun <T : Any?> get(key: Any, valueLoader: Callable<T>): T? {
val value = cacheItems.firstOrNull { it.first == key }?.second
return if (value != null) value as T? else valueLoader.call()
}
override fun put(key: Any, value: Any?) {
cacheItems.add(Pair(key as String, value))
}
override fun evict(key: Any) {
cacheItems.removeIf { it.first == key }
}
override fun clear() {
cacheItems.clear()
}
override fun getNativeCache() = cacheItems
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment