java实现LFU

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());
    });

  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值