<dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.0.0</version> </dependency> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ehcache.org/v3" xmlns:jsr107="http://www.ehcache.org/v3/jsr107" xsi:schemaLocation=" http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.1.xsd http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.1.xsd"> <cache alias="myCache"> <resources> <heap unit="entries">100</heap> <offheap unit="MB">10</offheap> </resources> <expiry> <ttl unit="seconds">60</ttl> </expiry> </cache> </config> import org.ehcache.Cache; import org.ehcache.CacheManager; import org.ehcache.config.CacheConfiguration; import org.ehcache.config.builders.CacheConfigurationBuilder; import org.ehcache.config.builders.CacheManagerBuilder; import org.ehcache.config.units.EntryUnit; import org.ehcache.config.units.MemoryUnit; public class EhcacheExample { public static void main(String[] args) { CacheConfiguration<Long, String> cacheConfiguration = CacheConfigurationBuilder .newCacheConfigurationBuilder(Long.class, String.class, ResourcePoolsBuilder.heap(100)) .build(); CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .withCache("myCache", cacheConfiguration) .build(); cacheManager.init(); Cache<Long, String> cache = cacheManager.getCache("myCache", Long.class, String.class); cache.put(1L, "Hello, Ehcache!"); String value = cache.get(1L); System.out.println(value); cacheManager.close(); } }


上一篇:
下一篇:
切换中文