2020-10-16 07:22:48 +03:00
|
|
|
module Urbit.Vere.Ames.LaneCache (cache) where
|
2020-09-27 01:55:10 +03:00
|
|
|
|
|
|
|
import Urbit.Prelude
|
|
|
|
|
|
|
|
import Urbit.Noun.Time
|
|
|
|
|
|
|
|
expiry :: Gap
|
|
|
|
expiry = (2 * 60) ^. from secs
|
|
|
|
|
2020-10-16 07:22:48 +03:00
|
|
|
cache :: forall a b m n
|
|
|
|
. (Ord a, MonadIO m, MonadIO n)
|
|
|
|
=> (a -> m b)
|
|
|
|
-> n (a -> m b)
|
|
|
|
cache act = do
|
|
|
|
cas <- newTVarIO (mempty :: Map a (Wen, b))
|
2020-09-27 01:55:10 +03:00
|
|
|
|
2020-10-16 07:22:48 +03:00
|
|
|
let fun x = lookup x <$> readTVarIO cas >>= \case
|
|
|
|
Nothing -> thru
|
|
|
|
Just (t, v) -> do
|
|
|
|
t' <- io now
|
|
|
|
if gap t' t > expiry
|
|
|
|
then thru
|
|
|
|
else pure v
|
|
|
|
where
|
|
|
|
thru :: m b
|
|
|
|
thru = do
|
|
|
|
t <- io now
|
|
|
|
v <- act x
|
|
|
|
atomically $ modifyTVar' cas (insertMap x (t, v))
|
|
|
|
pure v
|
2020-09-27 01:55:10 +03:00
|
|
|
|
2020-10-16 07:22:48 +03:00
|
|
|
pure fun
|