利用Google的ConcurrentLinkedHashMap写一个LRU工具

            <dependency>
                <groupId>com.googlecode.concurrentlinkedhashmap</groupId>
                <artifactId>concurrentlinkedhashmap-lru</artifactId>
                <version>1.4.2</version>
            </dependency>

import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
import com.googlecode.concurrentlinkedhashmap.EntryWeigher;
import com.googlecode.concurrentlinkedhashmap.EvictionListener;
/**
 * @author Cyberpunch
 * @version $Id$
 * @since 2021年09月22日 14:31
 */
public class LRUMap<T> {
    private final LRUWeigher<T> lruWeigher;
    private final ConcurrentLinkedHashMap<T, T> proxy;
    public LRUMap() {
        this.lruWeigher = new LRUWeigher<>();
        this.proxy = new ConcurrentLinkedHashMap.Builder<T, T>().maximumWeightedCapacity(20).listener(lruWeigher).weigher(lruWeigher).build();
    }
    public T computeIfAbsent(T key, Function<? super T, ? extends T> mappingFunction) {
        int increase = this.lruWeigher.increase(key);
        System.out.println(key + " weigher " + increase);
        return proxy.computeIfAbsent(key, mappingFunction);
    }
    public T putIfAbsent(T key, T value) {
        int increase = this.lruWeigher.increase(key);
        System.out.println(key + " weigher " + increase);
        return proxy.putIfAbsent(key, value);
    }
    public int size() {
        return proxy.size();
    }
    public T get(T key) {
        return proxy.get(key);
    }
    static class LRUWeigher<T> implements EntryWeigher<T, T>, EvictionListener<T, T> {
        private final ConcurrentHashMap<T, Integer> weigher = new ConcurrentHashMap<>();
        public int increase(T key) {
            int orDefault = weigher.computeIfAbsent(key, k -> 1);
            weigher.replace(key, orDefault + 1);
            return orDefault;
        }
        @Override
        public void onEviction(T key, T value) {
            System.out.println("del weigher " + key);
            weigher.remove(key);
        }
        @Override
        public int weightOf(T key, T value) {
            return weigher.getOrDefault(key, 1);
        }
    }
}
import org.junit.Test;
/**
 * @author Cyberpunch
 * @version $Id$
 * @since 2021年09月22日 11:21
 */
public class CacheTest {
    @Test
    public void LRUTest() {
        LRUMap<Object> map = new LRUMap<>();
        String key = "taiyi01";
        String key2 = "taiyi02";
        for (int i = 0; i < 200; i++) {
            map.putIfAbsent("" + i, "" + i);
            if (i % 2 == 0) {
                map.putIfAbsent(key, key);
            }
            if (i % 10 == 0) {
                map.putIfAbsent(key2, key2);
            }
        }
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 0; i < 100; i++) {
            map.get(key);
        }
        System.out.println(map.size());
        System.out.println(map.get(key));
        System.out.println(map.get(key2));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Click#593

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值