Skip to content
fogus edited this page Jun 15, 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 threshold is exceeded.

General use

To create a core.cache FIFOCache instance you should always use its associated constructor function fifo-cache-factory with an optional :threshold parameter:

    (ns your.lib 
      (:require [clojure.core.cache :as cache]))
	
    (def C (cache/fifo-cache-factory {:a 1, :b 2} :threshold 3))

note: the default :threshold value is 32

The cache instance C is initialized with the seed map {:a 1, :b 2} and a queue threshold of 3, For your own purposes 2 is probably too small, but it's fine for the purpose of illustration. Since the queue threshold 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