1
1
-- Memcached interface.
2
2
-- Copyright (C) 2005 Evan Martin <martine@danga.com>
3
3
4
- module Network.Memcache.Protocol (
5
- Server ,
6
- connect ,disconnect ,stats -- server-specific commands
7
- ) where
4
+ module Network.Memcache.Protocol where
8
5
9
6
-- TODO:
10
7
-- - use exceptions where appropriate for protocol errors
11
8
-- - expiration time in store
12
9
13
- import Network.Memcache
14
10
import qualified Network
15
11
import Network.Memcache.Key
16
12
import Network.Memcache.Serializable
@@ -42,19 +38,19 @@ hGetNetLn h = fmap init (hGetLine h) -- init gets rid of \r
42
38
hPutCommand :: Handle -> [String ] -> IO ()
43
39
hPutCommand h strs = hPutNetLn h (unwords strs) >> hFlush h
44
40
45
- newtype Server = Server { sHandle :: Handle }
41
+ newtype Connection = Connection { sHandle :: Handle }
46
42
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
49
45
connect host port = do
50
46
handle <- Network. connectTo host (Network. PortNumber port)
51
- return (Server handle)
47
+ return (Connection handle)
52
48
53
- disconnect :: Server -> IO ()
49
+ disconnect :: Connection -> IO ()
54
50
disconnect = hClose . sHandle
55
51
56
- stats :: Server -> IO [(String , String )]
57
- stats (Server handle) = do
52
+ stats :: Connection -> IO [(String , String )]
53
+ stats (Connection handle) = do
58
54
hPutCommand handle [" stats" ]
59
55
statistics <- ioUntil (== " END" ) (hGetNetLn handle)
60
56
return $ map (tupelize . stripSTAT) statistics where
@@ -64,10 +60,9 @@ stats (Server handle) = do
64
60
(key: rest) -> (key, unwords rest)
65
61
[] -> (line, " " )
66
62
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
69
65
let flags = (0 :: Int )
70
- let exptime = (0 :: Int )
71
66
let valstr = serialize val
72
67
let bytes = B. length valstr
73
68
let cmd = unwords [action, toKey key, show flags, show exptime, show bytes]
@@ -87,36 +82,28 @@ getOneValue handle = do
87
82
return $ Just val
88
83
_ -> return Nothing
89
84
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
92
87
hPutCommand handle [cmd, toKey key, show delta]
93
88
response <- hGetNetLn handle
94
89
case response of
95
90
" NOT_FOUND" -> return Nothing
96
91
x -> return $ Just (read x)
97
92
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" )
98
107
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"
121
108
122
109
-- vim: set ts=2 sw=2 et :
0 commit comments