MemoryCache is a simple in-memory cache implementation in Kotlin. It uses a ConcurrentHashMap to store cache items and provides basic cache operations such as adding, retrieving, and removing items.
To use the MemoryCache in your project, you need to first create an instance of the MemoryCache class. You can optionally provide CacheOptions during the creation of the MemoryCache instance.
val cache = MemoryCache()You can add items to the cache using the add method. The add method requires a key and a value.
cache.add("key", "value")You can retrieve items from the cache using the get method. The get method requires a key and returns the value if found, null otherwise.
val value = cache.get("key")You can remove items from the cache using the remove method. The remove method requires a key.
cache.remove("key")You can get the count of items in the cache using the getCount method.
val count = cache.getCount()The MemoryCache class supports cache options which can be used to control the behavior of the cache. The CacheOptions class has two properties: absolutDuration and slidingDuration which can be used to set the expiration mode of the cache items.
val options = CacheOptions(2.seconds, null)
val cache = MemoryCache(options)In the above example, the absolutDuration is set to 2 seconds which means the cache items will expire after 2 seconds.
Contributions are welcome. Please feel free to submit a pull request.
This project is licensed under the MIT License.