Java Code Examples for org.ehcache.config.builders.CacheManagerConfiguration

Following code examples demonstrate how to use org.ehcache.config.builders.CacheManagerConfigurationfrom ehcache. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.ehcache.config.builders.CacheManagerConfigurationand various code implementation of this class.

	public void testThreeTiers() {
		ResourcePools resourcePools = ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES)
				.offheap(10, MemoryUnit.MB).disk(20, MemoryUnit.MB).build();

		CacheConfiguration<Long, String> cacheConfiguration = CacheConfigurationBuilder
				.newCacheConfigurationBuilder(Long.class, String.class, resourcePools).build();

		String location = "D:" + File.pathSeparator + "temp" + File.pathSeparator + "ehcache";
		CacheManagerConfiguration<PersistentCacheManager> cacheManagerConfig = CacheManagerBuilder
				.persistence(location);

		String alias = "threeTiers";
		CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().with(cacheManagerConfig)
				.withCache(alias, cacheConfiguration).build(true);

		Cache<Long, String> cache = cacheManager.getCache(alias, Long.class, String.class);

		String value = "hello cat";
		cache.put(1L, value);

		Assert.assertTrue(value.equals(cache.get(1L)));

		cacheManager.close();

	} 


	public void testDiskPersistence() {

		String location = "D:" + File.pathSeparator + "temp" + File.pathSeparator + "ehcache";

		String persistenceCacheAlias = "persistenceCache";

		CacheManagerConfiguration<PersistentCacheManager> cacheManagerConfiguration = CacheManagerBuilder
				.persistence(location);

		ResourcePools resourcePools = ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES)
				.disk(10, MemoryUnit.MB, true).build();

		CacheConfiguration<Long, String> configuration = CacheConfigurationBuilder
				.newCacheConfigurationBuilder(Long.class, String.class, resourcePools).build();

		PersistentCacheManager persistentCacheManager = CacheManagerBuilder.newCacheManagerBuilder()
				.with(cacheManagerConfiguration).withCache(persistenceCacheAlias, configuration).build(true);

		Cache<Long, String> cache = persistentCacheManager.getCache(persistenceCacheAlias, Long.class, String.class);

		String value = "good afternoon";
		cache.put(1L, value);

		Assert.assertTrue(value.equals(cache.get(1L)));

		cache.clear();

		Assert.assertTrue(!value.equals(cache.get(1L)));

		persistentCacheManager.close();

	} 

    private static void testDiskCache() {
        CacheManagerConfiguration<PersistentCacheManager> persistentManagerConfig = CacheManagerBuilder
                .persistence(new File("d:/tmp", "ehcache-test"));

        PersistentCacheManager persistentCacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                .with(persistentManagerConfig).build(true);

        ResourcePoolsBuilder resource = ResourcePoolsBuilder.newResourcePoolsBuilder().disk(100, MemoryUnit.MB, true);

        CacheConfiguration<String, UserInfo> config = CacheConfigurationBuilder
                .newCacheConfigurationBuilder(String.class, UserInfo.class, resource).build();
        Cache<String, UserInfo> cache = persistentCacheManager.createCache("userInfoCache",
                CacheConfigurationBuilder.newCacheConfigurationBuilder(config));
        cache.put("cache1", new UserInfo());

        persistentCacheManager.close();
    } 

    public static void main(String[] args) {
        CacheManagerConfiguration<PersistentCacheManager> persistentManagerConfig = CacheManagerBuilder
                .persistence(new File("g:\\", "ehcache-test"));
        PersistentCacheManager persistentCacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                .with(persistentManagerConfig).build();
        persistentCacheManager.init();
        ResourcePoolsBuilder resource = ResourcePoolsBuilder.newResourcePoolsBuilder()
                .heap(10, MemoryUnit.MB)
                .offheap(100, MemoryUnit.MB)
                .disk(500, MemoryUnit.MB, true);
        CacheConfiguration<Long, User> config = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, User.class, resource)
                .withExpiry(Expirations.timeToLiveExpiration(Duration.of(10, TimeUnit.SECONDS)))
                .build();
        Cache<Long, User> cache = persistentCacheManager.createCache("userInfoCache", CacheConfigurationBuilder.newCacheConfigurationBuilder(config));
        cache.put(10001L, new User("张三",  1));
        cache.put(10002L, new User("李四",  2));
        cache.put(10003L, new User("王五",  3));
        User user1 = new User();
        user1 = cache.get(10001L);
        User user2 = new User();
        user2 = cache.get(10002L);
        User user3 = new User();
        user3 = cache.get(10003L);
        System.out.println(user1 == null ? "获取失败,以为没有成功放入缓存,其实是堆溢出清除!!" : "成功的获取了缓存:" + "**************" + user1 + "***********");
        System.out.println("成功的获取了缓存:" + "**************" + user2 + "***********");
        System.out.println(user3 == null ? "获取失败,以为没有成功放入缓存,可能的原因为声明的heap值过小!!" : "成功的获取了缓存:" + "**************" + user3 + "***********");
        persistentCacheManager.close();
    } 

Advertisement
Javadoc
A configuration type that enables to further specify the type of CacheManager in a CacheManagerBuild

er. @see org.ehcache.PersistentCacheManager

Read More
Advertisement