Skip to content

Instantly share code, notes, and snippets.

@jlandure
Created July 18, 2012 13:43
Show Gist options
  • Save jlandure/3136285 to your computer and use it in GitHub Desktop.
Save jlandure/3136285 to your computer and use it in GitHub Desktop.
TestPerfJCache.java
import java.util.concurrent.TimeUnit;
import javax.cache.Cache;
import javax.cache.CacheConfiguration.Duration;
import javax.cache.CacheConfiguration.ExpiryType;
import javax.cache.CacheException;
import javax.cache.Caching;
import org.perf4j.StopWatch;
public class TestPerfJCache {
public static final String KEY = "KEY";
public static final String VALUE = "VALUE";
public static void main(String[] args) throws CacheException {
StopWatch stopWatch = new StopWatch();
Cache<String, String> maps = Caching.getCacheManager().<String, String> createCacheBuilder("foo")//
.setExpiry(ExpiryType.ACCESSED, new Duration(TimeUnit.DAYS, 10))//
// .setStoreByValue(false)//
.build();
for (int i = 0; i < 1_000_000; i++) {
String key = KEY + i;
String value = VALUE + i;
System.out.println("get Object in memory :" + maps.get(key));
// set a new object
maps.put(key, value);
System.out.println("set Object in memory :");
System.out.println("Get Object after set :" + maps.get(key));
if (maps.get("KEY0") == null) {
System.out.println("STOPPING : no more object in cache!");
return;
}
}
stopWatch.stop();
System.out.println("OldFashion Elapsed Time: " + stopWatch.getElapsedTime());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment