date-cache-0.3.0/0000755000000000000000000000000012021004772011651 5ustar0000000000000000date-cache-0.3.0/date-cache.cabal0000644000000000000000000000140212021004772014610 0ustar0000000000000000Name: date-cache Version: 0.3.0 Author: Kazu Yamamoto Maintainer: Kazu Yamamoto 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 date-cache-0.3.0/LICENSE0000644000000000000000000000276512021004772012670 0ustar0000000000000000Copyright (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. date-cache-0.3.0/Setup.hs0000644000000000000000000000005612021004772013306 0ustar0000000000000000import Distribution.Simple main = defaultMain date-cache-0.3.0/System/0000755000000000000000000000000012021004772013135 5ustar0000000000000000date-cache-0.3.0/System/Date/0000755000000000000000000000000012021004772014012 5ustar0000000000000000date-cache-0.3.0/System/Date/Cache.hs0000644000000000000000000000431312021004772015352 0ustar0000000000000000-- | -- 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