Simple Java caching library or design pattern? [closed]
Asked Answered
S

5

14

I need to frequently access the result of a time-consuming calculation. The result changes infrequently, so I have to recalculate the data from time to time but it is ok to use the outdated result for a while. What would be the easiest way to do this and is there an existing library method or design pattern?

I am thinking of something like

private static List myCachedList = null;

...

// refresh list once in 3600 seconds
if (needsRefresh(myCachedList, 3600)) {
    // run the calculation
    myCachedList = ...
}
// use either updated or previous value from here on

A proper implementation might not be trivial, it might have to deal with thread safety, race conditions etc., so I would rather use a proven implementation than roll my own here.

Sybilla answered 31/10, 2012 at 13:5 Comment(0)
A
21

Congratulations for realising that writing your own can be more trouble it initially appears!

I would check out the Guava cache solution. Guava is a proven library and the caches are easily available (and configurable) via a fluent factory API.

All Guava caches, loading or not, support the method get(K, Callable<V>). This method returns the value associated with the key in the cache, or computes it from the specified Callable and adds it to the cache. No observable state associated with this cache is modified until loading completes. This method provides a simple substitute for the conventional "if cached, return; otherwise create, cache and return" pattern.

Aeroballistics answered 31/10, 2012 at 13:6 Comment(0)
W
3

I would take a look at Google guava-libraries. Much of this work has already been done for you.

There's specifically a section called Timed Eviction, which might be related to what you want. https://github.com/google/guava/wiki/CachesExplained#timed-eviction

Wastebasket answered 31/10, 2012 at 13:6 Comment(0)
D
3

I would suggest for you to use the Proxy Design Pattern that way you can encapsulate the caching logic-implementation in your proxy class

theres a cool example here that looks like to fit your needs

http://en.wikipedia.org/wiki/Proxy_pattern

Dispersal answered 31/10, 2012 at 13:8 Comment(0)
B
0

if you do not want to use third party library, simply you can create a static map which holds the key and value. using key you can retrieve the data fast.

and write methods to add values to cache, get, remove.

public SimpleCache{
    private static Map<String,Object> simpleCache = new HashMap<>();

    public static <T> T getValue(String key, Class type){

    //todo check contains here, if map doesn't contains key, throw not found exception

    Object val = simpleCache.get(key);

    return (T)val;    
  }
}

hope this helps

Blythe answered 28/2, 2017 at 11:16 Comment(1)
And where is the eviction? This cache will grow and grow until an OutOfMemoryError?Jacie
H
0

You can try SimplCache, it accepts implementation of Cache DB and persistent DB to provide an easy implementation for Cache system.

Hardy answered 12/7, 2020 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.