|
| 1 | +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
| 2 | + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
| 3 | + |
| 4 | +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
| 5 | +<head> |
| 6 | + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> |
| 7 | + |
| 8 | + <title>gwhalin/Memcached-Java-Client @ GitHub</title> |
| 9 | + |
| 10 | + <style type="text/css"> |
| 11 | + body { |
| 12 | + margin-top: 1.0em; |
| 13 | + background-color: #99da9c; |
| 14 | + font-family: "Helvetica,Arial,FreeSans"; |
| 15 | + color: #000000; |
| 16 | + } |
| 17 | + #container { |
| 18 | + margin: 0 auto; |
| 19 | + width: 700px; |
| 20 | + } |
| 21 | + h1 { font-size: 3.8em; color: #662563; margin-bottom: 3px; } |
| 22 | + h1 .small { font-size: 0.4em; } |
| 23 | + h1 a { text-decoration: none } |
| 24 | + h2 { font-size: 1.5em; color: #662563; } |
| 25 | + h3 { text-align: center; color: #662563; } |
| 26 | + a { color: #662563; } |
| 27 | + .description { font-size: 1.2em; margin-bottom: 30px; margin-top: 30px; font-style: italic;} |
| 28 | + .download { float: right; } |
| 29 | + pre { background: #000; color: #fff; padding: 15px;} |
| 30 | + hr { border: 0; width: 80%; border-bottom: 1px solid #aaa} |
| 31 | + .footer { text-align:center; padding-top:30px; font-style: italic; } |
| 32 | + </style> |
| 33 | + |
| 34 | +</head> |
| 35 | + |
| 36 | +<body> |
| 37 | + <a href="http://github.com/gwhalin/Memcached-Java-Client"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub" /></a> |
| 38 | + |
| 39 | + <div id="container"> |
| 40 | + |
| 41 | + <div class="download"> |
| 42 | + <a href="http://github.com/gwhalin/Memcached-Java-Client/zipball/master"> |
| 43 | + <img border="0" width="90" src="http://github.com/images/modules/download/zip.png"></a> |
| 44 | + <a href="http://github.com/gwhalin/Memcached-Java-Client/tarball/master"> |
| 45 | + <img border="0" width="90" src="http://github.com/images/modules/download/tar.png"></a> |
| 46 | + </div> |
| 47 | + |
| 48 | + <h1><a href="http://github.com/gwhalin/Memcached-Java-Client">Memcached-Java-Client</a> |
| 49 | + <span class="small">by <a href="http://github.com/gwhalin">gwhalin</a></span></h1> |
| 50 | + |
| 51 | + <div class="description"> |
| 52 | + Whalin java memcached client |
| 53 | + </div> |
| 54 | + |
| 55 | + <h2>Install</h2> |
| 56 | +<p>Howto |
| 57 | +===== |
| 58 | + |
| 59 | +Basic Example: |
| 60 | +============== |
| 61 | + |
| 62 | +Lets say you have 3 servers. Server 1 and server 2 have 3GB of space |
| 63 | +and server 3 has 2GB of space for cache. Here is how I would set up |
| 64 | +my client. |
| 65 | + |
| 66 | +import com.danga.MemCached.*; |
| 67 | +public class MyClass { |
| 68 | + |
| 69 | + // create a static client as most installs only need |
| 70 | + // a single instance |
| 71 | + protected static MemCachedClient mcc = new MemCachedClient(); |
| 72 | + |
| 73 | + // set up connection pool once at class load |
| 74 | + static { |
| 75 | + |
| 76 | + // server list and weights |
| 77 | + String[] servers = |
| 78 | + { |
| 79 | + "server1.mydomain.com:1624", |
| 80 | + "server2.mydomain.com:1624", |
| 81 | + "server3.mydomain.com:1624" |
| 82 | + }; |
| 83 | + |
| 84 | + Integer[] weights = { 3, 3, 2 }; |
| 85 | + |
| 86 | + // grab an instance of our connection pool |
| 87 | + SockIOPool pool = SockIOPool.getInstance(); |
| 88 | + |
| 89 | + // set the servers and the weights |
| 90 | + pool.setServers( servers ); |
| 91 | + pool.setWeights( weights ); |
| 92 | + |
| 93 | + // set some basic pool settings |
| 94 | + // 5 initial, 5 min, and 250 max conns |
| 95 | + // and set the max idle time for a conn |
| 96 | + // to 6 hours |
| 97 | + pool.setInitConn( 5 ); |
| 98 | + pool.setMinConn( 5 ); |
| 99 | + pool.setMaxConn( 250 ); |
| 100 | + pool.setMaxIdle( 1000 * 60 * 60 * 6 ); |
| 101 | + |
| 102 | + // set the sleep for the maint thread |
| 103 | + // it will wake up every x seconds and |
| 104 | + // maintain the pool size |
| 105 | + pool.setMaintSleep( 30 ); |
| 106 | + |
| 107 | + // set some TCP settings |
| 108 | + // disable nagle |
| 109 | + // set the read timeout to 3 secs |
| 110 | + // and don't set a connect timeout |
| 111 | + pool.setNagle( false ); |
| 112 | + pool.setSocketTO( 3000 ); |
| 113 | + pool.setSocketConnectTO( 0 ); |
| 114 | + |
| 115 | + // initialize the connection pool |
| 116 | + pool.initialize(); |
| 117 | + |
| 118 | + |
| 119 | + // lets set some compression on for the client |
| 120 | + // compress anything larger than 64k |
| 121 | + mcc.setCompressEnable( true ); |
| 122 | + mcc.setCompressThreshold( 64 * 1024 ); |
| 123 | + } |
| 124 | + |
| 125 | + // from here on down, you can call any of the client calls |
| 126 | + public static void examples() { |
| 127 | + mcc.set( "foo", "This is a test String" ); |
| 128 | + String bar = mcc.get( "foo" ); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +Multi-client Example: |
| 133 | +===================== |
| 134 | + |
| 135 | +If you need to support multiple clients (i.e. Java, PHP, Perl, etc.) |
| 136 | +you need to make a few changes when you are setting things up: |
| 137 | + |
| 138 | + // use a compatible hashing algorithm |
| 139 | + pool.setHashingAlg( SockIOPool.NEW_COMPAT_HASH ); |
| 140 | + |
| 141 | + // store primitives as strings |
| 142 | + // the java client serializes primitives |
| 143 | + // |
| 144 | + // note: this will not help you when it comes to |
| 145 | + // storing non primitives |
| 146 | + mcc.setPrimitiveAsString( true ); |
| 147 | + |
| 148 | + // don't url encode keys |
| 149 | + // by default the java client url encodes keys |
| 150 | + // to sanitize them so they will always work on the server |
| 151 | + // however, other clients do not do this |
| 152 | + mcc.setSanitizeKeys( false ); |
| 153 | + |
| 154 | + |
| 155 | +Failover/Failback Notes: |
| 156 | +======================== |
| 157 | + |
| 158 | +By default the java client will failover to a new server when a server |
| 159 | +dies. It will also failback to the original if it detects that the |
| 160 | +server comes back (it checks the server in a falling off pattern). |
| 161 | + |
| 162 | +If you want to disable this (useful if you have flapping servers), |
| 163 | +there are two settings to handle this. |
| 164 | + |
| 165 | + pool.setFailover( false ); |
| 166 | + pool.setFailback( false ); |
| 167 | + |
| 168 | + |
| 169 | +Serialization: |
| 170 | +============== |
| 171 | + |
| 172 | +For java "native types", which include: |
| 173 | + |
| 174 | +Boolean |
| 175 | +Byte |
| 176 | +String |
| 177 | +Character |
| 178 | +StringBuffer |
| 179 | +StringBuilder |
| 180 | +Short |
| 181 | +Long |
| 182 | +Double |
| 183 | +Float |
| 184 | +Date |
| 185 | +Integer |
| 186 | + |
| 187 | +The client will by default *NOT* use java serialization, and instead |
| 188 | +will serialize using the primitive values to save space. You can |
| 189 | +override this by using the mcc.setPrimitiveAsString( true ), which |
| 190 | +will use the toString representation of the object. |
| 191 | + |
| 192 | +For other java objects, you need to make sure the class implements |
| 193 | +Serializable in order to be able to be stored in the cache. |
| 194 | + |
| 195 | +I would also reccomend that if possible, classes should instead |
| 196 | +implement Externalizable as opposed to Serializable. This allows the |
| 197 | +author of the class to define how objects of that class should |
| 198 | +serialize. In practice at Meetup.com, we saw a 60% reduction in the size |
| 199 | +of our serialized objects by doing this. This means less data to eat up |
| 200 | +cache space and less data to transfer over the network. |
| 201 | + |
| 202 | +Other: |
| 203 | +====== |
| 204 | +See the java docs. |
| 205 | +</p> |
| 206 | +<h2>License</h2> |
| 207 | +<p>BSD</p> |
| 208 | +<h2>Authors</h2> |
| 209 | +<p>Gary Helmling ( [email protected]) <br/>Greg Whalin ( [email protected]) <br/><br/> </p> |
| 210 | +<h2>Contact</h2> |
| 211 | +<p>Greg Whalin ( [email protected]) <br/> </p> |
| 212 | + |
| 213 | + |
| 214 | + <h2>Download</h2> |
| 215 | + <p> |
| 216 | + You can download this project in either |
| 217 | + <a href="http://github.com/gwhalin/Memcached-Java-Client/zipball/master">zip</a> or |
| 218 | + <a href="http://github.com/gwhalin/Memcached-Java-Client/tarball/master">tar</a> formats. |
| 219 | + </p> |
| 220 | + <p>You can also clone the project with <a href="http://git-scm.com">Git</a> |
| 221 | + by running: |
| 222 | + <pre>$ git clone git://github.com/gwhalin/Memcached-Java-Client</pre> |
| 223 | + </p> |
| 224 | + |
| 225 | + <div class="footer"> |
| 226 | + get the source code on GitHub : <a href="http://github.com/gwhalin/Memcached-Java-Client">gwhalin/Memcached-Java-Client</a> |
| 227 | + </div> |
| 228 | + |
| 229 | + </div> |
| 230 | + |
| 231 | + |
| 232 | +</body> |
| 233 | +</html> |
|
0 commit comments