Skip to content

Instantly share code, notes, and snippets.

@digvijaybhakuni
Created May 28, 2017 16:54
Show Gist options
  • Save digvijaybhakuni/c33dc5eefda6d7ab4e32c831ad20a9b2 to your computer and use it in GitHub Desktop.
Save digvijaybhakuni/c33dc5eefda6d7ab4e32c831ad20a9b2 to your computer and use it in GitHub Desktop.
JCache JSR107 Sample from Main Method
<dependencies>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>jcache</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
package com.dgstack.dev.jcache;
import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.spi.CachingProvider;
public class JCacheSample {
public static void main(String[] args) {
final CachingProvider cachingProvider = Caching.getCachingProvider();
System.out.println("cachingProvider = " + cachingProvider);
final CacheManager cacheMgr = cachingProvider.getCacheManager();
final MutableConfiguration<String, String> configuration = new MutableConfiguration<>();
configuration.setTypes(String.class, String.class);
cacheMgr.createCache("sampleCache", configuration);
Cache<String, String> sampleCache = cacheMgr.getCache("sampleCache", String.class, String.class);
sampleCache.put("name", "Digivjay");
sampleCache.putIfAbsent("name", "Digvijay Bhakuni");
sampleCache.put("jobs", "Developer");
sampleCache.forEach(e -> {
System.out.println("(KEY:" + e.getKey() + ")=>[VAL:"+ e.getValue()+"]");
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment