diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2014-03-20 03:19:14 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-03-20 03:19:14 +0400 |
commit | 95f519df298c98d966c6408cac33cdd57b8e8a03 (patch) | |
tree | e6b0fb3ba6dafd6771cfc5084fcb3f7b1a7e26da /src/Network/BitTorrent/Internal/Cache.hs | |
parent | 17949276fbd32ab75bcb18016210b6947df54ed1 (diff) |
Add instance ToJSON TrackerEntry
Diffstat (limited to 'src/Network/BitTorrent/Internal/Cache.hs')
-rw-r--r-- | src/Network/BitTorrent/Internal/Cache.hs | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/Network/BitTorrent/Internal/Cache.hs b/src/Network/BitTorrent/Internal/Cache.hs index 8c74467a..b77e5e82 100644 --- a/src/Network/BitTorrent/Internal/Cache.hs +++ b/src/Network/BitTorrent/Internal/Cache.hs | |||
@@ -32,6 +32,7 @@ module Network.BitTorrent.Internal.Cache | |||
32 | ) where | 32 | ) where |
33 | 33 | ||
34 | import Control.Applicative | 34 | import Control.Applicative |
35 | import Data.Aeson | ||
35 | import Data.Monoid | 36 | import Data.Monoid |
36 | import Data.Default | 37 | import Data.Default |
37 | import Data.Time | 38 | import Data.Time |
@@ -55,6 +56,19 @@ data Cached a = Cached | |||
55 | 56 | ||
56 | -- INVARIANT: minUpdateInterval <= updateInterval | 57 | -- INVARIANT: minUpdateInterval <= updateInterval |
57 | 58 | ||
59 | -- | TODO exsample | ||
60 | instance ToJSON a => ToJSON (Cached a) where | ||
61 | toJSON Cached {..} | ||
62 | | currentTime < expireTime = object | ||
63 | [ "observed" .= posixSecondsToUTCTime lastUpdated | ||
64 | , "expired" .= posixSecondsToUTCTime expireTime | ||
65 | , "data" .= cachedData | ||
66 | ] | ||
67 | | otherwise = String "cached data expired" | ||
68 | where | ||
69 | expireTime = currentTime + updateInterval | ||
70 | currentTime = unsafePerformIO getPOSIXTime | ||
71 | |||
58 | instance Default (Cached a) where | 72 | instance Default (Cached a) where |
59 | def = mempty | 73 | def = mempty |
60 | 74 | ||