Skip to content

Commit

Permalink
feat(cache): 增加缓存构建结果自定义处理
Browse files Browse the repository at this point in the history
  • Loading branch information
houkunlin committed Aug 3, 2024
1 parent 714fd6f commit 682f542
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/main/java/com/houkunlin/system/dict/starter/DictUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ public class DictUtil {
public DictUtil(final DictRegistrar dictRegistrar, final DictStore store, final DictCacheFactory cacheFactory) {
DictUtil.dictRegistrar = dictRegistrar;
DictUtil.store = store;
cache = cacheFactory.build();
missCache = cacheFactory.build();
cache = cacheFactory.build("dict-text");
missCache = cacheFactory.build("dict-number-of-miss");
missNum = cacheFactory.getDictProperties().getCache().getMissNum();
cacheFactory.callbackCache("dict-text", cache);
cacheFactory.callbackCache("dict-number-of-miss", missCache);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.houkunlin.system.dict.starter.cache;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

/**
Expand All @@ -14,5 +15,29 @@ public interface DictCacheCustomizer {
*
* @param caffeine 缓存对象构造
*/
void customize(final Caffeine<Object, Object> caffeine);
default void customize(final Caffeine<Object, Object> caffeine) {
}

/**
* 处理方法
*
* @param name 缓存名称
* @param caffeine 缓存对象构造
* @since 1.5.5
*/
default void customize(String name, final Caffeine<Object, Object> caffeine) {
customize(caffeine);
}

/**
* 回调构建成功的缓存对象。可在此方法中把缓存加入到 {@link org.springframework.cache.caffeine.CaffeineCacheManager#registerCustomCache(String, Cache)} 进行统一管理
*
* @param name 缓存名称
* @param cache 缓存对象
* @param <K> KEY
* @param <V> VALUE
* @since 1.5.5
*/
default <K, V> void callbackCache(String name, Cache<K, V> cache) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,21 @@ public class DictCacheFactory {
private final DictProperties dictProperties;
private final List<DictCacheCustomizer> cacheCustomizers;

public <K1, V1> Cache<K1, V1> build() {
@Deprecated
public <K, V> Cache<K, V> build() {
return build(null);
}

/**
* 构建缓存对象
*
* @param name 缓存名称
* @param <K> KEY
* @param <V> VALUE
* @return 缓存对象
* @since 1.5.5
*/
public <K, V> Cache<K, V> build(String name) {
final DictPropertiesCache propertiesCache = dictProperties.getCache();
if (!propertiesCache.isEnabled()) {
return null;
Expand All @@ -35,9 +49,24 @@ public <K1, V1> Cache<K1, V1> build() {
.initialCapacity(propertiesCache.getInitialCapacity());

for (final DictCacheCustomizer customizer : cacheCustomizers) {
customizer.customize(builder);
customizer.customize(name, builder);
}

return builder.build();
}

/**
* 回调构建成功的缓存对象
*
* @param name 缓存名称
* @param cache 缓存对象
* @param <K> KEY
* @param <V> VALUE
* @since 1.5.5
*/
public <K, V> void callbackCache(String name, Cache<K, V> cache) {
for (final DictCacheCustomizer customizer : cacheCustomizers) {
customizer.callbackCache(name, cache);
}
}
}

0 comments on commit 682f542

Please sign in to comment.