Skip to content

Commit

Permalink
feat: custom cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
QwQ-dev committed Dec 25, 2024
1 parent 799a78f commit 60c80c2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
19 changes: 18 additions & 1 deletion cache/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Map<Integer, String>, String> customCache
* - value type
* - impl cache type, e.g. Map<Integer, String>
*
* CacheServiceFactory.createCustomCache(new ConcurrentHashMap<>())
* - impl cache, e.g. ConcurrentHashMap<>
*/
CacheServiceInterface<Map<Integer, String>, String> customCache =
CacheServiceFactory.createCustomCache(new ConcurrentHashMap<>());

// get impl cache, we can use get, execute, and more method like other cache service
Map<Integer, String> cache = customCache.getCache();
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -91,4 +92,16 @@ public static <K, V> CacheServiceInterface<AsyncCache<K, V>, V> createCaffeineAs
public static <K, V> CacheServiceInterface<AsyncCache<K, V>, V> createCaffeineAsyncCache() {
return new CaffeineAsyncCacheService<>();
}

/**
* Creates a {@link CustomCacheService} with the specified cache implementation.
*
* @param cache the underlying cache implementation
* @param <V> the cache value type
* @param <C> the cache implementation type
* @return a new {@link CustomCacheService} instance
*/
public static <C, V> CacheServiceInterface<C, V> createCustomCache(C cache) {
return new CustomCacheService<>(cache);
}
}
Original file line number Diff line number Diff line change
@@ -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 <C> the cache implementation type
* @param <V> the cache value type
* @author qwq-dev
* @see AbstractCacheService
* @see CacheServiceInterface
* @since 2024-12-25 12:17
*/
public class CustomCacheService<C, V> extends AbstractCacheService<C, V> implements CacheServiceInterface<C, V> {
/**
* Constructs a new CustomCacheService with the specified cache implementation.
*
* @param cache the underlying cache implementation
*/
public CustomCacheService(C cache) {
super(cache);
}
}

0 comments on commit 60c80c2

Please sign in to comment.