From 60c80c2213e10326241b8c1b780b21a176a25f17 Mon Sep 17 00:00:00 2001 From: QwQ-dev Date: Wed, 25 Dec 2024 12:38:33 +0800 Subject: [PATCH] feat: custom cache. --- cache/README.md | 19 +++++++++++++- .../cache/factory/CacheServiceFactory.java | 13 ++++++++++ .../service/custom/CustomCacheService.java | 25 +++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 cache/src/main/java/net/legacy/library/cache/service/custom/CustomCacheService.java diff --git a/cache/README.md b/cache/README.md index 8a0a648..62265e1 100644 --- a/cache/README.md +++ b/cache/README.md @@ -22,7 +22,7 @@ public class CacheLauncher { public static void main(String[] args) { Config config = new Config(); config.useSingleServer().setAddress("redis://127.0.0.1:6379"); - + /* * Redis cache example @@ -138,6 +138,23 @@ public class CacheLauncher { * we can directly use getCache() to operate these methods. */ caffeineCache.getCache().put(2, "hi"); + + + /* + * Custom cache example + * + * CacheServiceInterface, String> customCache + * - value type + * - impl cache type, e.g. Map + * + * CacheServiceFactory.createCustomCache(new ConcurrentHashMap<>()) + * - impl cache, e.g. ConcurrentHashMap<> + */ + CacheServiceInterface, String> customCache = + CacheServiceFactory.createCustomCache(new ConcurrentHashMap<>()); + + // get impl cache, we can use get, execute, and more method like other cache service + Map cache = customCache.getCache(); } } ``` \ No newline at end of file diff --git a/cache/src/main/java/net/legacy/library/cache/factory/CacheServiceFactory.java b/cache/src/main/java/net/legacy/library/cache/factory/CacheServiceFactory.java index 3d784b0..4b8d966 100644 --- a/cache/src/main/java/net/legacy/library/cache/factory/CacheServiceFactory.java +++ b/cache/src/main/java/net/legacy/library/cache/factory/CacheServiceFactory.java @@ -6,6 +6,7 @@ import net.legacy.library.cache.service.CacheServiceInterface; import net.legacy.library.cache.service.caffeine.CaffeineAsyncCacheService; import net.legacy.library.cache.service.caffeine.CaffeineCacheService; +import net.legacy.library.cache.service.custom.CustomCacheService; import net.legacy.library.cache.service.redis.RedisCacheService; import net.legacy.library.cache.service.redis.RedisCacheServiceInterface; import org.redisson.config.Config; @@ -91,4 +92,16 @@ public static CacheServiceInterface, V> createCaffeineAs public static CacheServiceInterface, V> createCaffeineAsyncCache() { return new CaffeineAsyncCacheService<>(); } + + /** + * Creates a {@link CustomCacheService} with the specified cache implementation. + * + * @param cache the underlying cache implementation + * @param the cache value type + * @param the cache implementation type + * @return a new {@link CustomCacheService} instance + */ + public static CacheServiceInterface createCustomCache(C cache) { + return new CustomCacheService<>(cache); + } } diff --git a/cache/src/main/java/net/legacy/library/cache/service/custom/CustomCacheService.java b/cache/src/main/java/net/legacy/library/cache/service/custom/CustomCacheService.java new file mode 100644 index 0000000..8bf8a5f --- /dev/null +++ b/cache/src/main/java/net/legacy/library/cache/service/custom/CustomCacheService.java @@ -0,0 +1,25 @@ +package net.legacy.library.cache.service.custom; + +import net.legacy.library.cache.service.AbstractCacheService; +import net.legacy.library.cache.service.CacheServiceInterface; + +/** + * CustomCacheService is a custom implementation of a cache service. + * + * @param the cache implementation type + * @param the cache value type + * @author qwq-dev + * @see AbstractCacheService + * @see CacheServiceInterface + * @since 2024-12-25 12:17 + */ +public class CustomCacheService extends AbstractCacheService implements CacheServiceInterface { + /** + * Constructs a new CustomCacheService with the specified cache implementation. + * + * @param cache the underlying cache implementation + */ + public CustomCacheService(C cache) { + super(cache); + } +} \ No newline at end of file