async-refresh

Package implementing core logic for refreshing of expiring data.

https://github.com/mtesseract/async-refresh

Version on this page:0.2.0
LTS Haskell 22.14:0.3.0.0
Stackage Nightly 2024-03-28:0.3.0.0
Latest on Hackage:0.3.0.0

See all snapshots async-refresh appears in

BSD-3-Clause licensed by Moritz Schulte
Maintained by [email protected]
This version can be pinned in stack with:async-refresh-0.2.0@sha256:166d24f2e94e6ad44e5689161ae6eb53a91295879e9195528ecee4f1863fd43e,2418

Module documentation for 0.2.0

async-refresh

About

This is Haskell library implementing the logic for refreshing of expiring data according to user-provided actions.

Usage

  • Create a new configuration using newAsyncRefreshConf, providing the action to be used for data refreshing.

  • Adjust the configuration using the asyncRefreshConfSet* functions, in particular using asyncRefreshConfSetCallback.

  • Use newAsyncRefresh to initiate a new thread managing the asynchronous refreshing.

Example

The following IO action produces a TVar which is updated every ten seconds to contain the current time (wrapped in an Either SomeException, because refreshing may fail).

periodicTimeUpdater :: IO (TVar (Either SomeException UTCTime))
periodicTimeUpdater = runStderrLoggingT $ do
  timeStore <- liftIO $ newTVarIO (Left (toException NotFound))
  let conf = newAsyncRefreshConf (RefreshResult <$> liftIO getCurrentTime <*> pure Nothing)
        & asyncRefreshConfSetLabel "CurrentTime updated every 10 seconds"
        & asyncRefreshConfSetDefaultInterval (10 * 10^3)
        & asyncRefreshConfSetCallback (liftIO . atomically . writeTVar timeStore . fmap refreshResult)
  _ <- newAsyncRefresh conf
  return timeStore