Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- data Cache k v s
- newCache :: (Hashable k, NFData v, EvictionStrategy s, Eq k, Ord k) => Natural -> s k -> Cache k v (s k)
- readThrough :: (Hashable k, NFData v, EvictionStrategy s, Eq k, Ord k, Monad m) => Cache k v (s k) -> k -> (k -> m v) -> m (v, Cache k v (s k))
- class EvictionStrategy s where
- data SeqLRU k
- newSeqLRU :: SeqLRU k
- data LRU k
- newLRU :: LRU k
- data MRU k
- newMRU :: MRU k
- data RR k
- newRR :: StdGen -> Int -> RR k
- data LFU k
- newLFU :: LFU k
- data FIFO k
- newFIFO :: FIFO k
Documentation
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.
:: (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
:: (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 #
:: (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 #
This is a naive and terribly slow version of an LRU cache
An optimized version of an LRU cache. The least recently used element in the cache is evicted once the cache fills up.
A Most Recently Used cache. This evicts the most recently accessed item from the cache
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.
newRR :: StdGen -> Int -> RR k Source #
Generate a new Random Replacement cache using the provided seed & size.
Least Frequently Used Cache
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.
First in First Out Cache (a queue)