Skip to content

Commit ed5c305

Browse files
author
root
committed
Fix issue gwhalin#50
1 parent a0c17f9 commit ed5c305

File tree

6 files changed

+44
-10
lines changed

6 files changed

+44
-10
lines changed

src/main/java/com/schooner/MemCached/AscIIClient.java

100644100755
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ public boolean set(String key, Object value, Date expiry) {
243243
public boolean set(String key, Object value, Date expiry, Integer hashCode) {
244244
return set("set", key, value, expiry, hashCode, 0L, primitiveAsString);
245245
}
246+
247+
public boolean set(String key, Object value, Date expiry, Integer hashCode, boolean asString) {
248+
return set("set", key, value, expiry, hashCode, 0L, asString);
249+
}
246250

247251
public boolean add(String key, Object value) {
248252
return set("add", key, value, null, null, 0L, primitiveAsString);
@@ -334,6 +338,8 @@ public boolean replace(String key, Object value, Date expiry, Integer hashCode)
334338
* expiration
335339
* @param hashCode
336340
* if not null, then the int hashcode to use
341+
* @param asString
342+
* if true, then store all primitives as their string value
337343
* @return true/false indicating success
338344
*/
339345
private boolean set(String cmdname, String key, Object value, Date expiry, Integer hashCode, Long casUnique,

src/main/java/com/schooner/MemCached/AscIIUDPClient.java

100644100755
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ public boolean set(String key, Object value, Date expiry) {
156156
public boolean set(String key, Object value, Date expiry, Integer hashCode) {
157157
return set("set", key, value, expiry, hashCode, 0L);
158158
}
159+
160+
public boolean set(String key, Object value, Date expiry, Integer hashCode, boolean asString) {
161+
return set("set", key, value, expiry, hashCode, 0L);
162+
}
159163

160164
public boolean add(String key, Object value) {
161165
return set("add", key, value, null, null, 0L);

src/main/java/com/schooner/MemCached/BinaryClient.java

100644100755
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ public boolean set(String key, Object value, Date expiry) {
238238
public boolean set(String key, Object value, Date expiry, Integer hashCode) {
239239
return set(OPCODE_SET, key, value, expiry, hashCode, 0L, primitiveAsString);
240240
}
241+
242+
public boolean set(String key, Object value, Date expiry, Integer hashCode, boolean asString) {
243+
return set(OPCODE_SET, key, value, expiry, hashCode, 0L, asString);
244+
}
241245

242246
public boolean add(String key, Object value) {
243247
return set(OPCODE_ADD, key, value, null, null, 0L, primitiveAsString);
@@ -370,7 +374,7 @@ private boolean set(byte opcode, String key, Object value, Date expiry, Integer
370374

371375
try {
372376
// store flags
373-
int flags = NativeHandler.getMarkerFlag(value);
377+
int flags = asString ? MemCachedClient.MARKER_STRING : NativeHandler.getMarkerFlag(value);
374378
byte[] buf = key.getBytes();
375379
sock.writeBuf.clear();
376380
sock.writeBuf.put(MAGIC_REQ);

src/main/java/com/whalin/MemCached/MemCachedClient.java

100644100755
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,26 @@ public boolean set(String key, Object value, Date expiry) {
665665
public boolean set(String key, Object value, Date expiry, Integer hashCode) {
666666
return client.set(key, value, expiry, hashCode);
667667
}
668+
669+
/**
670+
* Stores data on the server; the key, value, and an expiration time are
671+
* specified.
672+
*
673+
* @param key
674+
* key to store data under
675+
* @param value
676+
* value to store
677+
* @param expiry
678+
* when to expire the record
679+
* @param hashCode
680+
* if not null, then the int hashcode to use
681+
* @param asString
682+
* if true, then store all primitives as their string value
683+
* @return true, if the data was successfully stored
684+
*/
685+
public boolean set(String key, Object value, Date expiry, Integer hashCode, boolean asString) {
686+
return client.set(key, value, expiry, hashCode, asString);
687+
}
668688

669689
/**
670690
* Adds data to the server; only the key and the value are specified.
@@ -834,7 +854,7 @@ public boolean storeCounter(String key, Long counter, Date date) {
834854
* @return true/false indicating success
835855
*/
836856
public boolean storeCounter(String key, Long counter, Date date, Integer hashCode) {
837-
return set(key, counter, date, hashCode);
857+
return set(key, counter, date, hashCode, true);
838858
}
839859

840860
/**

src/test/java/com/schooner/MemCached/MemCachedClientAsciiTest.java

100644100755
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,13 @@ public void testSetString() {
183183

184184
public void testStoreCounterStringLong() {
185185
mc.storeCounter("foo", 10L);
186-
Long s = (Long) mc.get("foo");
186+
Long s = Long.parseLong(mc.get("foo").toString());
187187
assertTrue(s == 10L);
188188
}
189189

190190
public void testStoreCounterStringLongInteger() {
191191
mc.storeCounter("foo", 10L, "foo".hashCode());
192-
Long s = (Long) mc.get("foo");
192+
Long s = Long.parseLong(mc.get("foo").toString());
193193
assertTrue(s == 10L);
194194
}
195195

@@ -830,8 +830,8 @@ public void testStoreCounterWithoutAsString() {
830830
final String key = "testKey" + Math.random();
831831
mc.storeCounter(key, 3L);
832832
mc.incr(key);
833-
long value = (Long) mc.getCounter(key);
834-
assertEquals(value, -1L);
833+
long value = mc.getCounter(key);
834+
assertEquals(value, 4L);
835835
}
836836

837837
public void testSpecialCharacterSetString() {

src/test/java/com/schooner/MemCached/MemCachedClientBinaryTest.java

100644100755
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,13 @@ public void testSetExp() {
314314

315315
public void testStoreCounterStringLong() {
316316
mc.storeCounter("foo", 10L);
317-
Long s = (Long) mc.get("foo");
317+
Long s = Long.parseLong(mc.get("foo").toString());
318318
assertTrue(s == 10L);
319319
}
320320

321321
public void testStoreCounterStringLongInteger() {
322322
mc.storeCounter("foo", 10L, "foo".hashCode());
323-
Long s = (Long) mc.get("foo");
323+
Long s = Long.parseLong(mc.get("foo").toString());
324324
assertTrue(s == 10L);
325325
}
326326

@@ -986,8 +986,8 @@ public void testStoreCounterWithoutAsString() {
986986
final String key = "testKey" + Math.random();
987987
mc.storeCounter(key, 3L);
988988
mc.incr(key);
989-
long value = (Long) mc.getCounter(key);
990-
assertEquals(value, -1L);
989+
long value = mc.getCounter(key);
990+
assertEquals(value, 4L);
991991
}
992992

993993
//

0 commit comments

Comments
 (0)