ttl-hashtables: Extends hashtables so that entries added can be expired after a TTL

[ bsd3, data, library ] [ Propose Tags ] [ Report a vulnerability ]

This library extends fast mutable hashtables so that entries added can be expired after a given TTL (time to live). This TTL can be specified as a default property of the table or on a per entry basis.


[Skip to Readme]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 1.0.0.0, 1.0.0.1, 1.1.0.0, 1.2.0.0, 1.2.1.0, 1.2.2.0, 1.3.0.0, 1.3.1.0, 1.3.1.1, 1.4.0.0, 1.4.1.0
Change log ChangeLog.md
Dependencies base (>=4.7 && <5), clock (>=0.7 && <0.9), containers (>=0.5.6 && <0.7), data-default (>=0.5 && <0.8), failable (>=0.1.0.0 && <2.0), hashable (>=1.2 && <1.4), hashtables (>=1.2 && <1.3), mtl (>=2.2 && <2.3), transformers (>=0.4 && <0.6) [details]
License BSD-3-Clause
Copyright 2019 Erick Gonzalez
Author Erick Gonzalez
Maintainer erick@codemonkeylabs.de
Revised Revision 1 made by erick at 2019-12-30T20:53:40Z
Category Data
Bug tracker https://gitlab.com/codemonkeylabs/ttl-hashtables/issues
Source repo head: git clone https://gitlab.com/codemonkeylabs/ttl-hashtables
Uploaded by erick at 2019-09-23T20:11:52Z
Distributions
Downloads 5126 total (18 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2019-09-23 [all 1 reports]

Readme for ttl-hashtables-1.4.1.0

[back to package description]

ttl-hashtables

This library extends fast mutable hashtables so that entries added can be expired after a given TTL (time to live). This TTL can be specified as a default property of the table or on a per entry basis.

How to use this module:

Import one of the hash table modules from the hashtables package.. i.e. Basic, Cuckoo, etc and "wrap" them in a TTLHashTable:

import Data.HashTable.ST.Basic as Basic
type HashTable k v = TTLHashTable Basic.HashTable k v

foo :: IO (HashTable Int Int)
foo = do
    -- create a hash table with maximum 2 entries and a default TTL of
    -- 100 mS
    ht <- H.newWithSettings def { H.maxSize = 2, H.defaultTTL = 100 }
    runMaybeT $ do
      H.insert ht 1 1
      H.insert ht 2 2
      H.insert ht 3 3 -- will never get past this point since max size is 2
    return ht

main :: IO ()
main = do
  ht <- foo
  v0 <- H.find ht 1
  threadDelay 200000 -- wait 200mS
  v1 <- H.find ht 2
  v2 <- H.find ht 3
  putStrLn $ "V0=" ++ show v0 -- v0 should be found
  putStrLn $ "V1=" ++ show v1 -- v1 won't be found (expired)
  putStrLn $ "V2=" ++ show v2 -- v2 won't be found (never got inserted)
  return ()

You can then use the functions in this module with this hashtable type. Note that the functions in this module which can fail offer a flexible error handling strategy by virtue of working in the context of a 'Failable' monad. So for example, if the function is used directly in the IO monad and a failure occurs it would then result in an exception being thrown. However if the context supports the possibiliy of failure like a MaybeT or ExceptT transformer, it would then instead return something like IO Nothing or Left NotFound respectively (depending on the actual failure of course).

None of the functions in this module are thread safe, just as the underlying mutable hash tables in the ST monad aren't as well. If concurrent threads need to operate on the same table, you need to provide external means of synchronization to guarantee exclusive access to the table