exp-cache-0.1.0.1

Safe HaskellNone
LanguageHaskell2010

Data.Cache

Synopsis

Documentation

data Cache k v s Source #

A Cache is a bounded collection of data that supports fast random access. When the cach is full, the associated EvictionStrategy is used to discard an item from the cache. Library users should use readThrough to read and replace items in the cache.

newCache Source #

Arguments

:: (Hashable k, NFData v, EvictionStrategy s, Eq k, Ord k) 
=> Natural

The maximum cache size

-> s k

The evictionStrategy

-> Cache k v (s k) 

Create a new cache of the specified size using the provided EvictionStrategy

readThrough Source #

Arguments

:: (Hashable k, NFData v, EvictionStrategy s, Eq k, Ord k, Monad m) 
=> Cache k v (s k)

The current cache state

-> k

The key to look up in the cache

-> (k -> m v)

The accessor function to evaluate if the key is not found

-> m (v, Cache k v (s k)) 

Performs a read-through operation on a given cache.

class EvictionStrategy s where Source #

Minimal complete definition

recordLookup, evict

Methods

recordLookup Source #

Arguments

:: (Eq k, Hashable k, Ord k) 
=> k

The key to lookup

-> s k

The strategy (containing any state necessary)

-> s k

The strategy + state folloiwng adding the key

evict :: (Eq k, Hashable k, Ord k) => s k -> (s k, Maybe k) Source #

Instances

EvictionStrategy FIFO Source # 

Methods

recordLookup :: (Eq k, Hashable k, Ord k) => k -> FIFO k -> FIFO k Source #

evict :: (Eq k, Hashable k, Ord k) => FIFO k -> (FIFO k, Maybe k) Source #

EvictionStrategy LFU Source # 

Methods

recordLookup :: (Eq k, Hashable k, Ord k) => k -> LFU k -> LFU k Source #

evict :: (Eq k, Hashable k, Ord k) => LFU k -> (LFU k, Maybe k) Source #

EvictionStrategy LRU Source # 

Methods

recordLookup :: (Eq k, Hashable k, Ord k) => k -> LRU k -> LRU k Source #

evict :: (Eq k, Hashable k, Ord k) => LRU k -> (LRU k, Maybe k) Source #

EvictionStrategy SeqLRU Source # 

Methods

recordLookup :: (Eq k, Hashable k, Ord k) => k -> SeqLRU k -> SeqLRU k Source #

evict :: (Eq k, Hashable k, Ord k) => SeqLRU k -> (SeqLRU k, Maybe k) Source #

EvictionStrategy MRU Source # 

Methods

recordLookup :: (Eq k, Hashable k, Ord k) => k -> MRU k -> MRU k Source #

evict :: (Eq k, Hashable k, Ord k) => MRU k -> (MRU k, Maybe k) Source #

EvictionStrategy RR Source # 

Methods

recordLookup :: (Eq k, Hashable k, Ord k) => k -> RR k -> RR k Source #

evict :: (Eq k, Hashable k, Ord k) => RR k -> (RR k, Maybe k) Source #

data SeqLRU k Source #

This is a naive and terribly slow version of an LRU cache

Instances

EvictionStrategy SeqLRU Source # 

Methods

recordLookup :: (Eq k, Hashable k, Ord k) => k -> SeqLRU k -> SeqLRU k Source #

evict :: (Eq k, Hashable k, Ord k) => SeqLRU k -> (SeqLRU k, Maybe k) Source #

Eq k => Eq (SeqLRU k) Source # 

Methods

(==) :: SeqLRU k -> SeqLRU k -> Bool #

(/=) :: SeqLRU k -> SeqLRU k -> Bool #

Show k => Show (SeqLRU k) Source # 

Methods

showsPrec :: Int -> SeqLRU k -> ShowS #

show :: SeqLRU k -> String #

showList :: [SeqLRU k] -> ShowS #

data LRU k Source #

An optimized version of an LRU cache. The least recently used element in the cache is evicted once the cache fills up.

Instances

EvictionStrategy LRU Source # 

Methods

recordLookup :: (Eq k, Hashable k, Ord k) => k -> LRU k -> LRU k Source #

evict :: (Eq k, Hashable k, Ord k) => LRU k -> (LRU k, Maybe k) Source #

(Ord k, Hashable k) => Eq (LRU k) Source # 

Methods

(==) :: LRU k -> LRU k -> Bool #

(/=) :: LRU k -> LRU k -> Bool #

Show k => Show (LRU k) Source # 

Methods

showsPrec :: Int -> LRU k -> ShowS #

show :: LRU k -> String #

showList :: [LRU k] -> ShowS #

data MRU k Source #

A Most Recently Used cache. This evicts the most recently accessed item from the cache

Instances

EvictionStrategy MRU Source # 

Methods

recordLookup :: (Eq k, Hashable k, Ord k) => k -> MRU k -> MRU k Source #

evict :: (Eq k, Hashable k, Ord k) => MRU k -> (MRU k, Maybe k) Source #

(Ord k, Hashable k) => Eq (MRU k) Source # 

Methods

(==) :: MRU k -> MRU k -> Bool #

(/=) :: MRU k -> MRU k -> Bool #

Show k => Show (MRU k) Source # 

Methods

showsPrec :: Int -> MRU k -> ShowS #

show :: MRU k -> String #

showList :: [MRU k] -> ShowS #

data RR k Source #

Random Replacement cache. The seed is fixed to an StdGen since its both easily accessible & good enough for this purpose. Random replacement means exactly what it sounds like: when the cache fills up a random element is selected and evicted.

Instances

EvictionStrategy RR Source # 

Methods

recordLookup :: (Eq k, Hashable k, Ord k) => k -> RR k -> RR k Source #

evict :: (Eq k, Hashable k, Ord k) => RR k -> (RR k, Maybe k) Source #

Eq k => Eq (RR k) Source # 

Methods

(==) :: RR k -> RR k -> Bool #

(/=) :: RR k -> RR k -> Bool #

Show k => Show (RR k) Source # 

Methods

showsPrec :: Int -> RR k -> ShowS #

show :: RR k -> String #

showList :: [RR k] -> ShowS #

newRR :: StdGen -> Int -> RR k Source #

Generate a new Random Replacement cache using the provided seed & size.

Least Frequently Used Cache

data LFU k Source #

Evict the least frequently used element from the cache. This means as an element is accessed, its "score" increases and the element is more likely to survive eviction once the cache fills up.

Instances

EvictionStrategy LFU Source # 

Methods

recordLookup :: (Eq k, Hashable k, Ord k) => k -> LFU k -> LFU k Source #

evict :: (Eq k, Hashable k, Ord k) => LFU k -> (LFU k, Maybe k) Source #

(Ord k, Hashable k) => Eq (LFU k) Source # 

Methods

(==) :: LFU k -> LFU k -> Bool #

(/=) :: LFU k -> LFU k -> Bool #

Show k => Show (LFU k) Source # 

Methods

showsPrec :: Int -> LFU k -> ShowS #

show :: LFU k -> String #

showList :: [LFU k] -> ShowS #

First in First Out Cache (a queue)

data FIFO k Source #

A First in First Out eviction strategy. Items are evicted based on order added, regardless of usage patterns.

Instances

EvictionStrategy FIFO Source # 

Methods

recordLookup :: (Eq k, Hashable k, Ord k) => k -> FIFO k -> FIFO k Source #

evict :: (Eq k, Hashable k, Ord k) => FIFO k -> (FIFO k, Maybe k) Source #

Eq k => Eq (FIFO k) Source # 

Methods

(==) :: FIFO k -> FIFO k -> Bool #

(/=) :: FIFO k -> FIFO k -> Bool #

Show k => Show (FIFO k) Source # 

Methods

showsPrec :: Int -> FIFO k -> ShowS #

show :: FIFO k -> String #

showList :: [FIFO k] -> ShowS #