A flexible multi-level caching framework that integrates Caffeine and Redis, featuring comprehensive lock mechanisms and functional programming support.
-
🔄 Flexible Cache Implementations
- Caffeine-based in-memory caching (
CaffeineCacheService
) - Redis-based distributed caching (
RedisCacheService
) - Custom cache support (
CustomCacheService
) - Multi-level caching (
FlexibleMultiLevelCacheService
)
- Caffeine-based in-memory caching (
-
🔒 Comprehensive Lock Mechanism
- Abstract lock handling via
AbstractLockableCache
- Support for both ReentrantLock and Redisson RLock
- Configurable lock timeouts and retry policies
- Thread-safe operations with
LockSettings
- Abstract lock handling via
-
🎯 Type-Safe Operations
- Generic cache interfaces for type safety
- Functional programming style API
- Explicit type conversion support in Redis operations
- Type-safe multi-level cache access
-
⚡ Performance Optimizations
- Lazy loading capabilities
- Configurable cache expiration
- Efficient multi-level cache lookups
- Thread-safe concurrent operations
dependencies {
implementation("net.legacy.library:cache:1.0-SNAPSHOT")
}
1️⃣ Caffeine Cache (In-Memory)
CacheServiceInterface<Cache<String, User>, User> caffeineCache =
CacheServiceFactory.createCaffeineCache();
User user = caffeineCache.get(
cache -> cache.getIfPresent("user1"),
() -> new User("user1"),
(cache, value) -> cache.put("user1", value),
true
);
2️⃣ Redis Cache (Distributed)
Config config = new Config();
config.useSingleServer().setAddress("redis://localhost:6379");
RedisCacheServiceInterface redisCache =
CacheServiceFactory.createRedisCache(config);
User user = redisCache.getWithType(
client -> client.getBucket("user1").get(),
() -> new User("user1"),
(client, value) -> client.getBucket("user1").set(value),
true
);
3️⃣ Multi-Level Cache
Set<TieredCacheLevel<?, ?>> tiers = Set.of(
TieredCacheLevel.of("L1", caffeineCache.getCache()),
TieredCacheLevel.of("L2", redisCache.getCache())
);
FlexibleMultiLevelCacheService multiLevelCache =
CacheServiceFactory.createFlexibleMultiLevelCacheService(tiers);
CaffeineCacheService
: In-memory caching with CaffeineRedisCacheService
: Distributed caching with RedisCustomCacheService
: Support for custom cache implementationsFlexibleMultiLevelCacheService
: Multi-level cache orchestration
AbstractLockableCache
: Base class for lock-enabled cachesLockSettings
: Configuration for lock behaviorLockableCacheInterface
: Core locking operations
CacheItem
: Wrapper for cached values with expirationExpirationSettings
: TTL configurationTieredCacheLevel
: Multi-level cache tier definition
LockSettings lockSettings = LockSettings.of(1000, 500, TimeUnit.MILLISECONDS);
User user = caffeineCache.get(
cache -> cache.asMap().get("key"),
() -> new User("key"),
(cache, value) -> cache.put("key", value),
true,
lockSettings
);
Map<String, User> customMap = new ConcurrentHashMap<>();
CacheServiceInterface<Map<String, User>, User> customCache =
CacheServiceFactory.createCustomCache(customMap);
User l1User = multiLevelCache.applyFunctionWithoutLock(
"L1",
cache -> ((Cache<String, User>) cache).getIfPresent("user1")
);
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
Made with ❤️ by LegacyLands Team