Skip to content
fogus edited this page Mar 13, 2012 · 11 revisions

FIFO cache

A First-In-First-Out cache is one that uses queuing logic for its backing store, expunging the elements at the front of the queue when a predetermined limit is exceeded.

General use

To create a core.cache FifoCache instance you can do the following:

(ns your.lib 
  (:require [clojure.core.cache :as cache]))

(def C (cache/fifo-cache-factory {:a 1, :b 2} :limit 3))

The cache instance C is initialized with the seed map {:a 1, :b 2} and a queue limit of 3, For your own purposes 2 is probably too small, but it's fine for the purpose of illustration. Since the queue limit was set to 3 you might expect that adding another element to the cache would insert a 3rd element, and indeed that is the case:

(assoc C :c 42)
;=> {:a 1, :b 2, :c 3}

However, adding one more element should evict one element, and indeed it does:

(-> C (assoc :c 3, :z 42))
;=> {:z 42, :c 3, :b 2}

Like all of the implementations in core.cache, FifoCache instances operate like regular maps and are immutable.

FIFO cache use cases

TBD

Clone this wiki locally