如何在elisp中实现过期的LRU缓存?

Chr*_*s R 3 caching elisp lru

我的数据包含以下三个组件:

  • a_path
  • a_key
  • a_value =f(a_path, a_key)

a_value计算起来很昂贵,所以我想不经常计算它.在一个理想的世界中,只有当它发生变化时才会这样.所以,我对这个缓存的要求如下:

  • 具有可配置最大大小的LRU缓存
  • 键入 (a_path, a_key)
  • 能够根据年龄使条目到期(例如,每小时左右重新计算)
  • 基于的能力使条目到期 expiry_func(a_path, a_key)

我的谷歌搜索在这里失败了; 即使在搜索"elisp LRU缓存"时,我也发现了很多Java站点.

Gar*_*ees 5

以下是您想要的大部分内容:固定大小最近最少使用的缓存,其中包含O(1)查找,O(1)插入和O(1)删除.

让所有这些操作都成为O(1)有点棘手,因此这个稍微精细的实现.我将哈希表(用于快速查找)与双向链接项列表组合在一起(用于快速删除,重新排序和查找最旧的元素).

(require 'cl)

(defstruct lru-cache max-size size newest oldest table)

(defstruct lru-item key value next prev)

(defun lru-remove-item (item lru)
  (let ((next (lru-item-next item))
        (prev (lru-item-prev item)))
    (if next (setf (lru-item-prev next) prev) 
      (setf (lru-cache-newest lru) prev))
    (if prev (setf (lru-item-next prev) next)
      (setf (lru-cache-oldest lru) next))))

(defun lru-insert-item (item lru)
  (let ((newest (lru-cache-newest lru)))
    (setf (lru-item-next item) nil (lru-item-prev item) newest)
    (if newest (setf (lru-item-next newest) item)
      (setf (lru-cache-oldest lru) item))
    (setf (lru-cache-newest lru) item)))

;;; Public interface starts here.

(defun* lru-create (&key (size 65) (test 'eql))
  "Create a new least-recently-used cache and return it.
Takes keyword arguments
:SIZE the maximum number of entries (default: 65).
:TEST a hash table test (default 'EQL)."
  (make-lru-cache 
   :max-size size
   :size 0
   :newest nil
   :oldest nil
   :table (make-hash-table :size size :test test)))

(defun lru-get (key lru &optional default)
  "Look up KEY in least-recently-used cache LRU and return
its associated value.
If KEY is not found, return DEFAULT which defaults to nil."
  (let ((item (gethash key (lru-cache-table lru))))
    (if item
        (progn
          (lru-remove-item item lru)
          (lru-insert-item item lru)
          (lru-item-value item))
      default)))

(defun lru-rem (key lru)
  "Remove KEY from least-recently-used cache LRU."
  (let ((item (gethash key (lru-cache-table lru))))
    (when item
      (remhash (lru-item-key item) (lru-cache-table lru))
      (lru-remove-item item lru)
      (decf (lru-cache-size lru)))))

(defun lru-put (key value lru)
  "Associate KEY with VALUE in least-recently-used cache LRU.
If KEY is already present in LRU, replace its current value with VALUE."
  (let ((item (gethash key (lru-cache-table lru))))
    (if item
        (setf (lru-item-value item) value)
      (when (eql (lru-cache-size lru) (lru-cache-max-size lru))
        (lru-rem (lru-item-key (lru-cache-oldest lru)) lru))
      (let ((newitem (make-lru-item :key key :value value)))
        (lru-insert-item newitem lru)
        (puthash key newitem (lru-cache-table lru))
        (incf (lru-cache-size lru))))))

 ;;; Exercise for the reader: implement lru-clr and lru-map to complete the
 ;;; analogy with hash tables.
Run Code Online (Sandbox Code Playgroud)

对于您的应用程序对对键,你可能要提供:test 'equallru-create.或者如果您需要特殊的东西,请参阅定义哈希比较.

我会告诉你如何做到基于时间的到期; 它应该从这里直截了当.

(如果有人知道一种更简单的方法来实现这一点,同时保持操作在恒定时间内运行,我会非常有兴趣看到它.)