packages feed

date-cache (empty) → 0.3.0

raw patch · 4 files changed

+130/−0 lines, 4 filesdep +basedep +bytestringsetup-changed

Dependencies added: base, bytestring

Files

+ LICENSE view
@@ -0,0 +1,29 @@+Copyright (c) 2012, IIJ Innovation Institute Inc.+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 the copyright holders nor the names of its+    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
+ System/Date/Cache.hs view
@@ -0,0 +1,77 @@+-- |+-- Formatting time is slow.+-- This package provides mechanisms to cache formatted date.+module System.Date.Cache (+  -- * Types+    DateCacheConf(..)+  , DateCacheGetter+  , DateCacheCloser+  -- * Date cacher+  , ondemandDateCacher+  , clockDateCacher+  ) where++import Control.Applicative+import Control.Concurrent+import Data.ByteString (ByteString)+import Data.IORef++type DateCacheGetter = IO ByteString+type DateCacheCloser = IO ()++data DateCache t = DateCache {+    timeKey :: !t+  , formattedDate :: !ByteString+  } deriving (Eq, Show)++data DateCacheConf t = DateCacheConf {+    -- | A function to get a time. E.g 'epochTime' and 'getCurrentTime'.+    getTime :: IO t+    -- | A function to format a time.+  , formatDate :: t -> IO ByteString+  }++newDate :: DateCacheConf t -> t -> IO (DateCache t)+newDate setting tm = DateCache tm <$> formatDate setting tm++-- |+-- Date cacher which gets a time and formatted it only when+-- returned getter is executed.+ondemandDateCacher :: Eq t => DateCacheConf t -> IO (DateCacheGetter, DateCacheCloser)+ondemandDateCacher setting = do+    ref <- getTime setting >>= newDate setting >>= newIORef+    return $! (getter ref, closer)+  where+    getter ref = do+        newTm <- getTime setting+        cache <- readIORef ref+        let oldTm = timeKey cache+        if oldTm == newTm then+            return $ formattedDate cache+          else do+            newCache <- newDate setting newTm+            writeIORef ref newCache+            return $ formattedDate newCache+    closer = return ()++-- |+-- Date cacher which gets a time and formatted it every second.+-- This returns a getter.+clockDateCacher :: Eq t => DateCacheConf t -> IO (DateCacheGetter, DateCacheCloser)+clockDateCacher setting = do+    ref <- getTime setting >>= newDate setting >>= newIORef+    tid <- forkIO $ clock ref+    return $! (getter ref, closer tid)+  where+    getter ref = formattedDate <$> readIORef ref+    clock ref = do+        threadDelay 1000000+        tm <- getTime setting+        date <- formatDate setting tm+        let new = DateCache {+                timeKey = tm+              , formattedDate = date+              }+        writeIORef ref new+        clock ref+    closer tid = killThread tid
+ date-cache.cabal view
@@ -0,0 +1,22 @@+Name:                   date-cache+Version:                0.3.0+Author:                 Kazu Yamamoto <kazu@iij.ad.jp>+Maintainer:             Kazu Yamamoto <kazu@iij.ad.jp>+License:                BSD3+License-File:           LICENSE+Synopsis:               Date cacher+Description:            Formatting time is slow. This package provides+                        mechanisms to cache formatted date+Category:               System+Cabal-Version:          >= 1.8+Build-Type:             Simple++Library+  GHC-Options:          -Wall+  Exposed-Modules:      System.Date.Cache+  Build-Depends:        base >= 4 && < 5+                      , bytestring++Source-Repository head+  Type:                 git+  Location:             git://github.com/kazu-yamamoto/logger.git