11/*
22 * Copyright (c) 2024-2025, redis-keeper ([email protected] ) 33 *
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
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
77 *
8- * http://www.apache.org/licenses/LICENSE-2.0
8+ * http://www.apache.org/licenses/LICENSE-2.0
99 *
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.
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.
1515 */
1616
1717package org .codeba .redis .keeper .spring .boot ;
2626import org .redisson .config .Config ;
2727import org .redisson .config .SentinelServersConfig ;
2828import org .redisson .config .SingleServerConfig ;
29+ import org .springframework .aop .scope .ScopedProxyUtils ;
2930import org .springframework .beans .factory .annotation .Autowired ;
3031import org .springframework .boot .autoconfigure .data .redis .RedisProperties ;
3132import org .springframework .boot .context .properties .EnableConfigurationProperties ;
3233import org .springframework .cloud .context .config .annotation .RefreshScope ;
34+ import org .springframework .cloud .context .scope .refresh .RefreshScopeRefreshedEvent ;
35+ import org .springframework .context .ApplicationContext ;
3336import org .springframework .context .annotation .Bean ;
3437import org .springframework .context .annotation .Configuration ;
38+ import org .springframework .context .event .EventListener ;
3539import org .springframework .util .ReflectionUtils ;
3640import org .springframework .util .StringUtils ;
3741
5660@ SuppressWarnings ({"unchecked" })
5761@ EnableConfigurationProperties ({RedisDatasourceProperties .class , RedissonDatasourceProperties .class })
5862public class RedisKeeperAutoConfiguration <T > {
63+ /**
64+ * The constant PROVIDER_BEAN_NAME.
65+ */
66+ private static final String PROVIDER_BEAN_NAME = "cacheTemplateProvider" ;
67+
68+ /**
69+ * The Context.
70+ */
71+ @ Autowired
72+ private ApplicationContext context ;
5973
74+ /**
75+ * The Cache datasource.
76+ */
6077 @ Autowired (required = false )
6178 private CacheDatasource <T > cacheDatasource ;
6279
80+ /**
81+ * The Redis properties.
82+ */
83+ @ Autowired
84+ private RedisDatasourceProperties redisProperties ;
85+
86+ /**
87+ * The Redisson properties.
88+ */
89+ @ Autowired
90+ private RedissonDatasourceProperties redissonProperties ;
91+
6392 /**
6493 * Cache template provider cache template provider.
6594 *
66- * @param redisProperties the redis properties
67- * @param redissonProperties the redisson properties
6895 * @return the cache template provider
6996 * @throws IOException the io exception
7097 */
71- @ Bean
98+ @ Bean ( name = RedisKeeperAutoConfiguration . PROVIDER_BEAN_NAME )
7299 @ RefreshScope
73- public CacheTemplateProvider <T > cacheTemplateProvider (RedisDatasourceProperties redisProperties , RedissonDatasourceProperties redissonProperties ) throws IOException {
100+ public CacheTemplateProvider <T > cacheTemplateProvider () throws IOException {
74101 final Map <String , T > loadMap = load (redisProperties , redissonProperties );
75- final Map <String , List <T >> loadsMap = loads (redisProperties , redissonProperties );
76-
77- return new CacheTemplateProvider <>(loadMap , loadsMap );
102+ final Map <String , List <T >> loadListMap = loads (redisProperties , redissonProperties );
103+ // clean
104+ cacheDatasource .clean ();
105+ return new CacheTemplateProvider <>(loadMap , loadListMap );
78106 }
79107
108+ /**
109+ * On refresh.
110+ */
111+ @ EventListener (RefreshScopeRefreshedEvent .class )
112+ public void onRefresh () {
113+ if (!redisProperties .isLazyRefresh () || !redissonProperties .isLazyRefresh ()) {
114+ this .context .getBean (ScopedProxyUtils .getTargetBeanName (PROVIDER_BEAN_NAME ));
115+ }
116+ }
80117
118+ /**
119+ * Load map.
120+ *
121+ * @param redisProperties the redis properties
122+ * @param redissonProperties the redisson properties
123+ * @return the map
124+ * @throws IOException the io exception
125+ */
81126 private Map <String , T > load (RedisDatasourceProperties redisProperties , RedissonDatasourceProperties redissonProperties ) throws IOException {
82127 if (null == cacheDatasource ) {
83128 cacheDatasource = (CacheDatasource <T >) new DefaultCacheDatasource ();
@@ -116,6 +161,13 @@ private Map<String, T> load(RedisDatasourceProperties redisProperties, RedissonD
116161 return cacheDatasource .initialize (map );
117162 }
118163
164+ /**
165+ * Loads map.
166+ *
167+ * @param redisProperties the redis properties
168+ * @param redissonProperties the redisson properties
169+ * @return the map
170+ */
119171 private Map <String , List <T >> loads (RedisDatasourceProperties redisProperties , RedissonDatasourceProperties redissonProperties ) {
120172 if (null == cacheDatasource ) {
121173 cacheDatasource = (CacheDatasource <T >) new DefaultCacheDatasource ();
@@ -175,6 +227,13 @@ private Map<String, List<T>> loads(RedisDatasourceProperties redisProperties, Re
175227 return cacheDatasource .initializeMulti (map );
176228 }
177229
230+ /**
231+ * Config config.
232+ *
233+ * @param redissonKeeperProperties the redisson keeper properties
234+ * @return the config
235+ * @throws IOException the io exception
236+ */
178237 private Config config (RedissonKeeperProperties redissonKeeperProperties ) throws IOException {
179238 Config result = null ;
180239 final String config = redissonKeeperProperties .getConfig ();
@@ -187,6 +246,13 @@ private Config config(RedissonKeeperProperties redissonKeeperProperties) throws
187246 return result ;
188247 }
189248
249+ /**
250+ * Config config.
251+ *
252+ * @param redisProperties the redis properties
253+ * @return the config
254+ * @throws IOException the io exception
255+ */
190256 private Config config (RedisProperties redisProperties ) throws IOException {
191257 Config config ;
192258 Method clusterMethod = ReflectionUtils .findMethod (RedisProperties .class , "getCluster" );
0 commit comments