Created
May 28, 2017 16:54
-
-
Save digvijaybhakuni/c33dc5eefda6d7ab4e32c831ad20a9b2 to your computer and use it in GitHub Desktop.
JCache JSR107 Sample from Main Method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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