|
| 1 | +# Micronaut Redis Cache |
| 2 | + |
| 3 | +A Micronaut cache implementation using the Jedis Redis driver, providing drop-in compatibility with the official Micronaut Redis (Lettuce) cache module. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Works with Micronaut's `@Cacheable`, `@CachePut`, `@CacheInvalidate` annotations |
| 8 | +- Uses Jedis driver with JedisPool for connection pooling |
| 9 | +- Same configuration namespace as the official Lettuce implementation |
| 10 | +- Supports expiration policies (expire-after-write, expire-after-access) |
| 11 | +- Custom expiration policy support |
| 12 | +- Async cache operations via `AsyncCache` |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +Add the dependency to your `build.gradle`: |
| 17 | + |
| 18 | +```groovy |
| 19 | +dependencies { |
| 20 | + implementation 'io.seqera:micronaut-cache-redis:1.0.0' |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +## Configuration |
| 25 | + |
| 26 | +The module uses the same configuration as the official Micronaut Redis cache: |
| 27 | + |
| 28 | +```yaml |
| 29 | +redis: |
| 30 | + caches: |
| 31 | + my-cache: |
| 32 | + expire-after-write: 1h |
| 33 | + another-cache: |
| 34 | + expire-after-access: 30m |
| 35 | + invalidate-scan-count: 100 |
| 36 | +``` |
| 37 | +
|
| 38 | +### Configuration Options |
| 39 | +
|
| 40 | +| Property | Type | Description | |
| 41 | +|----------|------|-------------| |
| 42 | +| `expire-after-write` | Duration | TTL after writing a value | |
| 43 | +| `expire-after-access` | Duration | TTL after accessing a value (touch-based) | |
| 44 | +| `expiration-after-write-policy` | String | Custom policy class name | |
| 45 | +| `invalidate-scan-count` | Long | SCAN batch size for invalidateAll (default: 100) | |
| 46 | +| `key-serializer` | Class | Custom key serializer | |
| 47 | +| `value-serializer` | Class | Custom value serializer | |
| 48 | +| `charset` | Charset | Character encoding for keys | |
| 49 | + |
| 50 | +### Default Configuration |
| 51 | + |
| 52 | +You can set defaults for all caches: |
| 53 | + |
| 54 | +```yaml |
| 55 | +redis: |
| 56 | + cache: |
| 57 | + expire-after-write: 2h |
| 58 | + charset: UTF-8 |
| 59 | + caches: |
| 60 | + my-cache: |
| 61 | + # inherits defaults, can override |
| 62 | + expire-after-write: 30m |
| 63 | +``` |
| 64 | + |
| 65 | +## Usage |
| 66 | + |
| 67 | +### Provide a JedisPool Bean |
| 68 | + |
| 69 | +The module requires a `JedisPool` bean to be provided by your application: |
| 70 | + |
| 71 | +```java |
| 72 | +@Factory |
| 73 | +public class RedisFactory { |
| 74 | +
|
| 75 | + @Singleton |
| 76 | + public JedisPool jedisPool() { |
| 77 | + return new JedisPool("localhost", 6379); |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### Using with @Cacheable |
| 83 | + |
| 84 | +```java |
| 85 | +@Singleton |
| 86 | +public class UserService { |
| 87 | +
|
| 88 | + @Cacheable("users") |
| 89 | + public User findById(Long id) { |
| 90 | + // This will be cached |
| 91 | + return userRepository.findById(id); |
| 92 | + } |
| 93 | +
|
| 94 | + @CachePut("users") |
| 95 | + public User update(Long id, User user) { |
| 96 | + return userRepository.save(user); |
| 97 | + } |
| 98 | +
|
| 99 | + @CacheInvalidate("users") |
| 100 | + public void delete(Long id) { |
| 101 | + userRepository.deleteById(id); |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +### Programmatic Access |
| 107 | + |
| 108 | +```java |
| 109 | +@Singleton |
| 110 | +public class CacheService { |
| 111 | +
|
| 112 | + private final SyncCache<JedisPool> cache; |
| 113 | +
|
| 114 | + public CacheService(@Named("my-cache") SyncCache<JedisPool> cache) { |
| 115 | + this.cache = cache; |
| 116 | + } |
| 117 | +
|
| 118 | + public void example() { |
| 119 | + // Put |
| 120 | + cache.put("key", "value"); |
| 121 | +
|
| 122 | + // Get |
| 123 | + Optional<String> value = cache.get("key", String.class); |
| 124 | +
|
| 125 | + // Get with supplier |
| 126 | + String result = cache.get("key", String.class, () -> "default"); |
| 127 | +
|
| 128 | + // Put if absent |
| 129 | + Optional<String> existing = cache.putIfAbsent("key", "value"); |
| 130 | +
|
| 131 | + // Invalidate |
| 132 | + cache.invalidate("key"); |
| 133 | + cache.invalidateAll(); |
| 134 | +
|
| 135 | + // Async operations |
| 136 | + cache.async().get("key", String.class) |
| 137 | + .thenAccept(opt -> opt.ifPresent(System.out::println)); |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +### Custom Value Serializer |
| 143 | + |
| 144 | +The default serializer uses JDK serialization, which requires cached objects to implement `Serializable`. For objects that don't implement `Serializable` (e.g., generated DTOs), you can use a JSON-based serializer. |
| 145 | + |
| 146 | +Create a Jackson serializer: |
| 147 | + |
| 148 | +```java |
| 149 | +@Singleton |
| 150 | +public class JacksonObjectSerializer implements ObjectSerializer { |
| 151 | +
|
| 152 | + @Inject |
| 153 | + private ObjectMapper objectMapper; |
| 154 | +
|
| 155 | + @Override |
| 156 | + public Optional<byte[]> serialize(Object object) { |
| 157 | + if (object == null) { |
| 158 | + return Optional.empty(); |
| 159 | + } |
| 160 | + try { |
| 161 | + return Optional.of(objectMapper.writeValueAsBytes(object)); |
| 162 | + } catch (IOException e) { |
| 163 | + throw new RuntimeException("Failed to serialize object", e); |
| 164 | + } |
| 165 | + } |
| 166 | +
|
| 167 | + @Override |
| 168 | + public <T> Optional<T> deserialize(byte[] bytes, Class<T> requiredType) { |
| 169 | + if (bytes == null || bytes.length == 0) { |
| 170 | + return Optional.empty(); |
| 171 | + } |
| 172 | + try { |
| 173 | + return Optional.ofNullable(objectMapper.readValue(bytes, requiredType)); |
| 174 | + } catch (IOException e) { |
| 175 | + throw new RuntimeException("Failed to deserialize object", e); |
| 176 | + } |
| 177 | + } |
| 178 | +
|
| 179 | + @Override |
| 180 | + public <T> Optional<T> deserialize(byte[] bytes, Argument<T> requiredType) { |
| 181 | + if (bytes == null || bytes.length == 0) { |
| 182 | + return Optional.empty(); |
| 183 | + } |
| 184 | + try { |
| 185 | + return Optional.ofNullable(objectMapper.readValue(bytes, |
| 186 | + objectMapper.constructType(requiredType.asType()))); |
| 187 | + } catch (IOException e) { |
| 188 | + throw new RuntimeException("Failed to deserialize object", e); |
| 189 | + } |
| 190 | + } |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +Configure it: |
| 195 | + |
| 196 | +```yaml |
| 197 | +redis: |
| 198 | + caches: |
| 199 | + my-cache: |
| 200 | + value-serializer: com.example.JacksonObjectSerializer |
| 201 | +``` |
| 202 | + |
| 203 | +### Custom Expiration Policy |
| 204 | + |
| 205 | +Implement `ExpirationAfterWritePolicy` for dynamic TTL: |
| 206 | + |
| 207 | +```java |
| 208 | +@Singleton |
| 209 | +public class TypeBasedExpirationPolicy implements ExpirationAfterWritePolicy { |
| 210 | +
|
| 211 | + @Override |
| 212 | + public long getExpirationAfterWrite(Object value) { |
| 213 | + if (value instanceof TemporaryData) { |
| 214 | + return Duration.ofMinutes(5).toMillis(); |
| 215 | + } |
| 216 | + return Duration.ofHours(1).toMillis(); |
| 217 | + } |
| 218 | +} |
| 219 | +``` |
| 220 | + |
| 221 | +Configure it: |
| 222 | + |
| 223 | +```yaml |
| 224 | +redis: |
| 225 | + caches: |
| 226 | + my-cache: |
| 227 | + expiration-after-write-policy: com.example.TypeBasedExpirationPolicy |
| 228 | +``` |
| 229 | + |
| 230 | +## Migration from Lettuce |
| 231 | + |
| 232 | +This module is designed as a drop-in replacement for `micronaut-redis-lettuce` cache. To migrate: |
| 233 | + |
| 234 | +1. Replace the dependency |
| 235 | +2. Provide a `JedisPool` bean instead of Lettuce connection |
| 236 | +3. Configuration remains the same |
| 237 | + |
| 238 | +## License |
| 239 | + |
| 240 | +Apache License 2.0 |
0 commit comments