Skip to content

Commit b391038

Browse files
author
John Hann
committed
Rename Server -> Connection.
1 parent 889a2d4 commit b391038

File tree

1 file changed

+26
-39
lines changed

1 file changed

+26
-39
lines changed

Network/Memcache/Protocol.hs

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
-- Memcached interface.
22
-- Copyright (C) 2005 Evan Martin <martine@danga.com>
33

4-
module Network.Memcache.Protocol (
5-
Server,
6-
connect,disconnect,stats -- server-specific commands
7-
) where
4+
module Network.Memcache.Protocol where
85

96
-- TODO:
107
-- - use exceptions where appropriate for protocol errors
118
-- - expiration time in store
129

13-
import Network.Memcache
1410
import qualified Network
1511
import Network.Memcache.Key
1612
import Network.Memcache.Serializable
@@ -42,19 +38,19 @@ hGetNetLn h = fmap init (hGetLine h) -- init gets rid of \r
4238
hPutCommand :: Handle -> [String] -> IO ()
4339
hPutCommand h strs = hPutNetLn h (unwords strs) >> hFlush h
4440

45-
newtype Server = Server { sHandle :: Handle }
41+
newtype Connection = Connection { sHandle :: Handle }
4642

47-
-- connect :: String -> Network.Socket.PortNumber -> IO Server
48-
connect :: Network.HostName -> Network.PortNumber -> IO Server
43+
-- connect :: String -> Network.Socket.PortNumber -> IO Connection
44+
connect :: Network.HostName -> Network.PortNumber -> IO Connection
4945
connect host port = do
5046
handle <- Network.connectTo host (Network.PortNumber port)
51-
return (Server handle)
47+
return (Connection handle)
5248

53-
disconnect :: Server -> IO ()
49+
disconnect :: Connection -> IO ()
5450
disconnect = hClose . sHandle
5551

56-
stats :: Server -> IO [(String, String)]
57-
stats (Server handle) = do
52+
stats :: Connection -> IO [(String, String)]
53+
stats (Connection handle) = do
5854
hPutCommand handle ["stats"]
5955
statistics <- ioUntil (== "END") (hGetNetLn handle)
6056
return $ map (tupelize . stripSTAT) statistics where
@@ -64,10 +60,9 @@ stats (Server handle) = do
6460
(key:rest) -> (key, unwords rest)
6561
[] -> (line, "")
6662

67-
store :: (Key k, Serializable s) => String -> Server -> k -> s -> IO Bool
68-
store action (Server handle) key val = do
63+
store :: (Key k, Serializable s) => String -> Connection -> Int -> Int -> k -> s -> IO Bool
64+
store action (Connection handle) exptime flags key val = do
6965
let flags = (0::Int)
70-
let exptime = (0::Int)
7166
let valstr = serialize val
7267
let bytes = B.length valstr
7368
let cmd = unwords [action, toKey key, show flags, show exptime, show bytes]
@@ -87,36 +82,28 @@ getOneValue handle = do
8782
return $ Just val
8883
_ -> return Nothing
8984

90-
incDec :: (Key k) => String -> Server -> k -> Int -> IO (Maybe Int)
91-
incDec cmd (Server handle) key delta = do
85+
incDec :: (Key k) => String -> Connection -> Int -> Int -> k -> Int -> IO (Maybe Int)
86+
incDec cmd (Connection handle) exptime flags key delta = do
9287
hPutCommand handle [cmd, toKey key, show delta]
9388
response <- hGetNetLn handle
9489
case response of
9590
"NOT_FOUND" -> return Nothing
9691
x -> return $ Just (read x)
9792

93+
get (Connection handle) key = do
94+
hPutCommand handle ["get", toKey key]
95+
val <- getOneValue handle
96+
case val of
97+
Nothing -> return Nothing
98+
Just val -> do
99+
hGetNetLn handle
100+
hGetNetLn handle
101+
return $ deserialize val
102+
103+
delete (Connection handle) key delta = do
104+
hPutCommand handle ["delete", toKey key, show delta]
105+
response <- hGetNetLn handle
106+
return (response == "DELETED")
98107

99-
instance Memcache Server where
100-
set = store "set"
101-
add = store "add"
102-
replace = store "replace"
103-
104-
get (Server handle) key = do
105-
hPutCommand handle ["get", toKey key]
106-
val <- getOneValue handle
107-
case val of
108-
Nothing -> return Nothing
109-
Just val -> do
110-
hGetNetLn handle
111-
hGetNetLn handle
112-
return $ deserialize val
113-
114-
delete (Server handle) key delta = do
115-
hPutCommand handle ["delete", toKey key, show delta]
116-
response <- hGetNetLn handle
117-
return (response == "DELETED")
118-
119-
incr = incDec "incr"
120-
decr = incDec "decr"
121108

122109
-- vim: set ts=2 sw=2 et :

0 commit comments

Comments
 (0)