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

[ bsd3, data, library ] [ Propose Tags ]

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]
Versions [faq] 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
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.*), hashtables (==1.2.*), mtl (==2.2.*), transformers (>=0.4 && <0.6) [details]
License BSD-3-Clause
Copyright 2019 Erick Gonzalez
Author Erick Gonzalez
Maintainer erick@codemonkeylabs.de
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 Fri Aug 30 15:00:46 UTC 2019
Distributions NixOS:1.3.1.1, Stackage:1.3.1.0
Downloads 1017 total (196 in the last 30 days)
Rating (no votes yet) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs available [build log]
Last success reported on 2019-08-30 [all 1 reports]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees


Readme for ttl-hashtables-1.3.1.1

[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