All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.redisson.cache.LocalCacheView Maven / Gradle / Ivy

Go to download

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

There is a newer version: 3.40.2
Show newest version
/**
 * 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.cache;

import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.redisson.RedissonObject;
import org.redisson.api.LocalCachedMapOptions;
import org.redisson.misc.Hash;

import io.netty.buffer.ByteBuf;

/**
 * 
 * @author Nikita Koksharov
 *
 * @param  key type
 * @param  value type
 */
public class LocalCacheView {

    private final RedissonObject object;
    private final ConcurrentMap cache;
    private final ConcurrentMap cacheKeyMap;
    private final boolean useObjectAsCacheKey;

    public LocalCacheView(LocalCachedMapOptions options, RedissonObject object) {
        this.cache = createCache(options);
        this.object = object;
        this.cacheKeyMap = createCache(options);
        this.useObjectAsCacheKey = options.isUseObjectAsCacheKey();
    }

    public Set cachedKeySet() {
        return new LocalKeySet();
    }

    class LocalKeySet extends AbstractSet {

        @Override
        public Iterator iterator() {
            return new Iterator() {

                private Iterator iter = cache.values().iterator();
                
                @Override
                public boolean hasNext() {
                    return iter.hasNext();
                }

                @Override
                public K next() {
                    return (K) iter.next().getKey();
                }
                
                @Override
                public void remove() {
                    if (useObjectAsCacheKey) {
                        cacheKeyMap.remove(((AbstractCacheMap.MapIterator) iter).cursorValue().getKey());
                    }
                    iter.remove();
                }
            };
        }

        @Override
        public boolean contains(Object o) {
            CacheKey cacheKey = toCacheKey(o);
            return cache.containsKey(cacheKey);
        }

        @Override
        public boolean remove(Object o) {
            CacheKey cacheKey = toCacheKey(o);
            if (useObjectAsCacheKey) {
                cacheKeyMap.remove(o);
            }
            return cache.remove(cacheKey) != null;
        }

        @Override
        public int size() {
            return cache.size();
        }

        @Override
        public void clear() {
            if (useObjectAsCacheKey) {
                cacheKeyMap.clear();
            }
            cache.clear();
        }

    }
    
    public Collection cachedValues() {
        return new LocalValues();
    }
    
    final class LocalValues extends AbstractCollection {

        @Override
        public Iterator iterator() {
            return new Iterator() {
                
                private Iterator iter = cache.values().iterator();
                
                @Override
                public boolean hasNext() {
                    return iter.hasNext();
                }

                @Override
                public V next() {
                    return (V) iter.next().getValue();
                }
                
                @Override
                public void remove() {
                    if (useObjectAsCacheKey) {
                        cacheKeyMap.remove(((AbstractCacheMap.MapIterator) iter).cursorValue().getKey());
                    }
                    iter.remove();
                }
            };
        }

        @Override
        public boolean contains(Object o) {
            CacheValue cacheValue = new CacheValue(null, o);
            return cache.containsValue(cacheValue);
        }

        @Override
        public int size() {
            return cache.size();
        }

        @Override
        public void clear() {
            cache.clear();
            if (useObjectAsCacheKey) {
                cacheKeyMap.clear();
            }
        }

    }

    public Set> cachedEntrySet() {
        return new LocalEntrySet();
    }

    final class LocalEntrySet extends AbstractSet> {

        @Override
        public Iterator> iterator() {
            return new Iterator>() {
                
                private Iterator iter = cache.values().iterator();
                
                @Override
                public boolean hasNext() {
                    return iter.hasNext();
                }

                @Override
                public Map.Entry next() {
                    CacheValue e = iter.next();
                    V val = toValue(e);
                    return new AbstractMap.SimpleEntry((K) e.getKey(), val);
                }
                
                @Override
                public void remove() {
                    if (useObjectAsCacheKey) {
                        cacheKeyMap.remove(((AbstractCacheMap.MapIterator) iter).cursorValue().getKey());
                    }
                    iter.remove();
                }
            };
        }

        @Override
        public boolean contains(Object o) {
            if (!(o instanceof Map.Entry))
                return false;
            Map.Entry e = (Map.Entry) o;
            CacheKey cacheKey = toCacheKey(e.getKey());
            CacheValue entry = cache.get(cacheKey);
            return entry != null && entry.getValue().equals(e.getValue());
        }

        @Override
        public boolean remove(Object o) {
            if (o instanceof Map.Entry) {
                Map.Entry e = (Map.Entry) o;
                CacheKey cacheKey = toCacheKey(e.getKey());
                if (useObjectAsCacheKey) {
                    cacheKeyMap.remove(e.getKey());
                }
                return cache.remove(cacheKey) != null;
            }
            return false;
        }

        @Override
        public int size() {
            return cache.size();
        }

        @Override
        public void clear() {
            cache.clear();
        }

    }
    
    public Map getCachedMap() {
        return new LocalMap();
    }
    
    final class LocalMap extends AbstractMap {

        @Override
        public V get(Object key) {
            CacheKey cacheKey = toCacheKey(key);
            CacheValue e = cache.get(cacheKey);
            if (e != null) {
                return (V) e.getValue();
            }
            return null;
        }
        
        @Override
        public boolean containsKey(Object key) {
            CacheKey cacheKey = toCacheKey(key);
            return cache.containsKey(cacheKey);
        }
        
        @Override
        public boolean containsValue(Object value) {
            CacheValue cacheValue = new CacheValue(null, value);
            return cache.containsValue(cacheValue);
        }

        @Override
        public Set> entrySet() {
            return cachedEntrySet();
        }

    }

    protected V toValue(CacheValue cv) {
        return (V) cv.getValue();
    }

    public CacheKey toCacheKey(Object key) {
        CacheKey cacheKey;
        if (useObjectAsCacheKey) {
            cacheKey = cacheKeyMap.get(key);
            if (cacheKey != null) {
                return cacheKey;
            }
        }
        ByteBuf encoded = object.encodeMapKey(key);
        try {
            cacheKey = toCacheKey(encoded);
            if (useObjectAsCacheKey) {
                cacheKeyMap.put(key, cacheKey);
            }
            return cacheKey;
        } finally {
            encoded.release();
        }
    }

    public CacheKey toCacheKey(ByteBuf encodedKey) {
        return new CacheKey(Hash.hash128toArray(encodedKey));
    }

    public  ConcurrentMap getCache() {
        return (ConcurrentMap) cache;
    }

    public ConcurrentMap getCacheKeyMap() {
        return cacheKeyMap;
    }

    public  ConcurrentMap createCache(LocalCachedMapOptions options) {
        if (options.getCacheSize() == -1) {
            return new NoOpCacheMap<>();
        }

        if (options.getCacheProvider() == LocalCachedMapOptions.CacheProvider.CAFFEINE) {
            Caffeine caffeineBuilder = Caffeine.newBuilder();
            if (options.getTimeToLiveInMillis() > 0) {
                caffeineBuilder.expireAfterWrite(options.getTimeToLiveInMillis(), TimeUnit.MILLISECONDS);
            }
            if (options.getMaxIdleInMillis() > 0) {
                caffeineBuilder.expireAfterAccess(options.getMaxIdleInMillis(), TimeUnit.MILLISECONDS);
            }
            if (options.getCacheSize() > 0) {
                caffeineBuilder.maximumSize(options.getCacheSize());
            }
            if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.SOFT) {
                caffeineBuilder.softValues();
            }
            if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.WEAK) {
                caffeineBuilder.weakValues();
            }
            return caffeineBuilder.build().asMap();
        }

        if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.NONE) {
            return new NoneCacheMap<>(options.getTimeToLiveInMillis(), options.getMaxIdleInMillis());
        }
        if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.LRU) {
            return new LRUCacheMap<>(options.getCacheSize(), options.getTimeToLiveInMillis(), options.getMaxIdleInMillis());
        }
        if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.LFU) {
            return new LFUCacheMap<>(options.getCacheSize(), options.getTimeToLiveInMillis(), options.getMaxIdleInMillis());
        }
        if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.SOFT) {
            return ReferenceCacheMap.soft(options.getTimeToLiveInMillis(), options.getMaxIdleInMillis());
        }
        if (options.getEvictionPolicy() == LocalCachedMapOptions.EvictionPolicy.WEAK) {
            return ReferenceCacheMap.weak(options.getTimeToLiveInMillis(), options.getMaxIdleInMillis());
        }
        throw new IllegalArgumentException("Invalid eviction policy: " + options.getEvictionPolicy());
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy