From b4c96722b0a6f52c13e6976dc59c37fa06cde558 Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Thu, 27 Jun 2024 10:13:31 +0300 Subject: [PATCH] Feature - RedissonCacheNative implementation added for Mybatis cache. --- .../org/redisson/mybatis/RedissonCache.java | 2 +- .../redisson/mybatis/RedissonCacheNative.java | 51 ++++++++++++++ .../org/redisson/MapCacheNativeWrapper.java | 66 +++++++++++++++++++ 3 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 redisson-mybatis/src/main/java/org/redisson/mybatis/RedissonCacheNative.java create mode 100644 redisson/src/main/java/org/redisson/MapCacheNativeWrapper.java diff --git a/redisson-mybatis/src/main/java/org/redisson/mybatis/RedissonCache.java b/redisson-mybatis/src/main/java/org/redisson/mybatis/RedissonCache.java index e65924111..2b43fe638 100644 --- a/redisson-mybatis/src/main/java/org/redisson/mybatis/RedissonCache.java +++ b/redisson-mybatis/src/main/java/org/redisson/mybatis/RedissonCache.java @@ -52,7 +52,7 @@ public class RedissonCache implements Cache { @Override public void putObject(Object o, Object o1) { check(); - mapCache.put(o, o1, timeToLive, TimeUnit.MILLISECONDS, maxIdleTime, TimeUnit.MILLISECONDS); + mapCache.fastPut(o, o1, timeToLive, TimeUnit.MILLISECONDS, maxIdleTime, TimeUnit.MILLISECONDS); } @Override diff --git a/redisson-mybatis/src/main/java/org/redisson/mybatis/RedissonCacheNative.java b/redisson-mybatis/src/main/java/org/redisson/mybatis/RedissonCacheNative.java new file mode 100644 index 000000000..07686a986 --- /dev/null +++ b/redisson-mybatis/src/main/java/org/redisson/mybatis/RedissonCacheNative.java @@ -0,0 +1,51 @@ +/** + * 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.mybatis; + +import org.redisson.MapCacheNativeWrapper; +import org.redisson.Redisson; +import org.redisson.api.RMapCache; +import org.redisson.api.RMapCacheNative; +import org.redisson.api.RedissonClient; + +/** + * + * @author Nikita Koksharov + * + */ +public class RedissonCacheNative extends RedissonCache { + + public RedissonCacheNative(String id) { + super(id); + } + + @Override + public void setMaxIdleTime(long maxIdleTime) { + throw new IllegalArgumentException("maxIdleTime setting isn't supported"); + } + + @Override + public void setMaxSize(int maxSize) { + throw new IllegalArgumentException("maxSize setting isn't supported"); + } + + @Override + protected RMapCache getMapCache(String id, RedissonClient redisson) { + RMapCacheNative cache = redisson.getMapCacheNative(id); + return new MapCacheNativeWrapper<>(cache, (Redisson) redisson); + } + +} diff --git a/redisson/src/main/java/org/redisson/MapCacheNativeWrapper.java b/redisson/src/main/java/org/redisson/MapCacheNativeWrapper.java new file mode 100644 index 000000000..848184546 --- /dev/null +++ b/redisson/src/main/java/org/redisson/MapCacheNativeWrapper.java @@ -0,0 +1,66 @@ +/** + * 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; + +import org.redisson.api.RMapCacheNative; + +import java.time.Duration; +import java.util.concurrent.TimeUnit; + +public class MapCacheNativeWrapper extends RedissonMapCache { + + private final RMapCacheNative cache; + + public MapCacheNativeWrapper(RMapCacheNative cache, Redisson redisson) { + super(null, redisson.getCommandExecutor(), "", null, null, null); + this.cache = cache; + } + + @Override + public V getWithTTLOnly(K key) { + return cache.get(key); + } + + @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 V putIfAbsent(K key, V value, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit) { + return cache.putIfAbsent(key, value, Duration.ofMillis(ttlUnit.toMillis(ttl))); + } + + @Override + public boolean containsKey(Object key) { + return cache.containsKey(key); + } + + @Override + public void clear() { + cache.clear(); + } + + @Override + public long fastRemove(K... keys) { + return cache.fastRemove(keys); + } + + @Override + public void destroy() { + cache.destroy(); + } +}