diff --git a/MallChat b/MallChat deleted file mode 160000 index c47b487..0000000 --- a/MallChat +++ /dev/null @@ -1 +0,0 @@ -Subproject commit c47b48760dd1eaaed6cf1c62930c65032ed66752 diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/service/cache/AbstractLocalCache.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/service/cache/AbstractLocalCache.java new file mode 100644 index 0000000..28bceb5 --- /dev/null +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/service/cache/AbstractLocalCache.java @@ -0,0 +1,81 @@ +package com.abin.mallchat.common.common.service.cache; + +import com.github.benmanes.caffeine.cache.CacheLoader; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; +import com.google.common.collect.Iterables; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.lang.reflect.ParameterizedType; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +/** + * Description: redis string类型的批量缓存框架 + * Author: abin + * Date: 2023-06-10 + */ +public abstract class AbstractLocalCache implements BatchCache { + + private Class outClass; + private Class inClass; + private LoadingCache cache; + + protected AbstractLocalCache() { + init(60, 10 * 60, 1024); + } + + protected AbstractLocalCache(long refreshSeconds, long expireSeconds, int maxSize) { + init(refreshSeconds, expireSeconds, maxSize); + } + + private void init(long refreshSeconds, long expireSeconds, int maxSize) { + ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass(); + this.outClass = (Class) genericSuperclass.getActualTypeArguments()[1]; + this.inClass = (Class) genericSuperclass.getActualTypeArguments()[0]; + cache = Caffeine.newBuilder() + //自动刷新,不会阻塞线程,其他线程返回旧值 + .refreshAfterWrite(refreshSeconds, TimeUnit.SECONDS) + .expireAfterWrite(expireSeconds, TimeUnit.SECONDS) + .maximumSize(maxSize) + .build(new CacheLoader() { + @Nullable + @Override + public OUT load(@NonNull IN in) throws Exception { + return AbstractLocalCache.this.load(Collections.singletonList(in)).get(in); + } + + @Override + public @NonNull Map loadAll(@NonNull Iterable keys) throws Exception { + IN[] ins = Iterables.toArray(keys, inClass); + return AbstractLocalCache.this.load(Arrays.asList(ins)); + } + }); + } + + protected abstract Map load(List req); + + @Override + public OUT get(IN req) { + return cache.get(req); + } + + @Override + public Map getBatch(List req) { + return cache.getAll(req); + } + + @Override + public void delete(IN req) { + cache.invalidate(req); + } + + @Override + public void deleteBatch(List req) { + cache.invalidateAll(req); + } +} diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/service/cache/AbstractRedisStringCache.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/service/cache/AbstractRedisStringCache.java index 9f6b087..0614a8e 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/common/service/cache/AbstractRedisStringCache.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/service/cache/AbstractRedisStringCache.java @@ -64,4 +64,15 @@ public abstract class AbstractRedisStringCache implements BatchCache req) { + List keys = req.stream().map(this::getKey).collect(Collectors.toList()); + RedisUtils.del(keys); + } } diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/service/cache/BatchCache.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/service/cache/BatchCache.java index 1d97529..131dca9 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/common/service/cache/BatchCache.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/service/cache/BatchCache.java @@ -13,4 +13,14 @@ public interface BatchCache { * 获取批量 */ Map getBatch(List req); + + /** + * 修改删除单个 + */ + void delete(IN req); + + /** + * 修改删除多个 + */ + void deleteBatch(List req); } diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/utils/RedisUtils.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/utils/RedisUtils.java index 415c676..4d42b54 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/common/utils/RedisUtils.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/utils/RedisUtils.java @@ -199,6 +199,10 @@ public class RedisUtils { } } + public static void del(List keys) { + stringRedisTemplate.delete(keys); + } + // ============================String============================= /** diff --git a/mallchat-tools/mallchat-redis/pom.xml b/mallchat-tools/mallchat-redis/pom.xml new file mode 100644 index 0000000..82698a7 --- /dev/null +++ b/mallchat-tools/mallchat-redis/pom.xml @@ -0,0 +1,15 @@ + + + + mallchat-tools + com.abin.mallchat + 1.0-SNAPSHOT + + 4.0.0 + + mallchat-redis + + + \ No newline at end of file diff --git a/mallchat-tools/pom.xml b/mallchat-tools/pom.xml new file mode 100644 index 0000000..00afeb2 --- /dev/null +++ b/mallchat-tools/pom.xml @@ -0,0 +1,19 @@ + + + + mallchat + com.abin.mallchat + 1.0-SNAPSHOT + + 4.0.0 + + mallchat-tools + pom + + mallchat-redis + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 93913fa..e3b43bc 100644 --- a/pom.xml +++ b/pom.xml @@ -10,6 +10,7 @@ mallchat-common mallchat-custom-server + mallchat-tools pom