LRU Cache implementation in Java

A cache is an amount of faster memory used to improve data access by storing portions of a data set the whole of which is slower to access. Sometimes the total data set are not actually stored at all; instead, each data item is calculated as necessary, in which case the cache stores results from the calculations.

When we need a datum then first we check the cache if it contains the required datum. If it exists, the datum is used from the cache, without having to access the main data store. This is known as a ‘cache hit’. If the datum is not found in the cache, it is transferred from the main data store; this is known as a ‘cache miss’. When the cache fills up, items are ejected from the cache to make space for new items.

LRU is known as Least Recently Used where items are added to the cache as they are accessed; when the cache is full, the least recently used item is ejected. This type of cache is typically implemented as a linked list, so that an item in cache, when it is accessed again, can be moved back up to the head of the queue; items are ejected from the tail of the queue. Cache access overhead is again constant time. This algorithm is simple and fast, and it has a significant advantage over FIFO in being able to adapt somewhat to the data access pattern; frequently used items are less likely to be ejected from the cache. The main disadvantage is that it can still get filled up with items that are unlikely to be re-accessed soon; in particular, it can become useless in the face of scans over a larger number of items than fit in the cache. Nonetheless, this is by far the most frequently used caching algorithm.

This tutorial will show you how we can implement LRU cache using Java. For this tutorial we will create a standalone maven project in Eclipse.

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.

Sometimes we need to retrieve one or few records from the database. We can retrieve one or multiple records from database with the help of JdbcTemplate.

Prerequisites

The following configurations are required in order to run the application

Now we will see the below steps how to create a maven based spring project in Eclipse

Step 1. Create a standalone maven project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.

Step 3. If you see JRE System Library[J2SE-1.4] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.4], click on Edit button and select the appropriate jdk 1.7 from the next window. Click on Finish then Ok.

CacheCleaner scanning for expired objects...
CacheCleaner scanning for expired objects...
CacheCleaner Running. Found an expired object in the Cache : 6
CacheCleaner Running. Found an expired object in the Cache : 4
CacheCleaner Running. Found an expired object in the Cache : 2
CacheCleaner Running. Found an expired object in the Cache : 14
CacheCleaner Running. Found an expired object in the Cache : 12
CacheCleaner Running. Found an expired object in the Cache : 10
CacheCleaner Running. Found an expired object in the Cache : 8
CacheCleaner Running. Found an expired object in the Cache : 18
CacheCleaner Running. Found an expired object in the Cache : 16
CacheCleaner removed an expired object from the Cache : 6
CacheCleaner removed an expired object from the Cache : 4
CacheCleaner removed an expired object from the Cache : 2
CacheCleaner removed an expired object from the Cache : 14
CacheCleaner removed an expired object from the Cache : 12
CacheCleaner removed an expired object from the Cache : 10
CacheCleaner removed an expired object from the Cache : 8
CacheCleaner removed an expired object from the Cache : 18
CacheCleaner removed an expired object from the Cache : 16
CacheCleaner scanning for expired objects...