ttl-hashtables 1.3.1.1 → 1.4.0.0
raw patch · 2 files changed
+30/−34 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Data.TTLHashTable: [renewUponRead] :: Settings -> Bool
+ Data.TTLHashTable: getTimeStamp :: MonadIO m => m Int
+ Data.TTLHashTable: lookupAndRenew :: (Eq k, Hashable k, MonadIO m, Failable m) => TTLHashTable h k v -> k -> m v
+ Data.TTLHashTable: lookupMaybeExpired :: (Eq k, Hashable k, MonadIO m, Failable m) => TTLHashTable h k v -> k -> m (v, Int)
- Data.TTLHashTable: Settings :: Int -> Bool -> Int -> Int -> Settings
+ Data.TTLHashTable: Settings :: Int -> Int -> Int -> Settings
Files
- src/Data/TTLHashTable.hs +28/−32
- ttl-hashtables.cabal +2/−2
src/Data/TTLHashTable.hs view
@@ -45,8 +45,11 @@ find, foldM, getSettings,+ getTimeStamp, Data.TTLHashTable.mapM_, lookup,+ lookupAndRenew,+ lookupMaybeExpired, mutate, new, newWithSettings,@@ -86,7 +89,6 @@ maxSizeRef_ :: IORef Int, numEntriesRef_ :: IORef Int, timeStampsRef_ :: IORef (IntMap k),- renewUponReadRef_ :: IORef Bool, defaultTTLRef_ :: IORef Int, gcMaxEntriesRef_ :: IORef Int } -> TTLHashTable h k v@@ -100,9 +102,6 @@ -- | Maximum size of the hash table. Once reached, insertion of keys -- will fail. Defaults to @maxBound@ maxSize :: Int,- -- | Whether a succesful lookup of an entry means the TTL of the entry- -- should be restarted. Default is 'False'- renewUponRead :: Bool, -- | Default TTL value in milliseconds to be used for an entry if none -- is specified at insertion time defaultTTL :: Int,@@ -125,8 +124,7 @@ instance Default Settings where def = Settings { maxSize = maxBound,- renewUponRead = False,- defaultTTL = 365 * 24 * 60 * 60 * 1000, -- 1 year in milliseconds+ defaultTTL = 24 * 60 * 60 * 1000, -- 1 day in milliseconds gcMaxEntries = maxBound } @@ -154,14 +152,12 @@ sRef <- newIORef 0 tRef <- newIORef M.empty msRef <- newIORef maxSize- rurRef <- newIORef renewUponRead dTTLRef <- newIORef defaultTTL meRef <- newIORef gcMaxEntries return TTLHashTable { hashTable_ = table, maxSizeRef_ = msRef, numEntriesRef_ = sRef, timeStampsRef_ = tRef,- renewUponReadRef_ = rurRef, defaultTTLRef_ = dTTLRef, gcMaxEntriesRef_ = meRef }@@ -243,21 +239,29 @@ -- return @IO Nothing@ or @IO (Left NotFound)@ respectively. So you probably want to -- __execute this function in one of these transformer monads__ lookup :: (Eq k, Hashable k, MonadIO m, Failable m) => TTLHashTable h k v -> k -> m v-lookup ht@TTLHashTable {..} k = do- renewUponRead <- liftIO $ readIORef renewUponReadRef_- lookup' ht renewUponRead k+lookup = lookup' False -lookup' :: (Eq k, Hashable k, MonadIO m, Failable m) => TTLHashTable h k v -> Bool -> k -> m v-lookup' ht@TTLHashTable {..} False k = do+-- | Like lookup but it restarts the lifetime of the entry if found. Note that this is+-- not a read only operation (i.e. the entry must be of course modified to update its timestamp)+lookupAndRenew :: (Eq k, Hashable k, MonadIO m, Failable m) => TTLHashTable h k v -> k -> m v+lookupAndRenew = lookup' True++lookupMaybeExpired :: (Eq k, Hashable k, MonadIO m, Failable m) => TTLHashTable h k v -> k -> m (v, Int)+lookupMaybeExpired TTLHashTable {..} k =+ maybe (failure NotFound) returnValue =<< liftIO (H.lookup hashTable_ k)+ where returnValue Value {..} = return (value, expiresAt)++lookup' :: (Eq k, Hashable k, MonadIO m, Failable m) => Bool -> TTLHashTable h k v -> k -> m v+lookup' False TTLHashTable {..} k = do now <- getTimeStamp mValue <- liftIO $ H.lookup hashTable_ k- Value {..} <- checkLookedUp ht k mValue now+ Value {..} <- checkLookedUp mValue now return value-lookup' ht@TTLHashTable {..} True k = do+lookup' True ht@TTLHashTable {..} k = do now <- getTimeStamp (mExpire, mValue) <- mutateWith (refreshEntry now) ht k removeTimeStamp mExpire- Value {..} <- checkLookedUp ht k mValue now+ Value {..} <- checkLookedUp mValue now liftIO $ modifyIORef' timeStampsRef_ $ M.insert expiresAt k return value where refreshEntry _ Nothing =@@ -275,25 +279,20 @@ removeTimeStamp (Just timeStamp) = liftIO . modifyIORef' timeStampsRef_ $ M.delete timeStamp -checkLookedUp :: (Eq k, Hashable k, MonadIO m, C.HashTable h, Failable m)- => TTLHashTable h k v- -> k- -> Maybe (Value v)+checkLookedUp :: (MonadIO m, Failable m)+ => Maybe (Value v) -> Int -> m (Value v)-checkLookedUp _ _ Nothing _ = failure NotFound-checkLookedUp ht k (Just v@Value {..}) now =+checkLookedUp Nothing _ = failure NotFound+checkLookedUp (Just v@Value {..}) now = if expiresAt < now- then do- delete ht k- failure ExpiredEntry- else- return v+ then failure ExpiredEntry+ else return v -- | A lookup function which simply returns 'Maybe' wrapped in the calling 'MonadIO' -- context, to accomodate the more conventional users find :: (Eq k, Hashable k, MonadIO m) => TTLHashTable h k v -> k -> m (Maybe v)-find ht@TTLHashTable {..} k = do+find ht@TTLHashTable {..} k = runMaybeT $ lookup ht k -- | delete an entry from the hash table.@@ -330,7 +329,7 @@ let (mValue', result) = mutator mValue result' = runExcept $ do n <- case (mValue, mValue') of- (Nothing, Just _) -> do+ (Nothing, Just _) -> if numEntries < maxSize then return 1 else throwError HashTableFull@@ -392,18 +391,15 @@ when (numEntries > maxSize) $ failure HashTableTooLarge liftIO $ do writeIORef maxSizeRef_ maxSize- writeIORef renewUponReadRef_ renewUponRead writeIORef defaultTTLRef_ defaultTTL writeIORef gcMaxEntriesRef_ gcMaxEntries getSettings :: (MonadIO m) => TTLHashTable h k v -> m Settings getSettings TTLHashTable {..} = liftIO $ do maxSize <- readIORef maxSizeRef_- renewUponRead <- readIORef renewUponReadRef_ defaultTTL <- readIORef defaultTTLRef_ gcMaxEntries <- readIORef gcMaxEntriesRef_ return Settings { maxSize = maxSize,- renewUponRead = renewUponRead, defaultTTL = defaultTTL, gcMaxEntries = gcMaxEntries }
ttl-hashtables.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: d0820b75d28abc8c49166e916af08aad52803d220fa263f44b784425e46411e7+-- hash: 3388338e9a83ada9651f2f035614cb640965e9628df50285184ab9166923e93a name: ttl-hashtables-version: 1.3.1.1+version: 1.4.0.0 synopsis: Extends hashtables so that entries added can be expired after a TTL description: 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. category: Data