How to implement cache in Java
According to wikipedia, a cache is a hardware or software component that stores data so that future requests for that data can be served faster.
Benefits:
- Faster access of data in O(1)
- Computation complexity once for the first time
Types:
- Memory cache
- Database cache
- Disk cache, etc
In this post, we’ll go through the steps to create a in memory cache in java.
To create a cache, we can simply use a map / dictionary data structure and we can get the expected result of O(1) for both get and put operation.
But, we can’t store everything in our cache. We have storage and performance limits.
A cache eviction algorithm is a way of deciding which element to evict when the cache is full. To gain optimized benefits there are many algorithms for different use cases.
- Least Recently Used (LRU)
- Least Frequently Used (LFU)
- First In First Out (FIFO)
- Last In First Out (LIFO) etc.
There are only two hard things in computer science, Cache invalidation and naming things. — Phil Karlton
In our design, we will use
- HashMap to get and put data in O(1)
- Doubly linked list