Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

ExpiryFolder: Time Based Cache

DZone's Guide to

ExpiryFolder: Time Based Cache

· Performance Zone ·
Free Resource

SignalFx is the only real-time cloud monitoring platform for infrastructure, microservices, and applications. The platform collects metrics and traces across every component in your cloud environment, replacing traditional point tools with a single integrated solution that works across the stack.

I recently blogged about Top ten performance problems and their solutions. Here is a class that can help you when caching. This class is time based. Meaning that unless accessed, the entries will be removed after x minutes.
This can be useful when you want a session time-out for example.

How does it work?

You have to think about boxes and objects moving from boxes to boxes every tick until they reach the last box where they disappear.Let's say you have a time-out of 30 minutes with a tick every 30 seconds.
The ExpiryFolder will create 60 boxes.
Every objects created at about the same time are in the same box.
Every 30 seconds they move to the box n-1.
If an object is accessed, it moves to the top box.
When it reaches the box 0 and a tick is processed, the object is removed.

One of the big challenge is the synchronization. Here is the code:

 

Here is an example:

private final static int DURATION = 5000; //ms
private final static int PRECISION = 1000; //ms
private final static String NAME = "TestFolder";

ExpiryStrategy strategy = new ExpiryStrategy(DURATION, PRECISION);
ExpiryFolder folder = new ExpiryFolder(NAME, strategy);
final String KEY = "something";
final String VAL = "else";
folder.put(KEY, VAL);
assertEquals(1, folder.size());
assertEquals(VAL, folder.find(KEY)); // doesn't count as an access
assertEquals(VAL, folder.get(KEY));

Note that this code is already used on production for several years.

SignalFx is built on a massively scalable streaming architecture that applies advanced predictive analytics for real-time problem detection. With its NoSample™ distributed tracing capabilities, SignalFx reliably monitors all transactions across microservices, accurately identifying all anomalies. And through data-science-powered directed troubleshooting SignalFx guides the operator to find the root cause of issues in seconds.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}