LRU、LFU算法java实现

https://blog.csdn.net/foye12/article/details/78647647

近期使用springboot集成ehcache实现缓存,spring还支持使用简单ConcurrentMapCache实现,底层就是用ConcurrentHashMap实现。ehcache相对来说比较重,加pom依赖下载了很长时间,但是ehcache有很多可配置的选项,其中包括缓存达到一定大小淘汰的算法的选择。包括了FIFO、LRU、LFU可以根据不同的业务场景选择。

一、LRU的实现比较简单,因为java中的LinkedHashMap有很多特点正好适合LRU的思想。LRU(least recently used)最近最少使用,首先淘汰最长时间未被使用的页面。使用链表实现,当一个元素被访问了,把元素移动到链表的顶端,插入新元素也放到顶端,当缓存数量到达限制,直接从链表底端移除元素。

public class LRU<k, v> extends LinkedHashMap<k, v> {

  private final int MAX_SIZE;

  public LRU(int capcity) {
    super(8, 0.75f,true);
    this.MAX_SIZE = capcity;
  }

  @Override
  public boolean removeEldestEntry(Map.Entry<k, v> eldest) {
    if (size() > MAX_SIZE) {
      System.out.println("移除的元素为:" + eldest.getValue());
    }
    return size() > MAX_SIZE;
  }

  public static void main(String[] args) {
    Map<Integer, Integer> map = new LRU<>(5);
    for (int i = 1; i <= 11; i++) {
      map.put(i, i);
      System.out.println("cache的容量为:" + map.size());
      if (i == 4) {
        map.get(1);
      }
    }

    System.out.println("=-=-=-=-=-=-=-map元素:");
    map.entrySet().forEach(integerIntegerEntry -> System.out.println(integerIntegerEntry.getValue()));

  }

}


LinkedHashMap有一个accessOrder的参数正好和LRU的思路相契合,这里使用0.75的默认加载因子(加载因子过小空间利用率低,冲突减少,访问速度快,加载因子过大,反之。加载因子过大,当执行put操作,两个对象的hashcode相同时要操作链表,相应的get操作是也要操作链表,这样就使得访问变慢。) 
removeEldestEntry方法当结果返回为true时,它会清除map中的最老元素。以实现LRU的算法。 
运行结果为:

cache的容量为:1
cache的容量为:2
cache的容量为:3
cache的容量为:4
cache的容量为:5
移除的元素为:2
cache的容量为:5
移除的元素为:3
cache的容量为:5
移除的元素为:4
cache的容量为:5
移除的元素为:1
cache的容量为:5
移除的元素为:5
cache的容量为:5
移除的元素为:6
cache的容量为:5
=-=-=-=-=-=-=-map元素:
7
8
9
10
11


代码里面为了测试,在加入元素4的时候,访问了一下元素1,然后看到,在缓存达到限制时,最先移除的不是1,而是2,3然后是1。

第二种实现:双向链表加HashMap

public class LRUCache1<K, V> {

    private final int MAX_CACHE_SIZE;
    //链表头
    private Entry first;
    //链表尾,储存最旧的元素
    private Entry last;
    //键值对
    private HashMap<K, Entry<K, V>> hashMap;

    public LRUCache1(int cacheSize) {
        MAX_CACHE_SIZE = cacheSize;
        hashMap = new HashMap<K, Entry<K, V>>();
    }

    //基础方法put
    public void put(K key, V value) {
        Entry entry = getEntry(key);
        if (entry == null) {
            if (hashMap.size() >= MAX_CACHE_SIZE) {
                //删除hashmap中的last元素
                hashMap.remove(last.key);
                //删除双向链表中的last节点
                removeLast();
            }
            entry = new Entry();
            entry.key = key;
        }
        entry.value = value;
        //移到链表头部
        moveToFirst(entry);
        hashMap.put(key, entry);
    }

    //基础方法get
    public V get(K key) {
        Entry<K, V> entry = getEntry(key);
        if (entry == null) return null;
        moveToFirst(entry);
        return entry.value;
    }

    //将entry移到头部
    private void moveToFirst(Entry entry) {

        if (entry == first) return;
        if (entry.pre != null) entry.pre.next = entry.next;
        if (entry.next != null) entry.next.pre = entry.pre;
        if (entry == last) last = last.pre;

        if (first == null || last == null) {
            first = last = entry;
            return;
        }

        entry.next = first;
        first.pre = entry;
        first = entry;
        entry.pre = null;
    }

    //删除最老的元素
    private void removeLast() {
        if (last != null) {
            last = last.pre;
            if (last == null) first = null;
            else last.next = null;
        }
    }


    private Entry<K, V> getEntry(K key) {
        return hashMap.get(key);
    }


    //双向链表
    class Entry<K, V> {
        public Entry pre;
        public Entry next;
        public K key;
        public V value;
    }
}

二、LFU(Least Frequently Used)淘汰一定时期内被访问次数最少的元素。如果元素的一定时间内的访问次数相同时,则比较他们的最新一次的访问时间。

public class LFU<k, v> {
  private final int capcity;

  private Map<k, v> cache = new HashMap<>();

  private Map<k, HitRate> count = new HashMap<>();

  public LFU(int capcity) {
    this.capcity = capcity;
  }

  public void put(k key, v value) {
    v v = cache.get(key);
    if (v == null) {
      if (cache.size() == capcity) {
        removeElement();
      }
      count.put(key, new HitRate(key, 1, System.nanoTime()));
    } else {
      addHitCount(key);
    }
    cache.put(key, value);
  }

  public v get(k key) {
    v value = cache.get(key);
    if (value != null) {
      addHitCount(key);
      return value;
    }
    return null;
  }

  //移除元素
  private void removeElement() {
    HitRate hr = Collections.min(count.values());
    cache.remove(hr.key);
    count.remove(hr.key);
  }

  //更新访问元素状态
  private void addHitCount(k key) {
    HitRate hitRate = count.get(key);
    hitRate.hitCount = hitRate.hitCount + 1;
    hitRate.lastTime = System.nanoTime();
  }

  //内部类
  class HitRate implements Comparable<HitRate> {
    private k key;
    private int hitCount;
    private long lastTime;

    private HitRate(k key, int hitCount, long lastTime) {
      this.key = key;
      this.hitCount = hitCount;
      this.lastTime = lastTime;
    }

    @Override
    public int compareTo(HitRate o) {
      int compare = Integer.compare(this.hitCount, o.hitCount);
      return compare == 0 ? Long.compare(this.lastTime, o.lastTime) : compare;
    }
  }


  public static void main(String[] args) {
    LFU<Integer, Integer> cache = new LFU<>(3);
    cache.put(2, 2);
    cache.put(1, 1);

    System.out.println(cache.get(2));
    System.out.println(cache.get(1));
    System.out.println(cache.get(2));

    cache.put(3, 3);
    cache.put(4, 4);

    //1、2元素都有访问次数,放入3后缓存满,加入4时淘汰3
    System.out.println(cache.get(3));
    System.out.println(cache.get(2));
    //System.out.println(cache.get(1));
    System.out.println(cache.get(4));

    cache.put(5, 5);
    //目前2访问2次,1访问一次,4访问一次,由于4的时间比较新,放入5的时候移除1元素。
    System.out.println("-=-=-=-");
    cache.cache.entrySet().forEach(entry -> {
      System.out.println(entry.getValue());
    });

  }
}


这里实现略微复杂,首先要维持一个缓存的map还要维持一个访问次数以及时间的map。 
1、首先内部类有3个属性,有缓存的key,缓存的访问次数,最近一次的访问时间。内部实现比较器,按照先访问次数后时间的比较顺序。整个类作为对象放在一个hashmap的value里。 
2、put方法:当一个元素要放入缓存的时候,先去缓存检查是否有相同的key,也就是要填加的(key,value)在cache的map里get(key)是否为空。这里不用检查value是否有相同的,标识一个缓存的唯一性是key,key不同就是一个不同的缓存元素。 
当get(key)为空时,要放入缓存新元素了,首先检查缓存的容量是否达到限制值,达到了,执行移除一个元素的方法,然后在count的map里加入相应的访问信息,初始值为1。如果没达到限制值,直接在count的map里加入相应的访问信息,初始值为1。 
当get(key)不为空直接执行addHitCount方法。将访问次数加1,更新最新访问时间。 
最后不管什么情况,执行map.put方法。

运行结果:

2
1
2
null
2
4
-=-=-=-
2
4
5


开始加入1,2,访问1,一次2两次。加入3,在加入4.发现缓存元素为124.原因是1,2都有访问次数,3和4在时间上,比较古老,会被淘汰。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值