Simply put, you are aiming to hold the data in memory and return that instead of reading from the persisted store.
As an example it is something like this:
public Content getContent(Integer id)
{
Content c;
if (cache.containsKey(id))
{
c = cache.get(id);
}
else
{
c = queryContent(id);
cache.put(id, c);
}
return c;
}
private Content queryContent(Integer id)
{
return contentFromDb;
}
This however has a problem. You eventually have EVERYTHING in memory. If you are considering a large amount of data then you'll need to consider flushing the cache. Normally you'd do this in a separate thread just going through all the items in the cache and either removing the oldest ones or reducing the size to a particular limit.
Either of these approaches need the content in the cache to be stamped, I use age and requests where I have implemented this with a formula that keeps anything requested if on average it is being called more than a given number of minutes:
class Cache
{
void flushCache(int maxMinutes)
{
List<integer> remove = HashList<>();
for (Content c : cache.values())
{
if (c.isActive(maxMinites))
{
remove.add(c.getId());
}
}
for (Integer i : remove)
{
cache.remove(i);
}
}
}
class Content
{
bool isActive((int maxMinutes)
{
if ((age / calls) <= maxMinutes)
{
return true;
}
return false;
}</integer>
Now, when the content is no longer not in use it is slowly removed.