@@ -46,7 +46,7 @@ type Flags = Word32
46
46
47
47
data Expiry =
48
48
Never |
49
- Seconds Word32 |
49
+ Seconds Word32 | -- ^ Limited at run-time to 2592000 seconds (30 days).
50
50
Date UTCTime
51
51
deriving (Show )
52
52
-- figure out how to limit seconds to the memcached limit of 30 days.
@@ -76,7 +76,7 @@ store :: (Key k, Serializable s) => String -> Connection -> Expiry -> Maybe Flag
76
76
store action (Connection handle) expiry flags key val = do
77
77
let valstr = serialize val
78
78
let bytes = B. length valstr
79
- exptime <- expiryToWord expiry
79
+ let exptime = expiryToWord expiry
80
80
let cmd = unwords [action, toKey key, showFlags flags, show exptime, show bytes]
81
81
hPutNetLn handle cmd
82
82
hBSPutNetLn handle valstr
@@ -123,24 +123,18 @@ delete (Connection handle) key = do
123
123
response <- hGetNetLn handle
124
124
return (response == " DELETED" )
125
125
126
- expiryToWord :: Expiry -> IO Word32
126
+ expiryToWord :: Expiry -> Word32
127
127
expiryToWord expiry = do
128
128
case expiry of
129
- Never -> return 0
130
- Date d -> return $ floor $ utcTimeToPOSIXSeconds d
129
+ Never -> 0
130
+ Date d -> floor ( utcTimeToPOSIXSeconds d)
131
131
Seconds s -> safeMemcachedSeconds s
132
132
133
133
thirtyDays = 30 * 24 * 60 * 60
134
134
135
- safeMemcachedSeconds :: Word32 -> IO Word32
136
- safeMemcachedSeconds seconds = do
137
- if seconds <= thirtyDays
138
- -- fits within memcached "relative" range
139
- then return seconds
140
- -- "absolute" range. convert to a Unix time
141
- else (+ seconds) . floor <$> getPOSIXTime
135
+ -- | Prevents accidental converstion to Unix time by memcached
136
+ safeMemcachedSeconds :: Word32 -> Word32
137
+ safeMemcachedSeconds seconds = min seconds thirtyDays
142
138
143
139
showFlags Nothing = " 0"
144
140
showFlags (Just f) = show f
145
-
146
- -- vim: set ts=2 sw=2 et :
0 commit comments