org.redisson.RedissonClientSideCaching Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of redisson-all Show documentation
Show all versions of redisson-all Show documentation
Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC
/**
* 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.*;
import org.redisson.api.listener.TrackingListener;
import org.redisson.api.options.ClientSideCachingOptions;
import org.redisson.api.options.ClientSideCachingParams;
import org.redisson.cache.*;
import org.redisson.client.codec.Codec;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.pubsub.PublishSubscribeService;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
/**
*
* @author Nikita Koksharov
*
*/
public final class RedissonClientSideCaching implements RClientSideCaching {
Map cache;
final Map> name2cacheKey = new ConcurrentHashMap<>();
final CommandAsyncExecutor commandExecutor;
final int listenerId;
RedissonClientSideCaching(CommandAsyncExecutor commandExecutor, ClientSideCachingOptions options) {
ClientSideCachingParams params = (ClientSideCachingParams) options;
if (params.getEvictionPolicy() == ClientSideCachingOptions.EvictionPolicy.NONE) {
cache = new NoneCacheMap<>(params.getTtl(), params.getIdleTime());
}
if (params.getEvictionPolicy() == ClientSideCachingOptions.EvictionPolicy.LRU) {
cache = new LRUCacheMap<>(params.getSize(), params.getTtl(), params.getIdleTime());
}
if (params.getEvictionPolicy() == ClientSideCachingOptions.EvictionPolicy.LFU) {
cache = new LFUCacheMap<>(params.getSize(), params.getTtl(), params.getIdleTime());
}
if (params.getEvictionPolicy() == ClientSideCachingOptions.EvictionPolicy.SOFT) {
cache = ReferenceCacheMap.soft(params.getTtl(), params.getIdleTime());
}
if (params.getEvictionPolicy() == ClientSideCachingOptions.EvictionPolicy.WEAK) {
cache = ReferenceCacheMap.weak(params.getTtl(), params.getIdleTime());
}
CommandAsyncExecutor tracked = commandExecutor.copy(true);
this.commandExecutor = create(tracked, CommandAsyncExecutor.class);
PublishSubscribeService subscribeService = this.commandExecutor.getConnectionManager().getSubscribeService();
CompletableFuture r = subscribeService.subscribe(this.commandExecutor,
new TrackingListener() {
@Override
public void onChange(String name) {
Set keys = name2cacheKey.remove(name);
if (keys == null) {
return;
}
for (CacheKeyParams key : keys) {
cache.remove(key);
}
}
});
listenerId = r.join();
}
public T create(Object instance, Class clazz) {
InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (!method.getName().contains("read")) {
return method.invoke(instance, args);
}
String name = (String) Arrays.stream(args)
.filter(r -> r instanceof String)
.findFirst()
.orElse(null);
if (name == null) {
return method.invoke(instance, args);
}
CacheKeyParams key = new CacheKeyParams(args);
Set values = name2cacheKey.computeIfAbsent(name, v -> Collections.newSetFromMap(new ConcurrentHashMap<>()));
values.add(key);
return cache.computeIfAbsent(key, k -> {
try {
return method.invoke(instance, args);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new IllegalStateException(e);
}
});
}
};
return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz }, handler);
}
@Override
public RBucket getBucket(String name) {
return new RedissonBucket<>(commandExecutor, name);
}
@Override
public RBucket getBucket(String name, Codec codec) {
return new RedissonBucket<>(codec, commandExecutor, name);
}
@Override
public RStream getStream(String name) {
return new RedissonStream<>(commandExecutor, name);
}
@Override
public RStream getStream(String name, Codec codec) {
return new RedissonStream<>(codec, commandExecutor, name);
}
@Override
public RSet getSet(String name) {
return new RedissonSet<>(commandExecutor, name, null);
}
@Override
public RSet getSet(String name, Codec codec) {
return new RedissonSet<>(codec, commandExecutor, name, null);
}
@Override
public RMap getMap(String name) {
return new RedissonMap<>(commandExecutor, name, null, null, null);
}
@Override
public RMap getMap(String name, Codec codec) {
return new RedissonMap<>(codec, commandExecutor, name, null, null, null);
}
@Override
public RScoredSortedSet getScoredSortedSet(String name) {
return new RedissonScoredSortedSet<>(commandExecutor, name, null);
}
@Override
public RScoredSortedSet getScoredSortedSet(String name, Codec codec) {
return new RedissonScoredSortedSet<>(codec, commandExecutor, name, null);
}
@Override
public RList getList(String name) {
return new RedissonList<>(commandExecutor, name, null);
}
@Override
public RList getList(String name, Codec codec) {
return new RedissonList<>(codec, commandExecutor, name, null);
}
@Override
public RQueue getQueue(String name) {
return new RedissonQueue<>(commandExecutor, name, null);
}
@Override
public RQueue getQueue(String name, Codec codec) {
return new RedissonQueue<>(codec, commandExecutor, name, null);
}
@Override
public RDeque getDeque(String name) {
return new RedissonDeque<>(commandExecutor, name, null);
}
@Override
public RDeque getDeque(String name, Codec codec) {
return new RedissonDeque<>(codec, commandExecutor, name, null);
}
@Override
public RBlockingQueue getBlockingQueue(String name) {
return new RedissonBlockingQueue<>(commandExecutor, name, null);
}
@Override
public RBlockingQueue getBlockingQueue(String name, Codec codec) {
return new RedissonBlockingQueue<>(codec, commandExecutor, name, null);
}
@Override
public RBlockingDeque getBlockingDeque(String name) {
return new RedissonBlockingDeque<>(commandExecutor, name, null);
}
@Override
public RBlockingDeque getBlockingDeque(String name, Codec codec) {
return new RedissonBlockingDeque<>(codec, commandExecutor, name, null);
}
@Override
public RGeo getGeo(String name) {
return new RedissonGeo<>(commandExecutor, name, null);
}
@Override
public RGeo getGeo(String name, Codec codec) {
return new RedissonGeo<>(codec, commandExecutor, name, null);
}
@Override
public void destroy() {
PublishSubscribeService subscribeService = commandExecutor.getConnectionManager().getSubscribeService();
commandExecutor.get(subscribeService.removeFlushListenerAsync(listenerId));
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy