Skip to content

Commit fb4b527

Browse files
committed
updated docs and added StringBuilder to our native serialization
1 parent 7dfa75e commit fb4b527

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

doc/HOWTO.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,38 @@ there are two settings to handle this.
111111
pool.setFailback( false );
112112

113113

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.
114146

115147
Other:
116148
======

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)