hxt-cache 0.0.3 → 0.0.4
raw patch · 2 files changed
+47/−11 lines, 2 filesdep ~containersdep ~unixPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: containers, unix
API changes (from Hackage documentation)
+ Text.XML.HXT.Arrow.XmlCache: isInCache :: Attributes -> IOStateArrow s String ClockTime
+ Text.XML.HXT.Arrow.XmlCache: isInCache' :: CacheConfig -> String -> IO (Maybe ClockTime)
Files
- hxt-cache.cabal +3/−3
- src/Text/XML/HXT/Arrow/XmlCache.hs +44/−8
hxt-cache.cabal view
@@ -1,5 +1,5 @@ name: hxt-cache-version: 0.0.3+version: 0.0.4 license: OtherLicense license-file: LICENCE maintainer: Uwe Schmidt <uwe@fh-wedel.de>@@ -29,7 +29,7 @@ build-depends: base >= 4 && < 5, bytestring >= 0.9 && < 1, binary >= 0.5 && < 1,- containers >= 0.3 && < 1,+ containers >= 0.2 && < 1, deepseq >= 1.1 && < 2, directory >= 1 && < 2, filepath >= 1.1 && < 2,@@ -38,6 +38,6 @@ hxt-binary >= 0 && < 1, old-locale >= 1 && < 2, old-time >= 1 && < 2,- unix >= 2.4 && < 3,+ unix >= 2.3 && < 3, SHA >= 1.4 && < 2
src/Text/XML/HXT/Arrow/XmlCache.hs view
@@ -26,6 +26,8 @@ , sha1HashValue , sha1HashString , CacheConfig(..)+ , isInCache+ , isInCache' ) where @@ -93,10 +95,6 @@ where options = addEntries userOptions defaultOptions- defaultOptions = [ ( a_compress, v_0 )- , ( a_cache, "./.cache" )- , ( a_document_age, "" )- ] compr = optionIsSet a_compress options withCache = isJust . lookup a_cache $ userOptions cacheDir = lookup1 a_cache options@@ -110,8 +108,31 @@ , c_age = cacheAge } +defaultOptions :: [(String, String)]+defaultOptions = [ ( a_compress, v_0 )+ , ( a_cache, "./.cache" )+ , ( a_document_age, "" )+ ]+ -- ------------------------------------------------------------ +-- | Arrow for checking if a document is in the cache.+-- The arrow fails if document not there, else the file modification time is returned.++isInCache :: Attributes -> IOStateArrow s String ClockTime+isInCache options = arrIO ( isInCache' (CC { c_dir = lookup1 a_cache (addEntries options defaultOptions)+ , c_compress = undefined+ , c_age = 0+ }+ )+ )+ >>>+ arr maybeToList+ >>>+ unlistA+ +-- ------------------------------------------------------------+ data CacheConfig = CC { c_dir :: FilePath , c_compress :: Bool , c_age :: Integer@@ -128,6 +149,9 @@ where cf = uncurry (</>) $ cacheFile cc src + is200 = hasAttrValue transferStatus (== "200")+ is304 = hasAttrValue transferStatus (== "304")+ readDocumentFromCache = traceMsg 1 ("cache hit for " ++ show src ++ " reading " ++ show cf) >>>@@ -152,8 +176,6 @@ , this :-> traceMsg 1 "transfer status /= 200, page not cached" ] )- where- is200 = hasAttrValue transferStatus (== "200") readDocumentCond mt = traceMsg 1 ("cache out of date, read original document if modified " ++ show src)@@ -167,7 +189,7 @@ >>> readDocumentFromCache )- , documentStatusOk :-> ( traceMsg 1 "document read and cache updated"+ , is200 :-> ( traceMsg 1 "document read and cache updated" >>> perform (writeCache cc src) )@@ -177,7 +199,6 @@ ) ] where- is304 = hasAttrValue transferStatus (== "304") condOpts t = [("curl--header", "If-Modified-Since: " ++ fmtTime t)] -- [(a_if_modified_since, fmtTime t)] fmtTime = formatCalendarTime defaultTimeLocale rfc822DateFormat . toUTCTime@@ -230,6 +251,21 @@ cacheFile cc f = (c_dir cc </> fd, fn) where (fd, fn) = splitAt 2 . sha1HashString $ f++-- ------------------------------------------------------------++isInCache' :: CacheConfig -> String -> IO (Maybe ClockTime)+isInCache' cc f = do+ h <- cacheHit cc' cf+ return $+ case h of+ Just (Just t) -> Just t+ _ -> Nothing+ where+ cf = uncurry (</>) $ cacheFile cc f+ cc' = cc { c_age = 0 }++-- ------------------------------------------------------------ -- result interpretation for cacheHit --