Skip to content

Commit e0771d7

Browse files
committed
merge from head
1 parent 4de4516 commit e0771d7

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed

doc/HOWTO.txt

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
Howto
2+
=====
3+
4+
Basic Example:
5+
==============
6+
7+
Lets say you have 3 servers. Server 1 and server 2 have 3GB of space
8+
and server 3 has 2GB of space for cache. Here is how I would set up
9+
my client.
10+
11+
import com.danga.MemCached.*;
12+
public class MyClass {
13+
14+
// create a static client as most installs only need
15+
// a single instance
16+
protected static MemCachedClient mcc = new MemCachedClient();
17+
18+
// set up connection pool once at class load
19+
static {
20+
21+
// server list and weights
22+
String[] servers =
23+
{
24+
"server1.mydomain.com:1624",
25+
"server2.mydomain.com:1624",
26+
"server3.mydomain.com:1624"
27+
};
28+
29+
Integer[] weights = { 3, 3, 2 };
30+
31+
// grab an instance of our connection pool
32+
SockIOPool pool = SockIOPool.getInstance();
33+
34+
// set the servers and the weights
35+
pool.setServers( servers );
36+
pool.setWeights( weights );
37+
38+
// set some basic pool settings
39+
// 5 initial, 5 min, and 250 max conns
40+
// and set the max idle time for a conn
41+
// to 6 hours
42+
pool.setInitConn( 5 );
43+
pool.setMinConn( 5 );
44+
pool.setMaxConn( 250 );
45+
pool.setMaxIdle( 1000 * 60 * 60 * 6 );
46+
47+
// set the sleep for the maint thread
48+
// it will wake up every x seconds and
49+
// maintain the pool size
50+
pool.setMaintSleep( 30 );
51+
52+
// set some TCP settings
53+
// disable nagle
54+
// set the read timeout to 3 secs
55+
// and don't set a connect timeout
56+
pool.setNagle( false );
57+
pool.setSocketTO( 3000 );
58+
pool.setSocketConnectTO( 0 );
59+
60+
// initialize the connection pool
61+
pool.initialize();
62+
63+
64+
// lets set some compression on for the client
65+
// compress anything larger than 64k
66+
mcc.setCompressEnable( true );
67+
mcc.setCompressThreshold( 64 * 1024 );
68+
}
69+
70+
// from here on down, you can call any of the client calls
71+
public static void examples() {
72+
mcc.set( "foo", "This is a test String" );
73+
String bar = mcc.get( "foo" );
74+
}
75+
}
76+
77+
Multi-client Example:
78+
=====================
79+
80+
If you need to support multiple clients (i.e. Java, PHP, Perl, etc.)
81+
you need to make a few changes when you are setting things up:
82+
83+
// use a compatible hashing algorithm
84+
pool.setHashingAlg( SockIOPool.NEW_COMPAT_HASH );
85+
86+
// store primitives as strings
87+
// the java client serializes primitives
88+
//
89+
// note: this will not help you when it comes to
90+
// storing non primitives
91+
mcc.setPrimitiveAsString( true );
92+
93+
// don't url encode keys
94+
// by default the java client url encodes keys
95+
// to sanitize them so they will always work on the server
96+
// however, other clients do not do this
97+
mcc.setSanitizeKeys( false );
98+
99+
100+
Failover/Failback Notes:
101+
========================
102+
103+
By default the java client will failover to a new server when a server
104+
dies. It will also failback to the original if it detects that the
105+
server comes back (it checks the server in a falling off pattern).
106+
107+
If you want to disable this (useful if you have flapping servers),
108+
there are two settings to handle this.
109+
110+
pool.setFailover( false );
111+
pool.setFailback( false );
112+
113+
114+
Serialization:
115+
==============
116+
117+
For java "native types", which include:
118+
119+
Boolean
120+
Byte
121+
String
122+
Character
123+
StringBuffer
124+
StringBuilder
125+
Short
126+
Long
127+
Double
128+
Float
129+
Date
130+
Integer
131+
132+
The client will by default *NOT* use java serialization, and instead
133+
will serialize using the primitive values to save space. You can
134+
override this by using the mcc.setPrimitiveAsString( true ), which
135+
will use the toString representation of the object.
136+
137+
For other java objects, you need to make sure the class implements
138+
Serializable in order to be able to be stored in the cache.
139+
140+
I would also reccomend that if possible, classes should instead
141+
implement Externalizable as opposed to Serializable. This allows the
142+
author of the class to define how objects of that class should
143+
serialize. In practice at Meetup.com, we saw a 60% reduction in the size
144+
of our serialized objects by doing this. This means less data to eat up
145+
cache space and less data to transfer over the network.
146+
147+
Other:
148+
======
149+
See the java docs.

src/com/danga/MemCached/NativeHandler.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public class NativeHandler {
106106
public static final int MARKER_SHORT = 9;
107107
public static final int MARKER_DOUBLE = 10;
108108
public static final int MARKER_DATE = 11;
109+
public static final int MARKER_STRINGBUILDER = 12;
109110

110111
public static boolean isHandled( Object value ) {
111112

@@ -114,6 +115,7 @@ public static boolean isHandled( Object value ) {
114115
value instanceof String ||
115116
value instanceof Character ||
116117
value instanceof StringBuffer ||
118+
value instanceof StringBuilder ||
117119
value instanceof Short ||
118120
value instanceof Long ||
119121
value instanceof Double ||
@@ -153,6 +155,9 @@ public static byte[] encode( Object value ) throws Exception {
153155
if ( value instanceof StringBuffer )
154156
return encode( (StringBuffer)value );
155157

158+
if ( value instanceof StringBuilder )
159+
return encode( (StringBuilder)value );
160+
156161
if ( value instanceof Short )
157162
return encode( (Short)value );
158163

@@ -256,6 +261,15 @@ public static byte[] encode( StringBuffer value ) throws Exception {
256261

257262
}
258263

264+
public static byte[] encode( StringBuilder value ) throws Exception {
265+
266+
byte[] b = encode( value.toString() );
267+
b[0] = MARKER_STRINGBUILDER;
268+
269+
return b;
270+
271+
}
272+
259273
public static byte[] encode( Short value ) throws Exception {
260274

261275
byte[] b = encode( (int)value.shortValue() );
@@ -426,6 +440,12 @@ public static StringBuffer decodeStringBuffer( byte[] b ) throws Exception {
426440

427441
}
428442

443+
public static StringBuilder decodeStringBuilder( byte[] b ) throws Exception {
444+
445+
return new StringBuilder( decodeString( b ) );
446+
447+
}
448+
429449
public static Short decodeShort( byte[] b ) throws Exception {
430450

431451
//FIXME: this generates an extra object we don't need

0 commit comments

Comments
 (0)