Apache Zeppelin系列教程第八篇——LRU算法在Apache Zeppelin中的应用

LRU(最近最少使用)是一种内存管理的页面置换算法,当内存达到极限时,会淘汰最近最少使用的数据。文章介绍了Redis中使用LRU时钟来记录对象访问时间以及Java中通过LinkedHashMap实现LRU算法的示例,特别提到了ApacheZeppelin在Note缓存中应用LRU策略来优化性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LRU算法介绍

LRU过期策略---最近最少使用

概述:LRU 是 Least Recently Used 的缩写,即最近最少使用,是内存管理的一种页面置换算法。算法的核心是:如果一个数据在最近一段时间内没有被访问到,那么它在将来被访问的可能性也很小。换言之,当内存达到极限时,应该把内存中最久没有被访问的数据淘汰掉。

那么,如何表示这个最久呢?Redis 在实现上引入了一个 LRU 时钟来代替 unix 时间戳,每个对象的每次被访问都会记录下当前服务器的 LRU 时钟,然后用服务器的 LRU 时钟减去对象本身的时钟,得到的就是这个对象没有被访问的时间间隔(也称空闲时间),空闲时间最大的就是需要淘汰的对象。

LRU算法实现

LRU算法在java中的实现和应用

https://blog.csdn.net/weixin_34417635/article/details/91477695

LRU算法具体实现(参考leetcode代码)

https://leetcode.cn/problems/lru-cache/solution/ha-xi-biao-shuang-xiang-lian-biao-java-g-1uo3/

LRU算法在Apache Zeppelin中的应用

zeppelin 在保存note 的时候会采用LRU 算法,对于经常使用Note进行采用LRU算法,具体代码:

private class LRUCache extends LinkedHashMap<String, Note> {

      private static final long serialVersionUID = 1L;

      public LRUCache() {
        super(NoteCache.this.threshold, 0.5f, true /* lru by access mode */);
      }

      @Override
      protected boolean removeEldestEntry(java.util.Map.Entry<String, Note> eldest) {
        if (size() <= NoteCache.this.threshold) {
          return false;
        }
        final Note eldestNote = eldest.getValue();
        final Lock lock = eldestNote.getLock().writeLock();
        if (lock.tryLock()) { // avoid eviction in case the note is in use
          try {
            return true;
          } finally {
            lock.unlock();
          }
        } else {
          LOGGER.info("Can not evict note {}, because the write lock can not be acquired. {} notes currently loaded.",
              eldestNote.getId(), size());
          cleanupCache();
          return false;
        }
      }

      private void cleanupCache() {
        Iterator<Map.Entry<String, Note>> iterator = this.entrySet().iterator();
        int count = 0;
        // if size >= shrinked_size and have next() try remove
        while ((this.size() - 1) >= NoteCache.this.threshold && iterator.hasNext()) {
          Map.Entry<String, Note> noteEntry = iterator.next();
          final Note note = noteEntry.getValue();
          final Lock lock = note.getLock().writeLock();
          if (lock.tryLock()) { // avoid eviction in case the note is in use
            try {
              iterator.remove(); // remove LRU element from LinkedHashMap
              LOGGER.debug("Remove note {} from LRU Cache", note.getId());
              ++count;
            } finally {
              lock.unlock();
            }
          }
        }
        LOGGER.info("The cache cleanup removes {} entries", count);
      }
    }
  }

可以看到其实就是采用java LinkedHashMap来实现的LRU算法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

诸葛子房_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值