All Projects → lishuo9527 → LocalCache

lishuo9527 / LocalCache

Licence: other
JAVA LocalCache -- JAVA 本地缓存工具类

Labels

Projects that are alternatives of or similar to LocalCache

tiny-cache
Cache WordPress post content, template part, translations and nav menu output in persistent object cache
Stars: ✭ 26 (-58.06%)
Mutual labels:  cache
cache-command
Manages object and transient caches.
Stars: ✭ 12 (-80.65%)
Mutual labels:  cache
hk-cache-manager
Simple wrapper for Redis Cache with Stackoverflow.Redis & AspNetCore aim
Stars: ✭ 17 (-72.58%)
Mutual labels:  cache
tacky
Primitive Object Memoization for Ruby
Stars: ✭ 14 (-77.42%)
Mutual labels:  cache
fastapi-cache
fastapi-cache is a tool to cache fastapi response and function result, with backends support redis and memcached.
Stars: ✭ 375 (+504.84%)
Mutual labels:  cache
CacheLib
Pluggable in-process caching engine to build and scale high performance services
Stars: ✭ 637 (+927.42%)
Mutual labels:  cache
growl
gorm, redis and local cache layer
Stars: ✭ 19 (-69.35%)
Mutual labels:  cache
cache
LRU-based cache package for Go.
Stars: ✭ 25 (-59.68%)
Mutual labels:  cache
ultrafetch
Node-based fetch backed with an RFC-7234 compliant filesystem cache.
Stars: ✭ 30 (-51.61%)
Mutual labels:  cache
regex-cache
Memoize the results of a call to the RegExp constructor, avoiding repetitious runtime compilation of the same string and options, resulting in dramatic speed improvements.
Stars: ✭ 39 (-37.1%)
Mutual labels:  cache
KJNetworkPlugin
🎡A lightweight but powerful Network library. Network Plugin, Support batch and chain operation. 插件版网络架构
Stars: ✭ 43 (-30.65%)
Mutual labels:  cache
moment-cache
⏱ Simple utility to cache moment.js results and speed up moment calls.
Stars: ✭ 29 (-53.23%)
Mutual labels:  cache
cache
PSR-16 compatible cache library
Stars: ✭ 30 (-51.61%)
Mutual labels:  cache
bazel-cache
Minimal cloud oriented Bazel gRPC cache
Stars: ✭ 33 (-46.77%)
Mutual labels:  cache
qcache
In memory cache server with query capabilities
Stars: ✭ 36 (-41.94%)
Mutual labels:  cache
memoize
Caching library for asynchronous Python applications.
Stars: ✭ 53 (-14.52%)
Mutual labels:  cache
infinitree
Scalable and encrypted embedded database with 3-tier caching
Stars: ✭ 80 (+29.03%)
Mutual labels:  cache
punic
Punic is a remote cache CLI built for Carthage and Apple .xcframework
Stars: ✭ 25 (-59.68%)
Mutual labels:  cache
microstream
High-Performance Java-Native-Persistence. Store and load any Java Object Graph or Subgraphs partially, Relieved of Heavy-weight JPA. Microsecond Response Time. Ultra-High Throughput. Minimum of Latencies. Create Ultra-Fast In-Memory Database Applications & Microservices.
Stars: ✭ 283 (+356.45%)
Mutual labels:  cache
bash-cache
Transparent caching layer for bash functions; particularly useful for functions invoked as part of your prompt.
Stars: ✭ 45 (-27.42%)
Mutual labels:  cache

Java LocalCache

使用场景

Java应用中,对于访问频率高,更新少的数据,通常的方案是将这类数据加入缓存中。相对从数据库中读取来说,读缓存效率会有很大提升。

在集群环境下,常用的分布式缓存有RedisMemcached等。但在某些业务场景上,可能不需要去搭建一套复杂的分布式缓存系统,在单机环境下,通常是会希望使用内部的缓存(LocalCache)。

实现

这里提供了两种LocalCache的实现,一种是基于ConcurrentHashMap实现基本本地缓存,另外一种是基于LinkedHashMap实现LRU策略的本地缓存。

基于ConcurrentHashMap的实现

    static {
        timer = new Timer();
        map = new ConcurrentHashMap<>();
    }

ConcurrentHashMap作为缓存的存储结构。因为ConcurrentHashMap的线程安全的,所以基于此实现的LocalCache在多线程并发环境的操作是安全的。在JDK1.8中,ConcurrentHashMap是支持完全并发读,这对本地缓存的效率也是一种提升。通过调用ConcurrentHashMapmap的操作来实现对缓存的操作。

私有构造函数
    private LocalCache() {

    }

LocalCache是工具类,通过私有构造函数强化不可实例化的能力。

缓存清除机制
   /**
     * 清除缓存任务类
     */
    static class CleanWorkerTask extends TimerTask {

        private String key;

        public CleanWorkerTask(String key) {
            this.key = key;
        }

        public void run() {
            LocalCache.remove(key);
        }
    }

清理失效缓存是由Timer类实现的。内部类CleanWorkerTask继承于TimerTask用户清除缓存。每当新增一个元素的时候,都会调用timer.schedule加载清除缓存的任务。

基于LinkedHashMap的实现

LinkedHashMap作为缓存的存储结构。主要是通过LinkedHashMap的按照访问顺序的特性来实现LRU策略。

LRU

LRULeast Recently Used的缩写,即最近最久未使用。LRU缓存将会利用这个算法来淘汰缓存中老的数据元素,从而优化内存空间。

基于LRU策略的map

这里利用LinkedHashMap来实现基于LRU策略的map。通过调用父类LinkedHashMap的构造函数来实例化map。参数accessOrder设置为true保证其可以实现LRU策略。

static class LRUMap<K, V> extends LinkedHashMap<K, V> {

        ...  // 省略部分代码
        
        public LRUMap(int initialCapacity, float loadFactor) {
            super(initialCapacity, loadFactor, true);
        }

        ... // 省略部分代码
        
        /**
         * 重写LinkedHashMap中removeEldestEntry方法;
         * 新增元素的时候,会判断当前map大小是否超过DEFAULT_MAX_CAPACITY,超过则移除map中最老的节点;
         *
         * @param eldest
         * @return
         */
        protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
            return size() > DEFAULT_MAX_CAPACITY;
        }

    }
线程安全
        /**
         * 读写锁
         */
        private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

        private final Lock rLock = readWriteLock.readLock();

        private final Lock wLock = readWriteLock.writeLock();

LinkedHashMap并不是线程安全,如果不加控制的在多线程环境下使用的话,会有问题。所以在LRUMap中引入了ReentrantReadWriteLock读写锁,来控制并发问题。

缓存淘汰机制
        protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
            return size() > DEFAULT_MAX_CAPACITY;
        }

此处重写LinkedHashMapremoveEldestEntry方法, 当缓存新增元素的时候,会判断当前map大小是否超过DEFAULT_MAX_CAPACITY,超过则移除map中最老的节点。

缓存清除机制

缓存清除机制与ConcurrentHashMap的实现一致,均是通过timer实现。

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].