1
- module Network.Memcache.Cluster where
1
+ module Network.Memcache.Cluster (
2
+ Cluster (.. ),
3
+ Distribute ,
4
+ simple
5
+ ) where
2
6
3
7
import Network.Memcache.Server
4
8
import Network.Memcache.Key
@@ -13,10 +17,7 @@ data Cluster =
13
17
distribute :: Distribute
14
18
}
15
19
16
- type Distribute = [Server NoConnection ] -> String -> Server NoConnection
17
-
18
- choose :: Cluster -> String -> Server NoConnection
19
- choose (Cluster ss dist) keyable = dist ss (toKey keyable)
20
+ type Distribute = [Server AutoConnection ] -> String -> Server AutoConnection
20
21
21
22
-- TODO: ketama (requires an MD5 library)
22
23
-- Reference: http://www.last.fm/user/RJ/journal/2007/04/10/rz_libketama_-_a_consistent_hashing_algo_for_memcache_clients
@@ -25,21 +26,20 @@ choose (Cluster ss dist) keyable = dist ss (toKey keyable)
25
26
26
27
-- this seems ok for short keys
27
28
-- TODO: prevent empty list of servers from getting here
28
- simple :: String -> [Server NoConnection ] -> Server NoConnection
29
+ simple :: String -> [Server AutoConnection ] -> Server AutoConnection
29
30
simple k (s : [] ) = s
30
31
simple k ss = head . snd $ splitAt idx ss
31
32
where
32
33
hash = foldl' (\ c i -> i + c * 31 ) 0 . map fromEnum
33
34
idx = (hash . toKey) k `mod` length ss
34
35
35
36
instance Memcache Cluster where
36
- get cl k = P. get (getConnection cl k)
37
- delete cl k = P. delete (getConnection cl k)
38
- set cl e f k = P. store " set" (getConnection cl k) e f
39
- -- add (Connected s e f) = P.store "add" s e f
40
- -- replace (Connected s e f) = P.store "replace" s e f
41
- -- incr (Connected s _ _) = P.incDec "incr" s
42
- -- decr (Connected s _ _) = P.incDec "decr" s
43
-
44
- -- TODO: figure out how to connect the server. They're all Server NoConnection!
45
- getConnection cl key = cConn (distribute cl (servers cl) key)
37
+ get cl k = get (getServer cl k) k
38
+ delete cl k = delete (getServer cl k) k
39
+ set cl k = set (getServer cl k) k
40
+ add cl k = add (getServer cl k) k
41
+ replace cl k = replace (getServer cl k) k
42
+ incr cl k = incr (getServer cl k) k
43
+ decr cl k = decr (getServer cl k) k
44
+
45
+ getServer cl key = distribute cl (servers cl) (toKey key)
0 commit comments