diff options
| author | Raymond Hogenson <rhogenson@posteo.net> | 2018-11-14 11:41:03 -0500 |
|---|---|---|
| committer | Raymond Hogenson <rhogenson@posteo.net> | 2018-11-14 11:41:03 -0500 |
| commit | 5ed368631ca808d1fc028764ff8ab40e3badb913 (patch) | |
| tree | f77d1b1476b294e0fdc294eaa9d3da8f15f6aa9c /src/Cache.hs | |
| parent | 5d53db5fb872d32a9f9e48f6f3bed396b5a5b4e1 (diff) | |
| download | block-copy-5ed368631ca808d1fc028764ff8ab40e3badb913.tar.zst | |
Maybe I'll develop on my laptop for a bit
Diffstat (limited to 'src/Cache.hs')
| -rw-r--r-- | src/Cache.hs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/Cache.hs b/src/Cache.hs new file mode 100644 index 0000000..d2fd49e --- /dev/null +++ b/src/Cache.hs @@ -0,0 +1,34 @@ +module Cache (Cache, new, read, write) where + +import qualified System.Posix.Types as T +import qualified Data.IORef as IOR +import qualified Data.Set as S +import qualified Data.Map as M + +data WetCache k v = WC + { cTimeStamps :: S.Set (T.EpochTime, k) + , cByKey :: M.Map k (T.EpochTime, v) + , cDirty :: S.Set k + } + +data Cache k v = Cache + { cWetCache :: IOR.IORef (WetCache k v) + , cRead :: k -> IO v + , cWrite :: k -> v -> IO () + , cMaxSize :: Int + } + +new :: (k -> IO v) -> (k -> v -> IO ()) -> Int -> IO Cache +new r w size = Cache <$> IOR.newIORef CI + { cTimeStamps=S.empty, cByKey=M.empty, cDirty=S.empty, cRead=r, cWrite=w + , cMaxSize=size } + +readMiss :: Cache k v -> k -> IO v +readMiss r k = + +read :: Cache k v -> k -> IO v +read r k = do + CI { cByKey, .. } <- IOR.readIORef r + case M.lookup k cByKey of + (_, v) -> return v + Nothing -> readMiss r k |
