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

时间:2011-07-11 15:56:55

标签: 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站点。

1 个答案:

答案 0 :(得分:6)

以下是您想要的大部分内容:固定大小最近最少使用的缓存,包含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.

对于已成对关键字的应用,您可能希望向:test 'equal提供lru-create。如果您需要特别的东西,请参阅Defining Hash Comparisons

我会告诉你如何进行基于时间的到期;它应该直截了当。

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