From 2972b249bc55da88d2644aa30f78b5b0e20da532 Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Thu, 27 Jun 2024 10:52:51 +0300 Subject: [PATCH] Feature - RedissonSpringCacheNativeManager implemented --- .../org/redisson/MapCacheNativeWrapper.java | 21 +++ .../RedissonSpringCacheNativeManager.java | 146 ++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 redisson/src/main/java/org/redisson/spring/cache/RedissonSpringCacheNativeManager.java diff --git a/redisson/src/main/java/org/redisson/MapCacheNativeWrapper.java b/redisson/src/main/java/org/redisson/MapCacheNativeWrapper.java index 848184546..82f3a63af 100644 --- a/redisson/src/main/java/org/redisson/MapCacheNativeWrapper.java +++ b/redisson/src/main/java/org/redisson/MapCacheNativeWrapper.java @@ -15,6 +15,7 @@ */ package org.redisson; +import org.redisson.api.RFuture; import org.redisson.api.RMapCacheNative; import java.time.Duration; @@ -34,11 +35,26 @@ public class MapCacheNativeWrapper extends RedissonMapCache { return cache.get(key); } + @Override + public boolean fastPut(K key, V value) { + return cache.fastPut(key, value); + } + + @Override + public RFuture fastPutAsync(K key, V value, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit) { + return cache.fastPutAsync(key, value, Duration.ofMillis(ttlUnit.toMillis(ttl))); + } + @Override public boolean fastPut(K key, V value, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit) { return cache.fastPut(key, value, Duration.ofMillis(ttlUnit.toMillis(ttl))); } + @Override + public RFuture putIfAbsentAsync(K key, V value, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit) { + return cache.putIfAbsentAsync(key, value, Duration.ofMillis(ttlUnit.toMillis(ttl))); + } + @Override public V putIfAbsent(K key, V value, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit) { return cache.putIfAbsent(key, value, Duration.ofMillis(ttlUnit.toMillis(ttl))); @@ -54,6 +70,11 @@ public class MapCacheNativeWrapper extends RedissonMapCache { cache.clear(); } + @Override + public RFuture clearAsync() { + return cache.clearAsync(); + } + @Override public long fastRemove(K... keys) { return cache.fastRemove(keys); diff --git a/redisson/src/main/java/org/redisson/spring/cache/RedissonSpringCacheNativeManager.java b/redisson/src/main/java/org/redisson/spring/cache/RedissonSpringCacheNativeManager.java new file mode 100644 index 000000000..54e3d1276 --- /dev/null +++ b/redisson/src/main/java/org/redisson/spring/cache/RedissonSpringCacheNativeManager.java @@ -0,0 +1,146 @@ +/** + * Copyright (c) 2013-2024 Nikita Koksharov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.redisson.spring.cache; + +import org.redisson.MapCacheNativeWrapper; +import org.redisson.Redisson; +import org.redisson.api.RMap; +import org.redisson.api.RMapCache; +import org.redisson.api.RMapCacheNative; +import org.redisson.api.RedissonClient; +import org.redisson.client.codec.Codec; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.cache.CacheManager; +import org.springframework.context.ResourceLoaderAware; + +import java.util.Map; + +/** + * A {@link CacheManager} implementation + * backed by Redisson instance. + * + * @author Nikita Koksharov + * + */ +@SuppressWarnings("unchecked") +public class RedissonSpringCacheNativeManager extends RedissonSpringCacheManager implements CacheManager, ResourceLoaderAware, InitializingBean { + + /** + * Creates CacheManager supplied by Redisson instance + * + * @param redisson object + */ + public RedissonSpringCacheNativeManager(RedissonClient redisson) { + this(redisson, (String) null, null); + } + + /** + * Creates CacheManager supplied by Redisson instance and + * Cache config mapped by Cache name + * + * @param redisson object + * @param config object + */ + public RedissonSpringCacheNativeManager(RedissonClient redisson, Map config) { + this(redisson, config, null); + validateProps(); + } + + /** + * Creates CacheManager supplied by Redisson instance, Codec instance + * and Cache config mapped by Cache name. + *

+ * Each Cache instance share one Codec instance. + * + * @param redisson object + * @param config object + * @param codec object + */ + public RedissonSpringCacheNativeManager(RedissonClient redisson, Map config, Codec codec) { + super(redisson, config, codec); + validateProps(); + } + + /** + * Creates CacheManager supplied by Redisson instance + * and Cache config mapped by Cache name. + *

+ * Loads the config file from the class path, interpreting plain paths as class path resource names + * that include the package path (e.g. "mypackage/myresource.txt"). + * + * @param redisson object + * @param configLocation path + */ + public RedissonSpringCacheNativeManager(RedissonClient redisson, String configLocation) { + this(redisson, configLocation, null); + } + + /** + * Creates CacheManager supplied by Redisson instance, Codec instance + * and Config location path. + *

+ * Each Cache instance share one Codec instance. + *

+ * Loads the config file from the class path, interpreting plain paths as class path resource names + * that include the package path (e.g. "mypackage/myresource.txt"). + * + * @param redisson object + * @param configLocation path + * @param codec object + */ + public RedissonSpringCacheNativeManager(RedissonClient redisson, String configLocation, Codec codec) { + super(redisson, configLocation, codec); + } + + private void validateProps() { + for (CacheConfig value : configMap.values()) { + if (value.getMaxIdleTime() > 0) { + throw new UnsupportedOperationException("maxIdleTime isn't supported"); + } + if (value.getMaxSize() > 0) { + throw new UnsupportedOperationException("maxSize isn't supported"); + } + } + } + + @Override + public void setConfig(Map config) { + super.setConfig(config); + validateProps(); + } + + @Override + protected RMap getMap(String name, CacheConfig config) { + if (codec != null) { + return redisson.getMapCacheNative(name, codec); + } + return redisson.getMapCacheNative(name); + } + + @Override + protected RMapCache getMapCache(String name, CacheConfig config) { + RMapCacheNative map = (RMapCacheNative) getMap(name, config); + return new MapCacheNativeWrapper<>(map, (Redisson) redisson); + } + + @Override + public void afterPropertiesSet() throws Exception { + super.afterPropertiesSet(); + if (configLocation != null) { + validateProps(); + } + } +}