@@ -14,6 +14,8 @@ import System.IO
14
14
import qualified Data.ByteString as B
15
15
import Data.ByteString (ByteString )
16
16
import Data.Word
17
+ import Data.Time
18
+ import Data.Time.Clock.POSIX
17
19
18
20
-- | Gather results from action until condition is true.
19
21
ioUntil :: (a -> Bool ) -> IO a -> IO [a ]
@@ -41,8 +43,14 @@ hPutCommand h strs = hPutNetLn h (unwords strs) >> hFlush h
41
43
42
44
type Flags = Word32
43
45
46
+ data Expiry = Seconds Word32 | Date UTCTime
47
+ deriving (Show )
48
+ -- figure out how to limit seconds to the memcached limit of 30 days.
49
+
44
50
newtype Connection = Connection { sHandle :: Handle }
45
51
52
+ never = Seconds 0
53
+
46
54
-- connect :: String -> Network.Socket.PortNumber -> IO Connection
47
55
connect :: Network. HostName -> Network. PortNumber -> IO Connection
48
56
connect host port = do
@@ -63,11 +71,11 @@ stats (Connection handle) = do
63
71
(key: rest) -> (key, unwords rest)
64
72
[] -> (line, " " )
65
73
66
- store :: (Key k , Serializable s ) => String -> Connection -> Word32 -> Flags -> k -> s -> IO Bool
74
+ store :: (Key k , Serializable s ) => String -> Connection -> Expiry -> Flags -> k -> s -> IO Bool
67
75
store action (Connection handle) exptime flags key val = do
68
76
let valstr = serialize val
69
77
let bytes = B. length valstr
70
- let cmd = unwords [action, toKey key, show flags, show exptime, show bytes]
78
+ let cmd = unwords [action, toKey key, show flags, show (expiryToWord exptime) , show bytes]
71
79
hPutNetLn handle cmd
72
80
hBSPutNetLn handle valstr
73
81
hFlush handle
@@ -84,8 +92,8 @@ getOneValue handle = do
84
92
return $ Just val
85
93
_ -> return Nothing
86
94
87
- incDec :: (Key k ) => String -> Connection -> Word32 -> k -> Word32 -> IO (Maybe Int )
88
- incDec cmd (Connection handle) exptime key delta = do
95
+ incDec :: (Key k ) => String -> Connection -> k -> Word32 -> IO (Maybe Int )
96
+ incDec cmd (Connection handle) key delta = do
89
97
hPutCommand handle [cmd, toKey key, show delta]
90
98
response <- hGetNetLn handle
91
99
case response of
@@ -107,5 +115,8 @@ delete (Connection handle) key = do
107
115
response <- hGetNetLn handle
108
116
return (response == " DELETED" )
109
117
118
+ expiryToWord :: Expiry -> Word32
119
+ expiryToWord (Seconds s) = max (30 * 24 * 60 * 60 ) s
120
+ expiryToWord (Date d) = floor (utcTimeToPOSIXSeconds d)
110
121
111
122
-- vim: set ts=2 sw=2 et :
0 commit comments