lrucache-1.0: a simple, pure LRU cache

Data.Cache.LRU

Description

Implements an LRU cache.

This module provides a pure LRU cache based on a doubly-linked list through a Data.Map structure. This gives O(log n) operations on insert, lookup, delete, and pop, and O(n * log n) for toList.

The interface this module provides is opaque. If further control is desired, the Data.Cache.LRU.Internal module can be used.

Synopsis

Documentation

data LRU key val Source

Stores the information that makes up an LRU cache

Instances

(Eq key, Eq val) => Eq (LRU key val) 
(Ord key, Show key, Show val) => Show (LRU key val) 

newLRUSource

Arguments

:: Ord key 
=> Maybe Int

the optional maximum size of the LRU

-> LRU key val 

Make an LRU. If a size limit is specified, the LRU is guaranteed to not grow above the specified number of entries.

fromListSource

Arguments

:: Ord key 
=> Maybe Int

the optional maximum size of the LRU

-> [(key, val)] 
-> LRU key val 

Build a new LRU from the given maximum size and list of contents, in order from most recently accessed to least recently accessed.

toList :: Ord key => LRU key val -> [(key, val)]Source

Retrieve a list view of an LRU. The items are returned in order from most recently accessed to least recently accessed.

maxSize :: LRU key val -> Maybe IntSource

the maximum size of the LRU cache

insert :: Ord key => key -> val -> LRU key val -> LRU key valSource

Add an item to an LRU. If the key was already present in the LRU, the value is changed to the new value passed in. The item added is marked as the most recently accessed item in the LRU returned.

If this would cause the LRU to exceed its maximum size, the least recently used item is dropped from the cache.

lookup :: Ord key => key -> LRU key val -> (LRU key val, Maybe val)Source

Look up an item in an LRU. If it was present, it is marked as the most recently accesed in the returned LRU.

delete :: Ord key => key -> LRU key val -> (LRU key val, Maybe val)Source

Remove an item from an LRU. Returns the new LRU, and if the item was present to be removed.

pop :: Ord key => LRU key val -> (LRU key val, Maybe (key, val))Source

Removes the least-recently accessed element from the LRU. Returns the new LRU, and the key and value from the least-recently used element, if there was one.

size :: LRU key val -> IntSource

Returns the number of elements the LRU currently contains.