cached-traversable (empty) → 0.1.0.0
raw patch · 4 files changed
+120/−0 lines, 4 filesdep +basedep +binarydep +bytestringsetup-changed
Dependencies added: base, binary, bytestring, containers, directory, filepath, mtl
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- cached-traversable.cabal +32/−0
- src/Data/Traversable/Cached.hs +56/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2014, Thomas Tuegel++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Thomas Tuegel nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ cached-traversable.cabal view
@@ -0,0 +1,32 @@+name: cached-traversable+version: 0.1.0.0+synopsis: Transparent, persistent caching of lazy, traversable structures+description:+ cached-traversable provides persistent (on-disk) caching of lazy, traversable+ structures for any element type which has a Binary instance. Think of it as+ a poor man's acid-state for deterministic programs.+license: BSD3+license-file: LICENSE+author: Thomas Tuegel+maintainer: ttuegel@gmail.com+copyright: 2014 Thomas Tuegel+category: Data+build-type: Simple+cabal-version: >=1.8++source-repository head+ type: git+ location: https://github.com/ttuegel/cached-traversable.git++library+ exposed-modules:+ Data.Traversable.Cached+ build-depends:+ base >=4.6 && <5,+ mtl ==2.1.*,+ binary ==0.5.*,+ bytestring ==0.10.*,+ containers ==0.5.*,+ directory ==1.2.*,+ filepath ==1.3.*+ hs-source-dirs: src
+ src/Data/Traversable/Cached.hs view
@@ -0,0 +1,56 @@+module Data.Traversable.Cached where++import Control.Applicative+import Control.Arrow+import Control.Monad (when)+import qualified Control.Monad.State.Strict as State+import Control.Monad.Trans (liftIO)+import Data.Binary+import qualified Data.ByteString as BS+import qualified Data.ByteString.Lazy as LBS+import Data.Foldable+import Data.Sequence ((|>))+import qualified Data.Sequence as Seq+import Data.Traversable+import System.Directory (doesFileExist, removeFile, renameFile)+import System.FilePath ((<.>))++-- | Traverse the given structure, retrieving values from and storing values in+-- the cache at the given path. Only the element type need be serializable. The+-- struture must be lazy in the values for this to be effective. This does not+-- work well for large structures because the entire cache is rewritten every+-- time a new value is computed. Works best when values are small, but expensive+-- to compute. Atomicity and consistency are weakly guaranteed by writing the+-- cache to a new file every time, and renaming the file into place.+cached :: (Binary a, Traversable t) => FilePath -> t a -> IO (t a)+cached path dat = do+ cacheExists <- doesFileExist path+ cache <- if cacheExists then LBS.readFile path else return LBS.empty+ newExists <- doesFileExist newPath+ when newExists $ removeFile newPath+ as <- State.evalStateT (traverse cached_go dat) (cache, Seq.empty)+ return as+ where+ newPath = path <.> "new"++ appendEncoded = flip (|>) . LBS.toStrict . encode++ flushCache = do+ (oldCache, newCache) <- State.get+ -- When there is data yet to be read in oldCache, the cache is not stale+ -- so there is no reason to write a new one.+ when (LBS.null oldCache) $ liftIO $ do+ forM_ newCache (BS.appendFile newPath)+ renameFile newPath path++ cached_go computed = do+ decoded <- decodeOrFail . fst <$> State.get+ case decoded of+ Left _ -> do+ State.modify $ const LBS.empty *** appendEncoded computed+ seq computed flushCache+ return computed+ Right (nextCache, _, retrieved) -> do+ State.modify $ const nextCache *** appendEncoded retrieved+ seq retrieved flushCache+ return retrieved