|
1 | 1 | package com.baeldung.cachetest.config;
|
2 | 2 |
|
3 |
| -import java.math.BigDecimal; |
4 |
| -import java.time.Duration; |
5 |
| - |
6 |
| -import javax.cache.CacheManager; |
7 |
| - |
8 |
| -import org.ehcache.config.CacheConfiguration; |
9 |
| -import org.ehcache.config.ResourcePools; |
10 |
| -import org.ehcache.config.builders.CacheConfigurationBuilder; |
11 |
| -import org.ehcache.config.builders.CacheEventListenerConfigurationBuilder; |
12 |
| -import org.ehcache.config.builders.ExpiryPolicyBuilder; |
13 |
| -import org.ehcache.config.builders.ResourcePoolsBuilder; |
14 |
| -import org.ehcache.config.units.EntryUnit; |
15 |
| -import org.ehcache.config.units.MemoryUnit; |
16 |
| -import org.ehcache.event.EventType; |
17 |
| -import org.ehcache.jsr107.Eh107Configuration; |
18 |
| -import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer; |
19 | 3 | import org.springframework.cache.annotation.EnableCaching;
|
20 |
| -import org.springframework.context.annotation.Bean; |
21 | 4 | import org.springframework.context.annotation.Configuration;
|
22 | 5 |
|
23 | 6 | @Configuration
|
24 | 7 | @EnableCaching
|
25 | 8 | public class CacheConfig {
|
26 |
| - |
27 |
| - private static final int ON_HEAP_CACHE_SIZE_ENTRIES = 2; |
28 |
| - private static final int OFF_HEAP_CACHE_SIZE_MB = 10; |
29 |
| - private static final int CACHE_EXPIRY_SECONDS = 30; |
30 |
| - |
31 |
| - @Bean |
32 |
| - public JCacheManagerCustomizer jcacheManagerCustomizer() { |
33 |
| - return new JCacheManagerCustomizer() { |
34 |
| - |
35 |
| - @Override |
36 |
| - public void customize(CacheManager cacheManager) { |
37 |
| - ResourcePools resourcePools = ResourcePoolsBuilder.newResourcePoolsBuilder() |
38 |
| - .heap(ON_HEAP_CACHE_SIZE_ENTRIES, EntryUnit.ENTRIES) |
39 |
| - .offheap(OFF_HEAP_CACHE_SIZE_MB, MemoryUnit.MB).build(); |
40 |
| - |
41 |
| - CacheEventListenerConfigurationBuilder eventLoggerConfig = CacheEventListenerConfigurationBuilder |
42 |
| - .newEventListenerConfiguration(new CacheEventLogger(), EventType.CREATED, EventType.EXPIRED) |
43 |
| - .unordered().asynchronous(); |
44 |
| - |
45 |
| - CacheConfiguration<?, ?> cacheConfiguration = CacheConfigurationBuilder |
46 |
| - .newCacheConfigurationBuilder(Long.class, BigDecimal.class, resourcePools) |
47 |
| - .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(CACHE_EXPIRY_SECONDS))) |
48 |
| - .add(eventLoggerConfig).build(); |
49 |
| - |
50 |
| - cacheManager.createCache("squareCache", |
51 |
| - Eh107Configuration.fromEhcacheCacheConfiguration(cacheConfiguration)); |
52 |
| - |
53 |
| - } |
54 |
| - }; |
55 |
| - } |
56 |
| - |
| 9 | + |
57 | 10 | }
|
0 commit comments