Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deleteView function for pure and IO version, cached with Maybe param #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lrucaching.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: lrucaching
version: 0.3.3
version: 0.3.4
synopsis: LRU cache
description: Please see README.md
homepage: https://github.com/cocreature/lrucaching#readme
Expand Down
12 changes: 12 additions & 0 deletions src/Data/LruCache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Data.LruCache
, empty
, insert
, insertView
, deleteView
, lookup
) where

Expand Down Expand Up @@ -87,6 +88,17 @@ insert key val c =
, lruQueue = queue
}

-- | Delete an element from the 'LruCache'.
deleteView :: (Hashable k, Ord k) => k -> LruCache k v -> (Maybe v, LruCache k v)
deleteView key c =
case HashPSQ.deleteView key (lruQueue c) of
Nothing -> (Nothing, c)
Just (p,mbOldVal,queue) -> ( Just mbOldVal
, c { lruSize = lruSize c - 1
, lruQueue = queue
}
)

-- | Insert an element into the 'LruCache' returning the evicted
-- element if any.
--
Expand Down
56 changes: 55 additions & 1 deletion src/Data/LruCache/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ automatically when cache entries are evicted
module Data.LruCache.IO
( LruHandle(..)
, cached
, cachedMaybe
, newLruHandle
, StripedLruHandle(..)
, stripedCached
, stripedCachedMaybe
, newStripedLruHandle
, deleteViewIO
, stripedDeleteViewIO
) where

import Control.Applicative ((<$>))
Expand Down Expand Up @@ -48,6 +52,31 @@ cached (LruHandle ref) k io =
atomicModifyIORef' ref $ \c -> (insert k v c, ())
return v

-- | Maybe form of `cached`
cachedMaybe :: (Hashable k, Ord k) => LruHandle k v -> k -> IO (Maybe v) -> IO (Maybe v)
cachedMaybe (LruHandle ref) k io =
do lookupRes <- atomicModifyIORef' ref $ \c ->
case lookup k c of
Nothing -> (c, Nothing)
Just (v, c') -> (c', Just v)
case lookupRes of
Just v -> return $ Just v
Nothing ->
do v <- io
case v of
Nothing -> return Nothing
Just v' -> do
atomicModifyIORef' ref $ \c -> (insert k v' c, ())
return v

-- | Delete an item from the cache and return value of that item.
deleteViewIO :: (Hashable k, Ord k) => LruHandle k v -> k -> IO (Maybe v)
deleteViewIO (LruHandle ref) k =
atomicModifyIORef' ref $ \c ->
case deleteView k c of
(Nothing, c') -> (c', Nothing)
(Just v, c') -> (c', Just v)

-- | Using a stripe of multiple handles can improve the performance in
-- the case of concurrent accesses since several handles can be
-- accessed in parallel.
Expand All @@ -69,4 +98,29 @@ stripedCached ::
stripedCached (StripedLruHandle v) k =
cached (v Vector.! idx) k
where
idx = hash k `mod` Vector.length v
idx = hash k `mod` Vector.length v

-- | Maybe form of `stripedCached`
stripedCachedMaybe ::
(Hashable k, Ord k) =>
StripedLruHandle k v ->
k ->
IO (Maybe v) ->
IO (Maybe v)
stripedCachedMaybe (StripedLruHandle v) k =
cachedMaybe (v Vector.! idx) k
where
idx = hash k `mod` Vector.length v



-- | Striped version of `deleteViewIO`
stripedDeleteViewIO ::
(Hashable k, Ord k) =>
StripedLruHandle k v ->
k ->
IO (Maybe v)
stripedDeleteViewIO (StripedLruHandle v) k =
deleteViewIO (v Vector.! idx) k
where
idx = hash k `mod` Vector.length v