1 /*
2 * Copyright 2019 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.bremersee.dccon.config;
18
19 import lombok.extern.slf4j.Slf4j;
20 import org.springframework.cache.annotation.EnableCaching;
21 import org.springframework.context.annotation.Configuration;
22
23 /**
24 * The caching configuration.
25 *
26 * @author Christian Bremer
27 */
28 @Configuration
29 @EnableCaching
30 @Slf4j
31 public class CachingConfiguration {
32
33 /*
34 import java.io.Serializable;
35 import java.util.Map;
36 import java.util.concurrent.TimeUnit;
37 import javax.cache.configuration.FactoryBuilder;
38 import javax.cache.configuration.MutableCacheEntryListenerConfiguration;
39 import javax.cache.configuration.MutableConfiguration;
40 import javax.cache.event.CacheEntryCreatedListener;
41 import javax.cache.event.CacheEntryEvent;
42 import javax.cache.event.CacheEntryEventFilter;
43 import javax.cache.event.CacheEntryExpiredListener;
44 import javax.cache.event.CacheEntryListener;
45 import javax.cache.event.CacheEntryListenerException;
46 import javax.cache.event.CacheEntryRemovedListener;
47 import javax.cache.event.CacheEntryUpdatedListener;
48 import javax.cache.expiry.CreatedExpiryPolicy;
49 import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
50 import org.springframework.context.annotation.Bean;
51
52 @Bean
53 public JCacheManagerCustomizer cacheManagerCustomizer() {
54 return cacheManager -> {
55 final TimeUnit durationUnit = TimeUnit.SECONDS;
56 if (cacheManager.getCache("dhcp-leases-by-ip") == null) {
57 log.info("msg=[Creating cache 'dhcp-leases-by-ip']");
58 cacheManager.createCache(
59 "dhcp-leases-by-ip",
60 new MutableConfiguration<Object, Map<String, DhcpLease>>()
61 .setExpiryPolicyFactory(CreatedExpiryPolicy
62 .factoryOf(new javax.cache.expiry.Duration(durationUnit, 30L)))
63 .setStoreByValue(false)
64 .setStatisticsEnabled(true)
65 .addCacheEntryListenerConfiguration(cacheEntryListenerConfiguration())
66 );
67 }
68 if (cacheManager.getCache("dhcp-leases-by-name") == null) {
69 log.info("msg=[Creating cache 'dhcp-leases-by-name']");
70 cacheManager.createCache(
71 "dhcp-leases-by-name",
72 new MutableConfiguration<Object, Map<String, DhcpLease>>()
73 .setExpiryPolicyFactory(CreatedExpiryPolicy
74 .factoryOf(new javax.cache.expiry.Duration(durationUnit, 30L)))
75 .setStoreByValue(false)
76 .setStatisticsEnabled(true)
77 .addCacheEntryListenerConfiguration(cacheEntryListenerConfiguration())
78 );
79 }
80 };
81 }
82
83 @Bean
84 public MutableCacheEntryListenerConfiguration<Object, Map<String, DhcpLease>> cacheEntryListenerConfiguration() {
85 return new MutableCacheEntryListenerConfiguration<>(
86 FactoryBuilder.factoryOf(new DhcpLeaseCacheEntryListener()),
87 FactoryBuilder.factoryOf(new DhcpLeaseCacheEntryEventFilter()),
88 false, true);
89 }
90
91 @Slf4j
92 static class DhcpLeaseCacheEntryEventFilter
93 implements Serializable, CacheEntryEventFilter<Object, Map<String, DhcpLease>> {
94
95 @Override
96 public boolean evaluate(CacheEntryEvent<?, ? extends Map<String, DhcpLease>> cacheEntryEvent)
97 throws CacheEntryListenerException {
98 log.info("msg=[Evaluate cache entry event] eventType=[{}]", cacheEntryEvent.getEventType());
99 return true;
100 }
101 }
102
103 @Slf4j
104 static class DhcpLeaseCacheEntryListener implements
105 Serializable,
106 CacheEntryListener<Object, Map<String, DhcpLease>>,
107 CacheEntryCreatedListener<Object, Map<String, DhcpLease>>,
108 CacheEntryUpdatedListener<Object, Map<String, DhcpLease>>,
109 CacheEntryRemovedListener<Object, Map<String, DhcpLease>>,
110 CacheEntryExpiredListener<Object, Map<String, DhcpLease>> {
111
112 @Override
113 public void onCreated(Iterable<CacheEntryEvent<?, ? extends Map<String, DhcpLease>>> iterable)
114 throws CacheEntryListenerException {
115 iterable.iterator().forEachRemaining(cacheEntryEvent -> log
116 .info("msg=[Dhcp lease cache created.] values=[{}[", cacheEntryEvent.getValue()));
117 }
118
119 @Override
120 public void onUpdated(Iterable<CacheEntryEvent<?, ? extends Map<String, DhcpLease>>> iterable)
121 throws CacheEntryListenerException {
122 iterable.iterator().forEachRemaining(cacheEntryEvent -> log
123 .info("msg=[Dhcp lease cache updated.] values=[{}[", cacheEntryEvent.getValue()));
124 }
125
126 @Override
127 public void onRemoved(Iterable<CacheEntryEvent<?, ? extends Map<String, DhcpLease>>> iterable)
128 throws CacheEntryListenerException {
129 iterable.iterator().forEachRemaining(cacheEntryEvent -> log
130 .info("msg=[Dhcp lease cache removed.] values=[{}[", cacheEntryEvent.getValue()));
131 }
132
133 @Override
134 public void onExpired(Iterable<CacheEntryEvent<?, ? extends Map<String, DhcpLease>>> iterable)
135 throws CacheEntryListenerException {
136 iterable.iterator().forEachRemaining(cacheEntryEvent -> log
137 .info("msg=[Dhcp lease cache expired.] values=[{}[", cacheEntryEvent.getValue()));
138 }
139
140 }
141 */
142
143 }