3

次の3つのコンポーネントで構成されるデータがあります。

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

a_value計算に費用がかかるので、あまり計算したくないです。理想的な世界では、それはそれが変化するときだけです。したがって、このキャッシュの要件は次のとおりです。

  • 最大サイズを構成可能なLRUキャッシュ
  • キーオン(a_path, a_key)
  • 年齢に基づいてエントリを期限切れにする機能(たとえば、1時間ごとに再計算します)
  • に基づいてエントリを期限切れにする機能expiry_func(a_path, a_key)

私のグーグルはここで私を失敗させました。「elispLRUcache」を検索してもJavaサイトがたくさん見つかります。

4

1 に答える 1

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.

:test 'equalペアをキーとするアプリケーションの場合、おそらくlru-create. または、何か特別なことが必要な場合は、ハッシュ比較の定義を参照してください。

時間ベースの有効期限を設定する方法を説明します。ここからは簡単です。

(操作を一定時間内に実行しながらこれを実装する簡単な方法を誰かが知っている場合は、非常に興味があります。)

于 2011-07-13T00:08:16.663 に答える