Created
April 11, 2020 17:53
-
-
Save itasyurt/d1d575c0daa24a4afe061bebdbb30842 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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