lazy-cache: Library for caching IO action that leverages on GHC RTS implementation

[ library, mpl, system ] [ Propose Tags ]

The library provides an interface for caching the results of IO actions. This library relies on a blackholing — mechanism that is used to implement lazy evaluation for avoiding concurrent runs of the action (this is where the name of the library comes from).

The library provides caching mechanismsl with the following properties:

  1. when the function is called the successful result is cached by no more than a provided time;

  2. if the function ends with an exception, it will be rethrown, and the result will not be cached;

  3. if the function will be called with the same input value it immediately gets cached result;

  4. if functions are called concurrently with the different input values they will not block each other;

  5. if functions will be called concurrently with the same argument then only one will act, others will wait until the first one is finished and will either return the result once it's received, or will rethrow an exception if the first one exits with an exception;

  6. outdated values are cleared only on accesses to the cache, so if no action is called the data will not be cleared.

The main entry point of the library is System.Cache module, it explains how to use the library and provides the public API, that does not depend on the actual store implementation.

The library provides an interface for caching, and three implementations:

  1. System.Cache.Impl.Ghc — the main one based on GHC system;

  2. System.Cache.Impl.MVar — implementation based on library functions;

  3. System.Cache.Impl.NoCache — implementation that disables caching, but keeps the same interface

If you want to implement your own caching mechanism, or use internal API, you can refer to the System.Cache.Internal.Interface module for an additional documentation.

[Skip to Readme]


Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Versions [RSS]
Change log
Dependencies base (>=4.12 && <5), clock (>=0.8), hashable (>= && <1.5), psqueues (>=0.2 && <0.3) [details]
License MPL-2.0
Copyright 2019-2022 (C) Фонд "Талант и Успех"
Author Alexander Vershilov
Category System
Source repo head: git clone
Uploaded by AlexanderVershilov at 2023-01-06T06:54:59Z
Distributions NixOS:
Downloads 41 total (2 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for lazy-cache-

[back to package description]

Library for caching values.

The library allows to cache result of an asynchronous computation for no longer than a given period of time.

How to use

In order to use the package you should add the package to the dependency section of the cabal file of your package.

In order to use the cache you should create a 'Handle'

{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE BlockArguments #-}
import Control.Concurrent

import System.Cache qualified as Cache
import System.Cache.Impl.Ghc qualified as Cache.Ghc
import System.Clock.Seconds

main :: IO ()
main = do
   cache <- do
     Cache.mkConfig 60 Monotonic
   -- we create a cached version of computation
   -- in order to hide implementation
   let cachedTimeout input = Cache.requestOr cache input \i -> do
         threadDelay $ i * 1_000_000
         pure i
   -- We use our cached function
   print (cachedTimeout 1)
   print (cachedTimeout 1)
   print (cachedTimeout 2)
sh-3.2$ runhaskell local.hs | ts -S
00:00:03 2
00:00:03 2
00:00:06 3

When running the program first we wait for 2 seconds, the next call returns immediately. The third one is long as it uses a new input value.

Developing the package

cabal configure
cabal build