|
| 1 | +-- Memcached interface. |
| 2 | +-- Copyright (C) 2005 Evan Martin <[email protected]> |
| 3 | + |
| 4 | +module Network.Memcache.Protocol ( |
| 5 | + Server, |
| 6 | + connect,disconnect,stats -- server-specific commands |
| 7 | +) where |
| 8 | + |
| 9 | +-- TODO: |
| 10 | +-- - use exceptions where appropriate for protocol errors |
| 11 | +-- - expiration time in store |
| 12 | + |
| 13 | +import Network.Memcache |
| 14 | +import qualified Network |
| 15 | +import Network.Memcache.Key |
| 16 | +import Network.Memcache.Serializable |
| 17 | +import System.IO |
| 18 | + |
| 19 | +-- | Gather results from action until condition is true. |
| 20 | +ioUntil :: (a -> Bool) -> IO a -> IO [a] |
| 21 | +ioUntil stop io = do |
| 22 | + val <- io |
| 23 | + if stop val then return [] |
| 24 | + else do more <- ioUntil stop io |
| 25 | + return (val:more) |
| 26 | + |
| 27 | +-- | Put out a line with \r\n terminator. |
| 28 | +hPutNetLn :: Handle -> String -> IO () |
| 29 | +hPutNetLn h str = hPutStr h (str ++ "\r\n") |
| 30 | + |
| 31 | +-- | Get a line, stripping \r\n terminator. |
| 32 | +hGetNetLn :: Handle -> IO [Char] |
| 33 | +hGetNetLn h = do |
| 34 | + str <- ioUntil (== '\r') (hGetChar h) |
| 35 | + hGetChar h -- read following newline |
| 36 | + return str |
| 37 | + |
| 38 | +-- | Put out a command (words with terminator) and flush. |
| 39 | +hPutCommand :: Handle -> [String] -> IO () |
| 40 | +hPutCommand h strs = hPutNetLn h (unwords strs) >> hFlush h |
| 41 | + |
| 42 | +newtype Server = Server { sHandle :: Handle } |
| 43 | + |
| 44 | +-- connect :: String -> Network.Socket.PortNumber -> IO Server |
| 45 | +connect :: Network.HostName -> Network.PortNumber -> IO Server |
| 46 | +connect host port = do |
| 47 | + handle <- Network.connectTo host (Network.PortNumber port) |
| 48 | + return (Server handle) |
| 49 | + |
| 50 | +disconnect :: Server -> IO () |
| 51 | +disconnect = hClose . sHandle |
| 52 | + |
| 53 | +stats :: Server -> IO [(String, String)] |
| 54 | +stats (Server handle) = do |
| 55 | + hPutCommand handle ["stats"] |
| 56 | + statistics <- ioUntil (== "END") (hGetNetLn handle) |
| 57 | + return $ map (tupelize . stripSTAT) statistics where |
| 58 | + stripSTAT ('S':'T':'A':'T':' ':x) = x |
| 59 | + stripSTAT x = x |
| 60 | + tupelize line = case words line of |
| 61 | + (key:rest) -> (key, unwords rest) |
| 62 | + [] -> (line, "") |
| 63 | + |
| 64 | +store :: (Key k, Serializable s) => String -> Server -> k -> s -> IO Bool |
| 65 | +store action (Server handle) key val = do |
| 66 | + let flags = (0::Int) |
| 67 | + let exptime = (0::Int) |
| 68 | + let valstr = toString val |
| 69 | + let bytes = length valstr |
| 70 | + let cmd = unwords [action, toKey key, show flags, show exptime, show bytes] |
| 71 | + hPutNetLn handle cmd |
| 72 | + hPutNetLn handle valstr |
| 73 | + hFlush handle |
| 74 | + response <- hGetNetLn handle |
| 75 | + return (response == "STORED") |
| 76 | + |
| 77 | +getOneValue :: Handle -> IO (Maybe String) |
| 78 | +getOneValue handle = do |
| 79 | + s <- hGetNetLn handle |
| 80 | + case words s of |
| 81 | + ["VALUE", _, _, sbytes] -> do |
| 82 | + let count = read sbytes |
| 83 | + val <- sequence $ take count (repeat $ hGetChar handle) |
| 84 | + return $ Just val |
| 85 | + _ -> return Nothing |
| 86 | + |
| 87 | +incDec :: (Key k) => String -> Server -> k -> Int -> IO (Maybe Int) |
| 88 | +incDec cmd (Server handle) key delta = do |
| 89 | + hPutCommand handle [cmd, toKey key, show delta] |
| 90 | + response <- hGetNetLn handle |
| 91 | + case response of |
| 92 | + "NOT_FOUND" -> return Nothing |
| 93 | + x -> return $ Just (read x) |
| 94 | + |
| 95 | + |
| 96 | +instance Memcache Server where |
| 97 | + set = store "set" |
| 98 | + add = store "add" |
| 99 | + replace = store "replace" |
| 100 | + |
| 101 | + get (Server handle) key = do |
| 102 | + hPutCommand handle ["get", toKey key] |
| 103 | + val <- getOneValue handle |
| 104 | + case val of |
| 105 | + Nothing -> return Nothing |
| 106 | + Just val -> do |
| 107 | + hGetNetLn handle |
| 108 | + hGetNetLn handle |
| 109 | + return $ fromString val |
| 110 | + |
| 111 | + delete (Server handle) key delta = do |
| 112 | + hPutCommand handle [toKey key, show delta] |
| 113 | + response <- hGetNetLn handle |
| 114 | + return (response == "DELETED") |
| 115 | + |
| 116 | + incr = incDec "incr" |
| 117 | + decr = incDec "decr" |
| 118 | + |
| 119 | +-- vim: set ts=2 sw=2 et : |
0 commit comments