项目作者: ocafebabe

项目描述 :
JSR107的Google Guava Cache实现(JCACHE)
高级语言: Java
项目地址: git://github.com/ocafebabe/guava-jcache.git
创建时间: 2016-02-13T20:42:51Z
项目社区:https://github.com/ocafebabe/guava-jcache

开源协议:Apache License 2.0

下载


guava-jcache

This WIP shall be a full implementation of the API and SPI from JSR-107 (aka JCache). It provides a wrapper around a Google Guava cache that allows allows you to use Guava as the caching provider using only JSR-107 APIs.

Build Status
Maven Central
License

Usage

Development snapshots are available on Sonatype Nexus repository

  1. <repositories>
  2. <repository>
  3. <id>sonatype-nexus-snapshots</id>
  4. <name>Sonatype Nexus Snapshots</name>
  5. <url>https://oss.sonatype.org/content/repositories/snapshots</url>
  6. </repository>
  7. </repositories>
  8. <dependencies>
  9. <dependency>
  10. <groupId>ca.exprofesso</groupId>
  11. <artifactId>guava-jcache</artifactId>
  12. <version>1.0.5-SNAPSHOT</version>
  13. </dependency>
  14. </dependencies>

Example - Simple Cache

  1. MutableConfiguration<String, Integer> configuration = new MutableConfiguration<>();
  2. configuration.setStoreByValue(false);
  3. configuration.setTypes(String.class, Integer.class);
  4. CachingProvider cachingProvider = Caching.getCachingProvider(GuavaCachingProvider.class.getName());
  5. CacheManager cacheManager = cachingProvider.getCacheManager();
  6. Cache<String, Integer> cache = cacheManager.createCache("cache", configuration);
  7. cache.put("key", 1);
  8. Integer value = cache.get("key");

Example - Loading Cache

  1. final CacheLoader<String, Integer> cacheLoader = new CacheLoader<String, Integer>()
  2. {
  3. @Override
  4. public Integer load(String key)
  5. throws CacheLoaderException
  6. {
  7. // in a real application the value would probably come from a database...
  8. return Integer.valueOf(key);
  9. }
  10. @Override
  11. public Map<String, Integer> loadAll(Iterable<? extends String> keys)
  12. throws CacheLoaderException
  13. {
  14. Map<String, Integer> map = new HashMap<>();
  15. for (String key : keys)
  16. {
  17. // in a real application the value would probably come from a database...
  18. map.put(key, Integer.valueOf(key));
  19. }
  20. return map;
  21. }
  22. };
  23. MutableConfiguration<String, Integer> configuration = new MutableConfiguration<>();
  24. configuration.setStoreByValue(false);
  25. configuration.setTypes(String.class, Integer.class);
  26. custom.setReadThrough(true);
  27. custom.setCacheLoaderFactory
  28. (
  29. new Factory<CacheLoader<String, Integer>>()
  30. {
  31. @Override
  32. public CacheLoader<String, Integer> create()
  33. {
  34. return cacheLoader;
  35. }
  36. }
  37. );
  38. CachingProvider cachingProvider = Caching.getCachingProvider(GuavaCachingProvider.class.getName());
  39. CacheManager cacheManager = cachingProvider.getCacheManager();
  40. Cache<String, Integer> cache = cacheManager.createCache("cache", configuration);
  41. Integer value = cache.get("key");

Documentation

javax.cache (JSR107 API and SPI 1.0.0 API)

com.google.common.cache (Guava: Google Core Libraries for Java 28.1-jre API)