* @version 2.0
*/
/*******************************************************************************
* Copyright (c) 2009 Schooner Information Technology, Inc.
* All rights reserved.
- *
+ *
* http://www.schoonerinfotech.com/
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
@@ -34,7 +34,7 @@
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
- *
+ *
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
@@ -48,6 +48,7 @@
******************************************************************************/
package com.whalin.MemCached;
+import java.io.Serializable;
import java.util.Date;
import java.util.Map;
@@ -58,6 +59,7 @@
import com.schooner.MemCached.AscIIUDPClient;
import com.schooner.MemCached.BinaryClient;
import com.schooner.MemCached.MemcachedItem;
+import com.schooner.MemCached.TransBytecode;
import com.schooner.MemCached.TransCoder;
/**
@@ -69,68 +71,68 @@
* Here is an example to use schooner memcached client.
*
* Firstly, we should initialize the SockIOPool, which is a connection pool.
- *
+ *
* An example of initializing using defaults:
- *
+ *
*
* static {
* String[] serverlist = { "server1.com:port", "server2.com:port" };
- *
+ *
* SockIOPool pool = SockIOPool.getInstance();
* pool.setServers(serverlist);
* pool.initialize();
* }
*
- *
+ *
*
* Then we can create the client object.
- *
+ *
* To create cache client object:
- *
+ *
*
* MemCachedClient mc = new MemCachedClient();
*
- *
+ *
* To store an object:
- *
+ *
*
* MemCachedClient mc = new MemCachedClient();
* String key = "cacheKey1";
* Object value = SomeClass.getObject();
* mc.set(key, value);
*
- *
+ *
* To delete a cache entry:
- *
+ *
*
* MemCachedClient mc = new MemCachedClient();
* String key = "cacheKey1";
* mc.delete(key);
*
- *
+ *
* To retrieve an object from the cache:
- *
+ *
*
* MemCachedClient mc = new MemCachedClient();
* String key = "key";
* Object value = mc.get(key);
*
- *
+ *
* To retrieve an multiple objects from the cache
- *
+ *
*
* MemCachedClient mc = new MemCachedClient();
* String[] keys = { "key", "key1", "key2" };
* Map<Object> values = mc.getMulti(keys);
*
- *
+ *
* To flush all items in server(s)
- *
+ *
*
* MemCachedClient mc = new MemCachedClient();
* mc.flushAll();
*
- *
+ *
*/
public class MemCachedClient {
@@ -309,7 +311,7 @@ public MemCachedClient() {
/**
* Creates a new instance of MemCachedClient.
* Use binary protocol with TCP.
- *
+ *
* @param binaryProtocal
* whether use binary protocol.
*/
@@ -320,7 +322,7 @@ public MemCachedClient(boolean binaryProtocal) {
/**
* Creates a new instance of MemCachedClient accepting a passed in pool
* name.
- *
+ *
* @param poolName
* name of SockIOPool
*/
@@ -331,7 +333,7 @@ public MemCachedClient(String poolName) {
/**
* Creates a new instance of MemCachedClient accepting a passed in pool
* name.
- *
+ *
* @param poolName
* name of SockIOPool
* @param binaryProtocal
@@ -343,7 +345,7 @@ public MemCachedClient(String poolName, boolean binaryProtocal) {
/**
* Create default memcached client.
- *
+ *
* @param isTCP
* true - use tcp protocol
* false - use udp protocol
@@ -356,7 +358,7 @@ public MemCachedClient(boolean isTCP, boolean binaryProtocal) {
/**
* Create memcached client.
- *
+ *
* @param poolName
* name of SockIOPool
* @param isTCP
@@ -364,16 +366,19 @@ public MemCachedClient(boolean isTCP, boolean binaryProtocal) {
* @param binaryProtocal
* use binary protocol.
*/
- public MemCachedClient(String poolName, boolean isTcp, boolean binaryProtocal) {
- if (binaryProtocal)
+ public MemCachedClient(String poolName, boolean isTcp,
+ boolean binaryProtocal) {
+ if (binaryProtocal) {
client = new BinaryClient(poolName);
- else
- client = isTcp ? new AscIIClient(poolName) : new AscIIUDPClient(poolName);
+ } else {
+ client = isTcp ? new AscIIClient(poolName) : new AscIIUDPClient(
+ poolName);
+ }
}
/**
* create memcached client.
- *
+ *
* @param poolName
* pool name
* @param isTcp
@@ -389,17 +394,21 @@ public MemCachedClient(String poolName, boolean isTcp, boolean binaryProtocal) {
* {@link com.schooner.MemCached.AbstractTransCoder} to achieve
* the same objective.
*/
- public MemCachedClient(String poolName, boolean isTcp, boolean binaryProtocol, ClassLoader cl, ErrorHandler eh) {
- if (binaryProtocol)
+ @Deprecated
+ public MemCachedClient(String poolName, boolean isTcp,
+ boolean binaryProtocol, ClassLoader cl, ErrorHandler eh) {
+ if (binaryProtocol) {
client = new BinaryClient(poolName, cl, eh);
- else
- client = isTcp ? new AscIIClient(poolName, cl, eh) : new AscIIUDPClient(poolName, cl, eh);
+ } else {
+ client = isTcp ? new AscIIClient(poolName, cl, eh)
+ : new AscIIUDPClient(poolName, cl, eh);
+ }
}
/**
* Creates a new instance of MemCacheClient but acceptes a passed in
* ClassLoader.
- *
+ *
* @param classLoader
* ClassLoader object.
* @deprecated will be removed in next release.
@@ -408,6 +417,7 @@ public MemCachedClient(String poolName, boolean isTcp, boolean binaryProtocol, C
* the same objective.
*/
+ @Deprecated
public MemCachedClient(ClassLoader classLoader) {
this();
client.setClassLoader(classLoader);
@@ -416,7 +426,7 @@ public MemCachedClient(ClassLoader classLoader) {
/**
* Creates a new instance of MemCacheClient but acceptes a passed in
* ClassLoader and a passed in ErrorHandler.
- *
+ *
* @param classLoader
* ClassLoader object.
* @param errorHandler
@@ -426,6 +436,7 @@ public MemCachedClient(ClassLoader classLoader) {
* {@link com.schooner.MemCached.AbstractTransCoder} to achieve
* the same objective.
*/
+ @Deprecated
public MemCachedClient(ClassLoader classLoader, ErrorHandler errorHandler) {
this(null, true, false, classLoader, errorHandler);
}
@@ -433,7 +444,7 @@ public MemCachedClient(ClassLoader classLoader, ErrorHandler errorHandler) {
/**
* Creates a new instance of MemCacheClient but acceptes a passed in
* ClassLoader, ErrorHandler, and SockIOPool name.
- *
+ *
* @param classLoader
* ClassLoader object.
* @param errorHandler
@@ -446,31 +457,35 @@ public MemCachedClient(ClassLoader classLoader, ErrorHandler errorHandler) {
* the same objective.
*/
- public MemCachedClient(ClassLoader classLoader, ErrorHandler errorHandler, String poolName) {
+ @Deprecated
+ public MemCachedClient(ClassLoader classLoader, ErrorHandler errorHandler,
+ String poolName) {
this(poolName, true, false, classLoader, errorHandler);
}
/**
* Sets an optional ClassLoader to be used for serialization.
- *
+ *
* @param classLoader
* @deprecated will be removed in next release.
* Please use customized transcoder
* {@link com.schooner.MemCached.AbstractTransCoder} to achieve
* the same objective.
*/
+ @Deprecated
public void setClassLoader(ClassLoader classLoader) {
throw new UnsupportedOperationException();
}
/**
* Sets an optional ErrorHandler.
- *
+ *
* @param errorHandler
* @deprecated will be removed in next release. The purpose of adding this
* support was to make it compatible with previous releases.
*/
+ @Deprecated
public void setErrorHandler(ErrorHandler errorHandler) {
throw new UnsupportedOperationException();
}
@@ -478,7 +493,7 @@ public void setErrorHandler(ErrorHandler errorHandler) {
/**
* Enable storing compressed data, provided it meets the threshold
* requirements.
- *
+ *
* If enabled, data will be stored in compressed form if it is
* longer than the threshold length set with setCompressThreshold(int)
*
@@ -486,29 +501,31 @@ public void setErrorHandler(ErrorHandler errorHandler) {
*
* Even if compression is disabled, compressed data will be automatically
* decompressed.
- *
+ *
* @param compressEnable
* true
to enable compression, false
to
* disable compression
* @deprecated will be removed in next release.
*/
+ @Deprecated
public void setCompressEnable(boolean compressEnable) {
throw new UnsupportedOperationException();
}
/**
* Sets the required length for data to be considered for compression.
- *
+ *
* If the length of the data to be stored is not equal or larger than this
* value, it will not be compressed.
- *
+ *
* This defaults to 15 KB.
- *
+ *
* @param compressThreshold
* required length of data to consider compression
* @deprecated will be removed in next release.
*/
+ @Deprecated
public void setCompressThreshold(long compressThreshold) {
throw new UnsupportedOperationException();
}
@@ -516,7 +533,7 @@ public void setCompressThreshold(long compressThreshold) {
/**
* Sets default String encoding when storing primitives as Strings. Default
* is UTF-8.
- *
+ *
* @param defaultEncoding
*/
public void setDefaultEncoding(String defaultEncoding) {
@@ -525,7 +542,7 @@ public void setDefaultEncoding(String defaultEncoding) {
/**
* Enables storing primitive types as their String values.
- *
+ *
* @param primitiveAsString
* if true, then store all primitives as their string value.
*/
@@ -535,7 +552,7 @@ public void setPrimitiveAsString(boolean primitiveAsString) {
/**
* Enables/disables sanitizing keys by URLEncoding.
- *
+ *
* @param sanitizeKeys
* if true, then URLEncode all keys
*/
@@ -545,29 +562,29 @@ public void setSanitizeKeys(boolean sanitizeKeys) {
/**
* Checks to see if key exists in cache.
- *
+ *
* @param key
* the key to look for
* @return true if key found in cache, false if not (or if cache is down)
*/
- public boolean keyExists(String key) {
+ public boolean keyExists(Serializable key) {
return client.keyExists(key);
}
/**
* Deletes an object from cache given cache key.
- *
+ *
* @param key
* the key to be removed
* @return true
, if the data was deleted successfully
*/
- public boolean delete(String key) {
+ public boolean delete(Serializable key) {
return client.delete(key);
}
/**
* Deletes an object from cache given cache key and expiration date.
- *
+ *
* @deprecated not supported in memcached 1.4+
* @param key
* the key to be removed
@@ -575,14 +592,15 @@ public boolean delete(String key) {
* when to expire the record.
* @return true
, if the data was deleted successfully
*/
- public boolean delete(String key, Date expiry) {
+ @Deprecated
+ public boolean delete(Serializable key, Date expiry) {
return client.delete(key, expiry);
}
/**
* Deletes an object from cache given cache key, a delete time, and an
* optional hashcode.
- *
+ *
* The item is immediately made non retrievable.
* Keep in mind {@link #add(String, Object) add} and
* {@link #replace(String, Object) replace}
@@ -590,7 +608,7 @@ public boolean delete(String key, Date expiry) {
* the
* specified time. However, {@link #set(String, Object) set} will succeed,
* and the new value will not be deleted.
- *
+ *
* @deprecated not supported in memcached 1.4+
* @param key
* the key to be removed
@@ -600,26 +618,27 @@ public boolean delete(String key, Date expiry) {
* when to expire the record.
* @return true
, if the data was deleted successfully
*/
- public boolean delete(String key, Integer hashCode, Date expiry) {
+ @Deprecated
+ public boolean delete(Serializable key, Integer hashCode, Date expiry) {
return client.delete(key, hashCode, expiry);
}
/**
* Stores data on the server; only the key and the value are specified.
- *
+ *
* @param key
* key to store data under
* @param value
* value to store
* @return true, if the data was successfully stored
*/
- public boolean set(String key, Object value) {
+ public boolean set(Serializable key, Serializable value) {
return client.set(key, value);
}
/**
* Stores data on the server; only the key and the value are specified.
- *
+ *
* @param key
* key to store data under
* @param value
@@ -628,14 +647,14 @@ public boolean set(String key, Object value) {
* if not null, then the int hashcode to use
* @return true, if the data was successfully stored
*/
- public boolean set(String key, Object value, Integer hashCode) {
+ public boolean set(Serializable key, Serializable value, Integer hashCode) {
return client.set(key, value, hashCode);
}
/**
* Stores data on the server; the key, value, and an expiration time are
* specified.
- *
+ *
* @param key
* key to store data under
* @param value
@@ -644,14 +663,14 @@ public boolean set(String key, Object value, Integer hashCode) {
* when to expire the record
* @return true, if the data was successfully stored
*/
- public boolean set(String key, Object value, Date expiry) {
+ public boolean set(Serializable key, Serializable value, Date expiry) {
return client.set(key, value, expiry);
}
/**
* Stores data on the server; the key, value, and an expiration time are
* specified.
- *
+ *
* @param key
* key to store data under
* @param value
@@ -662,14 +681,15 @@ public boolean set(String key, Object value, Date expiry) {
* if not null, then the int hashcode to use
* @return true, if the data was successfully stored
*/
- public boolean set(String key, Object value, Date expiry, Integer hashCode) {
+ public boolean set(Serializable key, Serializable value, Date expiry,
+ Integer hashCode) {
return client.set(key, value, expiry, hashCode);
}
-
+
/**
* Stores data on the server; the key, value, and an expiration time are
* specified.
- *
+ *
* @param key
* key to store data under
* @param value
@@ -682,27 +702,28 @@ public boolean set(String key, Object value, Date expiry, Integer hashCode) {
* if true, then store all primitives as their string value
* @return true, if the data was successfully stored
*/
- public boolean set(String key, Object value, Date expiry, Integer hashCode, boolean asString) {
+ public boolean set(Serializable key, Serializable value, Date expiry,
+ Integer hashCode, boolean asString) {
return client.set(key, value, expiry, hashCode, asString);
}
/**
* Adds data to the server; only the key and the value are specified.
- *
+ *
* @param key
* key to store data under
* @param value
* value to store
* @return true, if the data was successfully stored
*/
- public boolean add(String key, Object value) {
+ public boolean add(Serializable key, Serializable value) {
return client.add(key, value);
}
/**
* Adds data to the server; the key, value, and an optional hashcode are
* passed in.
- *
+ *
* @param key
* key to store data under
* @param value
@@ -711,14 +732,14 @@ public boolean add(String key, Object value) {
* if not null, then the int hashcode to use
* @return true, if the data was successfully stored
*/
- public boolean add(String key, Object value, Integer hashCode) {
+ public boolean add(Serializable key, Serializable value, Integer hashCode) {
return client.add(key, value, hashCode);
}
/**
* Adds data to the server; the key, value, and an expiration time are
* specified.
- *
+ *
* @param key
* key to store data under
* @param value
@@ -727,14 +748,14 @@ public boolean add(String key, Object value, Integer hashCode) {
* when to expire the record
* @return true, if the data was successfully stored
*/
- public boolean add(String key, Object value, Date expiry) {
+ public boolean add(Serializable key, Serializable value, Date expiry) {
return client.add(key, value, expiry);
}
/**
* Adds data to the server; the key, value, and an expiration time are
* specified.
- *
+ *
* @param key
* key to store data under
* @param value
@@ -745,27 +766,28 @@ public boolean add(String key, Object value, Date expiry) {
* if not null, then the int hashcode to use
* @return true, if the data was successfully stored
*/
- public boolean add(String key, Object value, Date expiry, Integer hashCode) {
+ public boolean add(Serializable key, Serializable value, Date expiry,
+ Integer hashCode) {
return client.add(key, value, expiry, hashCode);
}
/**
* Updates data on the server; only the key and the value are specified.
- *
+ *
* @param key
* key to store data under
* @param value
* value to store
* @return true, if the data was successfully stored
*/
- public boolean replace(String key, Object value) {
+ public boolean replace(Serializable key, Serializable value) {
return client.replace(key, value);
}
/**
* Updates data on the server; only the key and the value and an optional
* hash are specified.
- *
+ *
* @param key
* key to store data under
* @param value
@@ -774,14 +796,15 @@ public boolean replace(String key, Object value) {
* if not null, then the int hashcode to use
* @return true, if the data was successfully stored
*/
- public boolean replace(String key, Object value, Integer hashCode) {
+ public boolean replace(Serializable key, Serializable value,
+ Integer hashCode) {
return client.replace(key, value, hashCode);
}
/**
* Updates data on the server; the key, value, and an expiration time are
* specified.
- *
+ *
* @param key
* key to store data under
* @param value
@@ -790,14 +813,14 @@ public boolean replace(String key, Object value, Integer hashCode) {
* when to expire the record
* @return true, if the data was successfully stored
*/
- public boolean replace(String key, Object value, Date expiry) {
+ public boolean replace(Serializable key, Serializable value, Date expiry) {
return client.replace(key, value, expiry);
}
/**
* Updates data on the server; the key, value, and an expiration time are
* specified.
- *
+ *
* @param key
* key to store data under
* @param value
@@ -808,26 +831,27 @@ public boolean replace(String key, Object value, Date expiry) {
* if not null, then the int hashcode to use
* @return true, if the data was successfully stored
*/
- public boolean replace(String key, Object value, Date expiry, Integer hashCode) {
+ public boolean replace(Serializable key, Serializable value, Date expiry,
+ Integer hashCode) {
return client.replace(key, value, expiry, hashCode);
}
/**
* Store a counter to memcached given a key
- *
+ *
* @param key
* cache key
* @param counter
* number to store
* @return true/false indicating success
*/
- public boolean storeCounter(String key, Long counter) {
+ public boolean storeCounter(Serializable key, Long counter) {
return storeCounter(key, counter, null, null);
}
/**
* Store a counter to memcached given a key
- *
+ *
* @param key
* cache key
* @param counter
@@ -836,13 +860,13 @@ public boolean storeCounter(String key, Long counter) {
* when to expire the record
* @return true/false indicating success
*/
- public boolean storeCounter(String key, Long counter, Date date) {
+ public boolean storeCounter(Serializable key, Long counter, Date date) {
return storeCounter(key, counter, date, null);
}
/**
* Store a counter to memcached given a key
- *
+ *
* @param key
* cache key
* @param counter
@@ -853,13 +877,14 @@ public boolean storeCounter(String key, Long counter, Date date) {
* if not null, then the int hashcode to use
* @return true/false indicating success
*/
- public boolean storeCounter(String key, Long counter, Date date, Integer hashCode) {
+ public boolean storeCounter(Serializable key, Long counter, Date date,
+ Integer hashCode) {
return set(key, counter, date, hashCode, true);
}
/**
* Store a counter to memcached given a key
- *
+ *
* @param key
* cache key
* @param counter
@@ -868,35 +893,36 @@ public boolean storeCounter(String key, Long counter, Date date, Integer hashCod
* if not null, then the int hashcode to use
* @return true/false indicating success
*/
- public boolean storeCounter(String key, Long counter, Integer hashCode) {
+ public boolean storeCounter(Serializable key, Long counter, Integer hashCode) {
return storeCounter(key, counter, null, hashCode);
}
/**
* Returns value in counter at given key as long.
- *
+ *
* @param key
* cache ket
* @return counter value or -1 if not found
*/
- public long getCounter(String key) {
+ public long getCounter(Serializable key) {
return getCounter(key, null);
}
/**
* Returns value in counter at given key as long.
- *
+ *
* @param key
* cache ket
* @param hashCode
* if not null, then the int hashcode to use
* @return counter value or -1 if not found
*/
- public long getCounter(String key, Integer hashCode) {
+ public long getCounter(Serializable key, Integer hashCode) {
if (key == null) {
- if (log.isErrorEnabled())
+ if (log.isErrorEnabled()) {
log.error("null key for getCounter()");
+ }
return -1;
}
@@ -906,12 +932,15 @@ public long getCounter(String key, Integer hashCode) {
} catch (Exception ex) {
// if we have an errorHandler, use its hook
- if (errorHandler != null)
- errorHandler.handleErrorOnGet(this, ex, key);
+ if (errorHandler != null) {
+ errorHandler.handleErrorOnGet(this, ex, key.toString());
+ }
// not found or error getting out
- if (log.isDebugEnabled())
- log.info(String.format("Failed to parse Long value for key: %s", key));
+ if (log.isDebugEnabled()) {
+ log.info(String.format(
+ "Failed to parse Long value for key: %s", key));
+ }
}
return counter;
@@ -919,31 +948,31 @@ public long getCounter(String key, Integer hashCode) {
/**
* Thread safe way to initialize and increment a counter.
- *
+ *
* @param key
* key where the data is stored
* @return value of incrementer
*/
- public long addOrIncr(String key) {
+ public long addOrIncr(Serializable key) {
return client.addOrIncr(key);
}
/**
* Thread safe way to initialize and increment a counter.
- *
+ *
* @param key
* key where the data is stored
* @param inc
* value to set or increment by
* @return value of incrementer
*/
- public long addOrIncr(String key, long inc) {
+ public long addOrIncr(Serializable key, long inc) {
return client.addOrIncr(key, inc);
}
/**
* Thread safe way to initialize and increment a counter.
- *
+ *
* @param key
* key where the data is stored
* @param inc
@@ -952,37 +981,37 @@ public long addOrIncr(String key, long inc) {
* if not null, then the int hashcode to use
* @return value of incrementer
*/
- public long addOrIncr(String key, long inc, Integer hashCode) {
+ public long addOrIncr(Serializable key, long inc, Integer hashCode) {
return client.addOrIncr(key, inc, hashCode);
}
/**
* Thread safe way to initialize and decrement a counter.
- *
+ *
* @param key
* key where the data is stored
* @return value of incrementer
*/
- public long addOrDecr(String key) {
+ public long addOrDecr(Serializable key) {
return client.addOrDecr(key);
}
/**
* Thread safe way to initialize and decrement a counter.
- *
+ *
* @param key
* key where the data is stored
* @param inc
* value to set or increment by
* @return value of incrementer
*/
- public long addOrDecr(String key, long inc) {
+ public long addOrDecr(Serializable key, long inc) {
return client.addOrDecr(key, inc);
}
/**
* Thread safe way to initialize and decrement a counter.
- *
+ *
* @param key
* key where the data is stored
* @param inc
@@ -991,7 +1020,7 @@ public long addOrDecr(String key, long inc) {
* if not null, then the int hashcode to use
* @return value of incrementer
*/
- public long addOrDecr(String key, long inc, Integer hashCode) {
+ public long addOrDecr(Serializable key, long inc, Integer hashCode) {
return client.addOrDecr(key, inc, hashCode);
}
@@ -999,13 +1028,13 @@ public long addOrDecr(String key, long inc, Integer hashCode) {
* Increment the value at the specified key by 1, and then return it.
* Please make sure setPrimitiveAsString=true if the key/value pair is
* stored with set command.
- *
+ *
* @param key
* key where the data is stored
* @return -1, if the key is not found, the value after incrementing
* otherwise
*/
- public long incr(String key) {
+ public long incr(Serializable key) {
return client.incr(key);
}
@@ -1013,7 +1042,7 @@ public long incr(String key) {
* Increment the value at the specified key by passed in val.
* Please make sure setPrimitiveAsString=true if the key/value pair is
* stored with set command.
- *
+ *
* @param key
* key where the data is stored
* @param inc
@@ -1021,7 +1050,7 @@ public long incr(String key) {
* @return -1, if the key is not found, the value after incrementing
* otherwise
*/
- public long incr(String key, long inc) {
+ public long incr(Serializable key, long inc) {
return client.incr(key, inc);
}
@@ -1030,7 +1059,7 @@ public long incr(String key, long inc) {
* then return it.
* Please make sure setPrimitiveAsString=true if the key/value pair is
* stored with set command.
- *
+ *
* @param key
* key where the data is stored
* @param inc
@@ -1040,7 +1069,7 @@ public long incr(String key, long inc) {
* @return -1, if the key is not found, the value after incrementing
* otherwise
*/
- public long incr(String key, long inc, Integer hashCode) {
+ public long incr(Serializable key, long inc, Integer hashCode) {
return client.incr(key, inc, hashCode);
}
@@ -1048,13 +1077,13 @@ public long incr(String key, long inc, Integer hashCode) {
* Decrement the value at the specified key by 1, and then return it.
* Please make sure setPrimitiveAsString=true if the key/value pair is
* stored with set command.
- *
+ *
* @param key
* key where the data is stored
* @return -1, if the key is not found, the value after incrementing
* otherwise
*/
- public long decr(String key) {
+ public long decr(Serializable key) {
return client.decr(key);
}
@@ -1063,7 +1092,7 @@ public long decr(String key) {
* return it.
* Please make sure setPrimitiveAsString=true if the key/value pair is
* stored with set command.
- *
+ *
* @param key
* key where the data is stored
* @param inc
@@ -1071,7 +1100,7 @@ public long decr(String key) {
* @return -1, if the key is not found, the value after incrementing
* otherwise
*/
- public long decr(String key, long inc) {
+ public long decr(Serializable key, long inc) {
return client.decr(key, inc);
}
@@ -1080,7 +1109,7 @@ public long decr(String key, long inc) {
* then return it.
* Please make sure setPrimitiveAsString=true if the key/value pair is
* stored with set command.
- *
+ *
* @param key
* key where the data is stored
* @param inc
@@ -1090,13 +1119,13 @@ public long decr(String key, long inc) {
* @return -1, if the key is not found, the value after incrementing
* otherwise
*/
- public long decr(String key, long inc, Integer hashCode) {
+ public long decr(Serializable key, long inc, Integer hashCode) {
return client.decr(key, inc, hashCode);
}
/**
* Retrieve a key from the server, using a specific hash.
- *
+ *
* If the data was compressed or serialized when compressed, it will
* automatically
* be decompressed or serialized, as appropriate. (Inclusive or)
@@ -1104,19 +1133,19 @@ public long decr(String key, long inc, Integer hashCode) {
* Non-serialized data will be returned as a string, so explicit conversion
* to
* numeric types will be necessary, if desired
- *
+ *
* @param key
* key where data is stored
* @return the object that was previously stored, or null if it was not
* previously stored
*/
- public Object get(String key) {
+ public Object get(Serializable key) {
return client.get(key);
}
/**
* Retrieve a key from the server, using a specific hash.
- *
+ *
* If the data was compressed or serialized when compressed, it will
* automatically
* be decompressed or serialized, as appropriate. (Inclusive or)
@@ -1124,7 +1153,7 @@ public Object get(String key) {
* Non-serialized data will be returned as a string, so explicit conversion
* to
* numeric types will be necessary, if desired
- *
+ *
* @param key
* key where data is stored
* @param hashCode
@@ -1132,25 +1161,29 @@ public Object get(String key) {
* @return the object that was previously stored, or null if it was not
* previously stored
*/
- public Object get(String key, Integer hashCode) {
+ public Object get(Serializable key, Integer hashCode) {
return client.get(key, hashCode);
}
- public MemcachedItem gets(String key) {
+ public MemcachedItem gets(Serializable key) {
return client.gets(key);
}
- public MemcachedItem gets(String key, Integer hashCode) {
+ public MemcachedItem gets(Serializable key, Integer hashCode) {
return client.gets(key, hashCode);
}
+ public void setKeyTransCoder(TransBytecode transCoder) {
+ client.setKeyTransCoder(transCoder);
+ }
+
public void setTransCoder(TransCoder transCoder) {
client.setTransCoder(transCoder);
}
/**
* Retrieve a key from the server, using a specific hash.
- *
+ *
* If the data was compressed or serialized when compressed, it will
* automatically
* be decompressed or serialized, as appropriate. (Inclusive or)
@@ -1158,7 +1191,7 @@ public void setTransCoder(TransCoder transCoder) {
* Non-serialized data will be returned as a string, so explicit conversion
* to
* numeric types will be necessary, if desired
- *
+ *
* @param key
* key where data is stored
* @param hashCode
@@ -1168,33 +1201,33 @@ public void setTransCoder(TransCoder transCoder) {
* @return the object that was previously stored, or null if it was not
* previously stored
*/
- public Object get(String key, Integer hashCode, boolean asString) {
+ public Object get(Serializable key, Integer hashCode, boolean asString) {
return client.get(key, hashCode, asString);
}
/**
* Retrieve multiple objects from the memcache.
- *
+ *
* This is recommended over repeated calls to {@link #get(String) get()},
* since it
* is more efficient.
- *
+ *
* @param keys
* String array of keys to retrieve
* @return Object array ordered in same order as key array containing
* results
*/
- public Object[] getMultiArray(String[] keys) {
+ public Object[] getMultiArray(Serializable[] keys) {
return client.getMultiArray(keys);
}
/**
* Retrieve multiple objects from the memcache.
- *
+ *
* This is recommended over repeated calls to {@link #get(String) get()},
* since it
* is more efficient.
- *
+ *
* @param keys
* String array of keys to retrieve
* @param hashCodes
@@ -1202,17 +1235,17 @@ public Object[] getMultiArray(String[] keys) {
* @return Object array ordered in same order as key array containing
* results
*/
- public Object[] getMultiArray(String[] keys, Integer[] hashCodes) {
+ public Object[] getMultiArray(Serializable[] keys, Integer[] hashCodes) {
return client.getMultiArray(keys, hashCodes);
}
/**
* Retrieve multiple objects from the memcache.
- *
+ *
* This is recommended over repeated calls to {@link #get(String) get()},
* since it
* is more efficient.
- *
+ *
* @param keys
* String array of keys to retrieve
* @param hashCodes
@@ -1222,34 +1255,35 @@ public Object[] getMultiArray(String[] keys, Integer[] hashCodes) {
* @return Object array ordered in same order as key array containing
* results
*/
- public Object[] getMultiArray(String[] keys, Integer[] hashCodes, boolean asString) {
+ public Object[] getMultiArray(Serializable[] keys, Integer[] hashCodes,
+ boolean asString) {
return client.getMultiArray(keys, hashCodes, asString);
}
/**
* Retrieve multiple objects from the memcache.
- *
+ *
* This is recommended over repeated calls to {@link #get(String) get()},
* since it
* is more efficient.
- *
+ *
* @param keys
* String array of keys to retrieve
* @return a hashmap with entries for each key is found by the server, keys
* that are not found are not entered into the hashmap, but
* attempting to retrieve them from the hashmap gives you null.
*/
- public Map getMulti(String[] keys) {
+ public Map getMulti(Serializable[] keys) {
return getMulti(keys, null);
}
/**
* Retrieve multiple keys from the memcache.
- *
+ *
* This is recommended over repeated calls to {@link #get(String) get()},
* since it
* is more efficient.
- *
+ *
* @param keys
* keys to retrieve
* @param hashCodes
@@ -1258,17 +1292,18 @@ public Map getMulti(String[] keys) {
* that are not found are not entered into the hashmap, but
* attempting to retrieve them from the hashmap gives you null.
*/
- public Map getMulti(String[] keys, Integer[] hashCodes) {
+ public Map getMulti(Serializable[] keys,
+ Integer[] hashCodes) {
return client.getMulti(keys, hashCodes);
}
/**
* Retrieve multiple keys from the memcache.
- *
+ *
* This is recommended over repeated calls to {@link #get(String) get()},
* since it
* is more efficient.
- *
+ *
* @param keys
* keys to retrieve
* @param hashCodes
@@ -1279,15 +1314,16 @@ public Map getMulti(String[] keys, Integer[] hashCodes) {
* that are not found are not entered into the hashmap, but
* attempting to retrieve them from the hashmap gives you null.
*/
- public Map getMulti(String[] keys, Integer[] hashCodes, boolean asString) {
+ public Map getMulti(Serializable[] keys,
+ Integer[] hashCodes, boolean asString) {
return client.getMulti(keys, hashCodes, asString);
}
/**
* Invalidates the entire cache.
- *
+ *
* Will return true only if succeeds in clearing all servers.
- *
+ *
* @return success true/false
*/
public boolean flushAll() {
@@ -1296,10 +1332,10 @@ public boolean flushAll() {
/**
* Invalidates the entire cache.
- *
+ *
* Will return true only if succeeds in clearing all servers. If pass in
* null, then will try to flush all servers.
- *
+ *
* @param servers
* optional array of host(s) to flush (host:port)
* @return success true/false
@@ -1310,10 +1346,10 @@ public boolean flushAll(String[] servers) {
/**
* Retrieves stats for all servers.
- *
+ *
* Returns a map keyed on the servername. The value is another map which
* contains stats with stat name as key and value as value.
- *
+ *
* @return Stats map
*/
public Map> stats() {
@@ -1322,10 +1358,10 @@ public Map> stats() {
/**
* Retrieves stats for passed in servers (or all servers).
- *
+ *
* Returns a map keyed on the servername. The value is another map which
* contains stats with stat name as key and value as value.
- *
+ *
* @param servers
* string array of servers to retrieve stats from, or all if this
* is null
@@ -1337,10 +1373,10 @@ public Map> stats(String[] servers) {
/**
* Retrieves stats items for all servers.
- *
+ *
* Returns a map keyed on the servername. The value is another map which
* contains item stats with itemname:number:field as key and value as value.
- *
+ *
* @return Stats map
*/
public Map> statsItems() {
@@ -1349,10 +1385,10 @@ public Map> statsItems() {
/**
* Retrieves stats for passed in servers (or all servers).
- *
+ *
* Returns a map keyed on the servername. The value is another map which
* contains item stats with itemname:number:field as key and value as value.
- *
+ *
* @param servers
* string array of servers to retrieve stats from, or all if this
* is null
@@ -1364,10 +1400,10 @@ public Map> statsItems(String[] servers) {
/**
* Retrieves stats items for all servers.
- *
+ *
* Returns a map keyed on the servername. The value is another map which
* contains slabs stats with slabnumber:field as key and value as value.
- *
+ *
* @return Stats map
*/
public Map> statsSlabs() {
@@ -1376,10 +1412,10 @@ public Map> statsSlabs() {
/**
* Retrieves stats for passed in servers (or all servers).
- *
+ *
* Returns a map keyed on the servername. The value is another map which
* contains slabs stats with slabnumber:field as key and value as value.
- *
+ *
* @param servers
* string array of servers to retrieve stats from, or all if this
* is null
@@ -1391,26 +1427,27 @@ public Map> statsSlabs(String[] servers) {
/**
* Retrieves items cachedump for all servers.
- *
+ *
* Returns a map keyed on the servername. The value is another map which
* contains cachedump stats with the cachekey as key and byte size and unix
* timestamp as value.
- *
+ *
* @param slabNumber
* the item number of the cache dump
* @return Stats map
*/
- public Map> statsCacheDump(int slabNumber, int limit) {
+ public Map> statsCacheDump(int slabNumber,
+ int limit) {
return client.statsCacheDump(slabNumber, limit);
}
/**
* Retrieves stats for passed in servers (or all servers).
- *
+ *
* Returns a map keyed on the servername. The value is another map which
* contains cachedump stats with the cachekey as key and byte size and unix
* timestamp as value.
- *
+ *
* @param servers
* string array of servers to retrieve stats from, or all if this
* is null
@@ -1418,15 +1455,16 @@ public Map> statsCacheDump(int slabNumber, int limit
* the item number of the cache dump
* @return Stats map
*/
- public Map> statsCacheDump(String[] servers, int slabNumber, int limit) {
+ public Map> statsCacheDump(String[] servers,
+ int slabNumber, int limit) {
return client.statsCacheDump(servers, slabNumber, limit);
}
- public boolean sync(String key, Integer hashCode) {
+ public boolean sync(Serializable key, Integer hashCode) {
return client.sync(key, hashCode);
}
- public boolean sync(String key) {
+ public boolean sync(Serializable key) {
return client.sync(key);
}
@@ -1438,35 +1476,39 @@ public boolean syncAll(String[] servers) {
return client.syncAll(servers);
}
- public boolean append(String key, Object value, Integer hashCode) {
+ public boolean append(Serializable key, Serializable value, Integer hashCode) {
return client.append(key, value, hashCode);
}
- public boolean append(String key, Object value) {
+ public boolean append(Serializable key, Serializable value) {
return client.append(key, value);
}
- public boolean cas(String key, Object value, Integer hashCode, long casUnique) {
+ public boolean cas(Serializable key, Serializable value, Integer hashCode,
+ long casUnique) {
return client.cas(key, value, hashCode, casUnique);
}
- public boolean cas(String key, Object value, Date expiry, long casUnique) {
+ public boolean cas(Serializable key, Serializable value, Date expiry,
+ long casUnique) {
return client.cas(key, value, expiry, casUnique);
}
- public boolean cas(String key, Object value, Date expiry, Integer hashCode, long casUnique) {
+ public boolean cas(Serializable key, Serializable value, Date expiry,
+ Integer hashCode, long casUnique) {
return client.cas(key, value, expiry, hashCode, casUnique);
}
- public boolean cas(String key, Object value, long casUnique) {
+ public boolean cas(Serializable key, Serializable value, long casUnique) {
return client.cas(key, value, casUnique);
}
- public boolean prepend(String key, Object value, Integer hashCode) {
+ public boolean prepend(Serializable key, Serializable value,
+ Integer hashCode) {
return client.prepend(key, value, hashCode);
}
- public boolean prepend(String key, Object value) {
+ public boolean prepend(Serializable key, Serializable value) {
return client.prepend(key, value);
}
}
diff --git a/src/test/java/com/schooner/MemCached/MemCachedBenchBinaryKey.java b/src/test/java/com/schooner/MemCached/MemCachedBenchBinaryKey.java
new file mode 100644
index 0000000..74b9ab1
--- /dev/null
+++ b/src/test/java/com/schooner/MemCached/MemCachedBenchBinaryKey.java
@@ -0,0 +1,10 @@
+package com.schooner.MemCached;
+
+public class MemCachedBenchBinaryKey extends MemCachedBenchTcpKey {
+ protected String FILE_URL = "spring-memcached-Binary.xml";
+
+ @Override
+ public String getFileUrl() {
+ return FILE_URL;
+ }
+}
diff --git a/src/test/java/com/schooner/MemCached/MemCachedBenchTcpKey.java b/src/test/java/com/schooner/MemCached/MemCachedBenchTcpKey.java
new file mode 100644
index 0000000..a130c08
--- /dev/null
+++ b/src/test/java/com/schooner/MemCached/MemCachedBenchTcpKey.java
@@ -0,0 +1,262 @@
+package com.schooner.MemCached;
+
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.io.Serializable;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+import com.whalin.MemCached.MemCachedClient;
+
+public class MemCachedBenchTcpKey extends TestCase {
+ private static final String FILE_URL = "spring-memcached-TCP.xml";
+
+ private static final String BEAN_NAME = "memcachedClient";
+
+ private ClassPathXmlApplicationContext context;
+
+ public String getFileUrl() {
+ return FILE_URL;
+ }
+
+ @Override
+ public void setUp() {
+ String fileUrl = this.getFileUrl();
+ context = new ClassPathXmlApplicationContext(fileUrl);
+ MemCachedClient memcachedClient = getClient();
+ memcachedClient.flushAll();
+ }
+
+ public void testStringKey() {
+ System.out.println("-----test string key begin-----");
+ MemCachedClient memcachedClient = getClient();
+ memcachedClient.set("test", "test");
+ System.out.println(memcachedClient.get(new String("test")));
+ System.out.println("-----test string key end-----");
+ }
+
+ public void testStringBuilderKey() {
+ System.out.println("-----test stringbuilder key begin-----");
+ MemCachedClient memcachedClient = getClient();
+ StringBuilder builder = new StringBuilder("test");
+ memcachedClient.set(builder, "test");
+ builder = new StringBuilder("test");
+ System.out.println(memcachedClient.get(builder));
+ System.out.println("-----test stringbuilder key end-----");
+ }
+
+ public void testStringBufferKey() {
+ System.out.println("-----test stringbuffer key begin-----");
+ MemCachedClient memcachedClient = getClient();
+ StringBuffer buffer = new StringBuffer("test");
+ memcachedClient.set(buffer, "test");
+ buffer = new StringBuffer("test");
+ System.out.println(memcachedClient.get(buffer));
+ System.out.println("-----test stringbuffer key end-----");
+ }
+
+ public void testBooleanKey() {
+ System.out.println("-----test Boolean key begin-----");
+ MemCachedClient memcachedClient = getClient();
+ memcachedClient.set(true, "test");
+ Boolean b = true;
+ System.out.println(memcachedClient.get(b));
+ System.out.println("-----test Boolean key end-----");
+ }
+
+ public void testByteKey() {
+ System.out.println("-----test byte key begin-----");
+ MemCachedClient memcachedClient = getClient();
+ memcachedClient.set((byte) 1, "test");
+ Byte b = 1;
+ System.out.println(memcachedClient.get(b));
+ System.out.println("-----test byte key end-----");
+ }
+
+ public void testShortKey() {
+ System.out.println("-----test Short key begin-----");
+ MemCachedClient memcachedClient = getClient();
+ memcachedClient.set((short) 1, "test");
+ Short s = 1;
+ System.out.println(memcachedClient.get(s));
+ System.out.println("-----test Short key end-----");
+ }
+
+ public void testCharacterKey() {
+ System.out.println("-----test Character key begin-----");
+ MemCachedClient memcachedClient = getClient();
+ memcachedClient.set('c', "test");
+ Character c = 'c';
+ System.out.println(memcachedClient.get(c));
+ System.out.println("-----test Character key end-----");
+ }
+
+ public void testIntegerKey() {
+ System.out.println("-----test Integer key begin-----");
+ MemCachedClient memcachedClient = getClient();
+ memcachedClient.set(1, "test");
+ Integer i = 1;
+ System.out.println(memcachedClient.get(i));
+ System.out.println("-----test Integer key end-----");
+ }
+
+ public void testLongKey() {
+ System.out.println("-----test Long key begin-----");
+ MemCachedClient memcachedClient = getClient();
+ memcachedClient.set(1L, "test");
+ Long lg = 1L;
+ System.out.println(memcachedClient.get(lg));
+ System.out.println("-----test Long key end-----");
+ }
+
+ public void testFloatKey() {
+ System.out.println("-----test Float key begin-----");
+ MemCachedClient memcachedClient = getClient();
+ memcachedClient.set(1.0F, "test");
+ Float f = 1.0F;
+ System.out.println(memcachedClient.get(f));
+ System.out.println("-----test Float key end-----");
+ }
+
+ public void testDoubleKey() {
+ System.out.println("-----test Double key begin-----");
+ MemCachedClient memcachedClient = getClient();
+ memcachedClient.set(1.0, "test");
+ Double d = 1.0;
+ System.out.println(memcachedClient.get(d));
+ System.out.println("-----test Double key end-----");
+ }
+
+ public void testDateKey() {
+ System.out.println("-----test array key begin-----");
+ MemCachedClient memcachedClient = getClient();
+ Date date = Calendar.getInstance().getTime();
+ memcachedClient.set(date, "test");
+ System.out.println(memcachedClient.get(date));
+ System.out.println("-----test array key end-----");
+ }
+
+ public void testArrayKey() {
+ System.out.println("-----test array key begin-----");
+ MemCachedClient memcachedClient = getClient();
+ User[] array = new User[10];
+ memcachedClient.set(array, "test");
+ array = new User[10];
+ System.out.println(memcachedClient.get(array));
+ System.out.println("-----test array key end-----");
+ }
+
+ public void testSerializableKey() {
+ System.out.println("-----test Serializable key begin-----");
+ MemCachedClient memcachedClient = getClient();
+ SUser user = new SUser("test");
+ memcachedClient.set(user, "test");
+ user = new SUser("test");
+ System.out.println(memcachedClient.get(user));
+ System.out.println("-----test Serializabl key end-----");
+ }
+
+ public void testExternalizableKey() {
+ System.out.println("-----test Externalizable key begin-----");
+ MemCachedClient memcachedClient = getClient();
+ EUser user = new EUser("test");
+ memcachedClient.set(user, "test");
+ user = new EUser("test");
+ System.out.println(memcachedClient.get(user));
+ System.out.println("-----test Externalizable key end-----");
+ }
+
+ private MemCachedClient getClient() {
+ MemCachedClient memcachedClient = (MemCachedClient) context
+ .getBean(BEAN_NAME);
+ return memcachedClient;
+ }
+
+ @Override
+ public void tearDown() {
+ MemCachedClient memcachedClient = getClient();
+ showKeys(memcachedClient);
+ memcachedClient.flushAll();
+ if (context != null) {
+ context.close();
+ }
+ }
+
+ private static void showKeys(MemCachedClient memcachedClient) {
+ System.out.println("----show keys begin-----");
+ Map> map = memcachedClient.statsItems();
+ Set set = new HashSet();
+ for (Map m : map.values()) {
+ for (String s : m.keySet()) {
+ int first = s.indexOf(":");
+ int second = s.indexOf(":", first + 1);
+ set.add(s.substring(first + 1, second));
+ }
+ }
+ Set keySet = new HashSet();
+ for (String s : set) {
+ int slab = Integer.valueOf(s);
+ Map> dumpMap = memcachedClient
+ .statsCacheDump(slab, 0);
+ for (Map m : dumpMap.values()) {
+ keySet.addAll(m.keySet());
+ }
+ }
+ System.out.println(keySet);
+ System.out.println("-----show keys end-----");
+ System.out.println();
+ }
+
+ private static class User {
+ }
+
+ private static class SUser implements Serializable {
+ private static final long serialVersionUID = -1921923046163600659L;
+
+ private String name;
+
+ SUser(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public String toString() {
+ return "hashcode:" + this.hashCode() + " name:" + name;
+ }
+ }
+
+ private static class EUser implements Externalizable {
+ private static final long serialVersionUID = 8674506162535178907L;
+
+ private String name;
+
+ EUser(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public void writeExternal(ObjectOutput out) throws IOException {
+ out.writeObject(name);
+ }
+
+ @Override
+ public void readExternal(ObjectInput in) throws IOException,
+ ClassNotFoundException {
+ name = (String) in.readObject();
+ }
+
+ @Override
+ public String toString() {
+ return "hashcode:" + this.hashCode() + " name:" + name;
+ }
+ }
+}
diff --git a/src/test/java/com/schooner/MemCached/MemCachedBenchUdpKey.java b/src/test/java/com/schooner/MemCached/MemCachedBenchUdpKey.java
new file mode 100644
index 0000000..fa65bc8
--- /dev/null
+++ b/src/test/java/com/schooner/MemCached/MemCachedBenchUdpKey.java
@@ -0,0 +1,10 @@
+package com.schooner.MemCached;
+
+public class MemCachedBenchUdpKey extends MemCachedBenchTcpKey {
+ protected String FILE_URL = "spring-memcached-UDP.xml";
+
+ @Override
+ public String getFileUrl() {
+ return FILE_URL;
+ }
+}
diff --git a/src/test/java/com/schooner/MemCached/MemCachedClientAsciiTest.java b/src/test/java/com/schooner/MemCached/MemCachedClientAsciiTest.java
index ba40a24..6846ac2 100755
--- a/src/test/java/com/schooner/MemCached/MemCachedClientAsciiTest.java
+++ b/src/test/java/com/schooner/MemCached/MemCachedClientAsciiTest.java
@@ -1,9 +1,9 @@
/*******************************************************************************
* Copyright (c) 2009 Schooner Information Technology, Inc.
* All rights reserved.
- *
+ *
* http://www.schoonerinfotech.com/
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
@@ -14,7 +14,7 @@
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
- *
+ *
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
@@ -68,13 +68,15 @@ private static synchronized String initString(int len) {
private MemCacheDaemon daemon = null;
+ @Override
protected void setUp() throws Exception {
String servers = System.getProperty("memcached.host");
if (servers == null) {
// create daemon and start it
daemon = new MemCacheDaemon();
- CacheStorage storage = ConcurrentLinkedHashMap.create(
- ConcurrentLinkedHashMap.EvictionPolicy.FIFO, 100000, 5 * 1024 * 1024);
+ CacheStorage storage = ConcurrentLinkedHashMap
+ .create(ConcurrentLinkedHashMap.EvictionPolicy.FIFO,
+ 100000, 5 * 1024 * 1024);
daemon.setCache(new CacheImpl(storage));
daemon.setBinary(false);
daemon.setAddr(new InetSocketAddress(11211));
@@ -91,6 +93,7 @@ protected void setUp() throws Exception {
mc = new MemCachedClient("test");
}
+ @Override
protected void tearDown() throws Exception {
super.tearDown();
mc.flushAll();
@@ -269,8 +272,8 @@ public void testIncr() {
long j = mc.addOrIncr("foo", i); // now == 0
assertEquals(mc.get("foo"), new Long(i).toString());
j = mc.incr("foo"); // foo now == 1
- j = mc.incr("foo", (long) 5); // foo now == 6
- j = mc.decr("foo", (long) 2); // foo now == 4
+ j = mc.incr("foo", 5); // foo now == 6
+ j = mc.decr("foo", 2); // foo now == 4
assertEquals(4, j);
j = mc.incr("foo1");
assertEquals(-1, j);
@@ -318,11 +321,11 @@ public void testAddOrIncr() {
j = mc.addOrIncr("foo"); // foo now == 0
assertEquals(0, j);
j = mc.incr("foo"); // foo now == 1
- j = mc.incr("foo", (long) 5); // foo now == 6
+ j = mc.incr("foo", 5); // foo now == 6
j = mc.addOrIncr("foo", 1); // foo now 7
- j = mc.decr("foo", (long) 3); // foo now == 4
+ j = mc.decr("foo", 3); // foo now == 4
assertEquals(4, j);
}
@@ -383,7 +386,7 @@ public void testGetMulti() {
mc.set(keys[i], "value" + i);
}
- Map results = mc.getMulti(keys);
+ Map results = mc.getMulti(keys);
for (int i = 0; i < max; i++) {
assertEquals(results.get(keys[i]), "value" + i);
}
@@ -430,8 +433,9 @@ public void testGets() {
public void testSetByteArray() {
byte[] b = new byte[10];
- for (int i = 0; i < 10; i++)
+ for (int i = 0; i < 10; i++) {
b[i] = (byte) i;
+ }
mc.set("foo", b);
assertTrue(Arrays.equals((byte[]) mc.get("foo"), b));
@@ -447,13 +451,15 @@ public void testSetObj() {
public void testCusTransCoder() {
TransCoder coder = new ObjectTransCoder() {
@Override
- public void encode(OutputStream out, Object object) throws IOException {
+ public void encode(OutputStream out, Object object)
+ throws IOException {
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
ObjectOutputStream oOut = new ObjectOutputStream(bOut);
oOut.writeObject(object);
byte[] bytes = bOut.toByteArray();
- for (byte b : bytes)
+ for (byte b : bytes) {
out.write(b);
+ }
}
};
mc.setTransCoder(coder);
@@ -493,14 +499,15 @@ public void testSetStringObjectInteger() {
public void testMultiKey() {
- String[] allKeys = { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
+ String[] allKeys = { "key1", "key2", "key3", "key4", "key5", "key6",
+ "key7" };
String[] setKeys = { "key1", "key3", "key5", "key7" };
for (String key : setKeys) {
mc.set(key, key);
}
- Map results = mc.getMulti(allKeys);
+ Map results = mc.getMulti(allKeys);
assert allKeys.length == results.size();
for (String key : setKeys) {
@@ -654,60 +661,61 @@ public void testPrependStringObjectInteger() {
* assertEquals("bb", item.getValue()); mc.set("aa", "cc");
* assertEquals("cc", mc.get("aa")); mc.cas("aa", "dd",
* item.getCasUnique()); assertEquals("cc", mc.get("aa")); }
- *
+ *
* public void testCasStringObjectIntegerLong() { String expected, actual;
* mc.set("foo", "bar", 10); MemcachedItem item = mc.gets("foo", 10);
* expected = "bar"; actual = (String) item.getValue();
* assertEquals(expected, actual);
- *
+ *
* mc.cas("foo", "bar1", 10, item.getCasUnique()); expected = "bar1"; actual
* = (String) mc.get("foo", 10); assertEquals(expected, actual);
- *
+ *
* mc.set("foo", "bar2", 10); expected = "bar2"; actual = (String)
* mc.get("foo", 10); assertEquals(expected, actual);
- *
+ *
* boolean res = mc.cas("foo", "bar3", 10, item.getCasUnique());
* assertFalse(res); }
- *
+ *
* public void testCasStringObjectDateLong() { String expected, actual;
* mc.set("foo", "bar"); MemcachedItem item = mc.gets("foo"); expected =
* "bar"; actual = (String) item.getValue(); assertEquals(expected, actual);
- *
+ *
* Date expiry = new Date(1000); mc.cas("foo", "bar1", expiry,
* item.getCasUnique()); expected = "bar1"; actual = (String) mc.get("foo");
* assertEquals(expected, actual);
- *
+ *
* try { Thread.sleep(2000); } catch (InterruptedException e) {
* e.printStackTrace(); } assertNull(mc.get("foo"));
- *
+ *
* mc.set("foo", "bar2"); expected = "bar2"; actual = (String)
* mc.get("foo"); assertEquals(expected, actual);
- *
+ *
* boolean res = mc.cas("foo", "bar3", expiry, item.getCasUnique());
* assertFalse(res); }
- *
+ *
* public void testCasStringObjectDateIntegerLong() { String expected,
* actual; mc.set("foo", "bar", 10); MemcachedItem item = mc.gets("foo",
* 10); expected = "bar"; actual = (String) item.getValue();
* assertEquals(expected, actual);
- *
+ *
* Date expiry = new Date(1000); mc.cas("foo", "bar1", expiry, 10,
* item.getCasUnique()); expected = "bar1"; actual = (String) mc.get("foo",
* 10); assertEquals(expected, actual);
- *
+ *
* try { Thread.sleep(2000); } catch (InterruptedException e) {
* e.printStackTrace(); } actual = (String) mc.get("foo", 10);
* assertNull(actual);
- *
+ *
* mc.set("foo", "bar2", 10); expected = "bar2"; actual = (String)
* mc.get("foo", 10); assertEquals(expected, actual);
- *
+ *
* boolean res = mc.cas("foo", "bar3", expiry, 10, item.getCasUnique());
* assertFalse(res); }
*/
public void testBigData() {
- TestClass cls = new TestClass(initString(1024), initString(10240), 10240);
+ TestClass cls = new TestClass(initString(1024), initString(10240),
+ 10240);
for (int i = 0; i < 10; ++i) {
mc.set("foo" + i, cls);
assertEquals(cls, mc.get("foo" + i));
@@ -721,7 +729,8 @@ public void testBigData() {
}
public void testExtremeBigData() {
- TestClass cls = new TestClass(initString(1024), initString(10240), 10240);
+ TestClass cls = new TestClass(initString(1024), initString(10240),
+ 10240);
for (int i = 0; i < 10; ++i) {
mc.set("foo" + i, cls);
assertEquals(cls, mc.get("foo" + i));
@@ -823,7 +832,7 @@ public void testStoreCounterWithAsString() {
mc.storeCounter(key, 3L);
mc.incr(key);
- assertTrue((Long) mc.getCounter(key) == 4);
+ assertTrue(mc.getCounter(key) == 4);
}
public void testStoreCounterWithoutAsString() {
@@ -882,17 +891,23 @@ public Integer getField3() {
return this.field3;
}
+ @Override
public boolean equals(Object o) {
- if (this == o)
+ if (this == o) {
return true;
- if (!(o instanceof TestClass))
+ }
+ if (!(o instanceof TestClass)) {
return false;
+ }
TestClass obj = (TestClass) o;
- return ((this.field1 == obj.getField1() || (this.field1 != null && this.field1.equals(obj.getField1())))
- && (this.field2 == obj.getField2() || (this.field2 != null && this.field2.equals(obj.getField2()))) && (this.field3 == obj
- .getField3() || (this.field3 != null && this.field3.equals(obj.getField3()))));
+ return ((this.field1 == obj.getField1() || (this.field1 != null && this.field1
+ .equals(obj.getField1())))
+ && (this.field2 == obj.getField2() || (this.field2 != null && this.field2
+ .equals(obj.getField2()))) && (this.field3 == obj
+ .getField3() || (this.field3 != null && this.field3
+ .equals(obj.getField3()))));
}
}
diff --git a/src/test/java/com/schooner/MemCached/MemCachedClientBinaryTest.java b/src/test/java/com/schooner/MemCached/MemCachedClientBinaryTest.java
index 533ac4a..fde7e57 100755
--- a/src/test/java/com/schooner/MemCached/MemCachedClientBinaryTest.java
+++ b/src/test/java/com/schooner/MemCached/MemCachedClientBinaryTest.java
@@ -1,9 +1,9 @@
/*******************************************************************************
* Copyright (c) 2009 Schooner Information Technology, Inc.
* All rights reserved.
- *
+ *
* http://www.schoonerinfotech.com/
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
@@ -14,7 +14,7 @@
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
- *
+ *
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
@@ -70,13 +70,15 @@ private static synchronized String initString(int len) {
private MemCacheDaemon daemon = null;
+ @Override
protected void setUp() throws Exception {
String servers = System.getProperty("memcached.host");
if (servers == null) {
// create daemon and start it
daemon = new MemCacheDaemon();
- CacheStorage storage = ConcurrentLinkedHashMap.create(
- ConcurrentLinkedHashMap.EvictionPolicy.FIFO, 100000, 5 * 1024 * 1024);
+ CacheStorage storage = ConcurrentLinkedHashMap
+ .create(ConcurrentLinkedHashMap.EvictionPolicy.FIFO,
+ 100000, 5 * 1024 * 1024);
daemon.setCache(new CacheImpl(storage));
daemon.setBinary(false);
daemon.setAddr(new InetSocketAddress(11211));
@@ -93,6 +95,7 @@ protected void setUp() throws Exception {
mc = new MemCachedClient("test", true);
}
+ @Override
protected void tearDown() throws Exception {
super.tearDown();
mc.flushAll();
@@ -350,8 +353,8 @@ public void testIncr() {
mc.addOrIncr("foo", i); // now == 0
assertEquals(mc.get("foo"), new Long(i).toString());
mc.incr("foo"); // foo now == 1
- mc.incr("foo", (long) 5); // foo now == 6
- long j = mc.decr("foo", (long) 2); // foo now == 4
+ mc.incr("foo", 5); // foo now == 6
+ long j = mc.decr("foo", 2); // foo now == 4
assertEquals(j, 4);
}
@@ -395,11 +398,11 @@ public void testAddOrIncr() {
j = mc.addOrIncr("foo"); // foo now == 0
assertEquals(0, j);
j = mc.incr("foo"); // foo now == 1
- j = mc.incr("foo", (long) 5); // foo now == 6
+ j = mc.incr("foo", 5); // foo now == 6
j = mc.addOrIncr("foo", 1); // foo now 7
- j = mc.decr("foo", (long) 3); // foo now == 4
+ j = mc.decr("foo", 3); // foo now == 4
assertEquals(4, j);
}
@@ -460,7 +463,7 @@ public void testGetMulti() {
mc.set(keys[i], "value" + i);
}
- Map results = mc.getMulti(keys);
+ Map results = mc.getMulti(keys);
for (int i = 0; i < max; i++) {
assertEquals(results.get(keys[i]), "value" + i);
}
@@ -476,7 +479,7 @@ public void testGetMultiString() {
mc.set(keys[i], "value" + i);
}
- Map results = mc.getMulti(keys, null, true);
+ Map results = mc.getMulti(keys, null, true);
for (int i = 0; i < max; i++) {
assertEquals(results.get(keys[i]), "value" + i);
}
@@ -535,8 +538,9 @@ public void testGets() {
public void testSetByteArray() {
byte[] b = new byte[10];
- for (int i = 0; i < 10; i++)
+ for (int i = 0; i < 10; i++) {
b[i] = (byte) i;
+ }
mc.set("foo", b);
assertTrue(Arrays.equals((byte[]) mc.get("foo"), b));
@@ -552,13 +556,15 @@ public void testSetObj() {
public void testCusTransCoder() {
TransCoder coder = new ObjectTransCoder() {
@Override
- public void encode(OutputStream out, Object object) throws IOException {
+ public void encode(OutputStream out, Object object)
+ throws IOException {
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
ObjectOutputStream oOut = new ObjectOutputStream(bOut);
oOut.writeObject(object);
byte[] bytes = bOut.toByteArray();
- for (byte b : bytes)
+ for (byte b : bytes) {
out.write(b);
+ }
}
};
mc.setTransCoder(coder);
@@ -598,14 +604,15 @@ public void testSetStringObjectInteger() {
public void testMultiKey() {
- String[] allKeys = { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
+ String[] allKeys = { "key1", "key2", "key3", "key4", "key5", "key6",
+ "key7" };
String[] setKeys = { "key1", "key3", "key5", "key7" };
for (String key : setKeys) {
mc.set(key, key);
}
- Map results = mc.getMulti(allKeys);
+ Map results = mc.getMulti(allKeys);
assert allKeys.length == results.size();
for (String key : setKeys) {
@@ -852,7 +859,8 @@ public void testCasStringObjectDateIntegerLong() {
}
public void testBigData() {
- TestClass cls = new TestClass(initString(1024), initString(10240), 10240);
+ TestClass cls = new TestClass(initString(1024), initString(10240),
+ 10240);
for (int i = 0; i < 10; ++i) {
mc.set("foo" + i, cls);
assertEquals(cls, mc.get("foo" + i));
@@ -866,7 +874,8 @@ public void testBigData() {
}
public void testExtremeBigData() {
- TestClass cls = new TestClass(initString(1024), initString(10240), 10240);
+ TestClass cls = new TestClass(initString(1024), initString(10240),
+ 10240);
for (int i = 0; i < 10; ++i) {
mc.set("foo" + i, cls);
assertEquals(cls, mc.get("foo" + i));
@@ -907,7 +916,8 @@ public void testStatsSlabsStringArray() {
}
public void testStatsCacheDumpStringArrayIntegerInteger() {
- Map> res = mc.statsCacheDump(serverlist, 1, 2);
+ Map> res = mc.statsCacheDump(serverlist, 1,
+ 2);
assertFalse(res.isEmpty());
}
@@ -923,10 +933,13 @@ public void testGetWithIOException() {
assertEquals(expect, actual);
mc.setTransCoder(new TransCoder() {
- public int encode(SockOutputStream out, Object object) throws IOException {
+ @Override
+ public int encode(SockOutputStream out, Object object)
+ throws IOException {
throw new IOException();
}
+ @Override
public Object decode(InputStream input) throws IOException {
throw new IOException();
}
@@ -944,10 +957,13 @@ public void testGetsWithIOException() {
assertEquals(expect, actual);
mc.setTransCoder(new TransCoder() {
- public int encode(SockOutputStream out, Object object) throws IOException {
+ @Override
+ public int encode(SockOutputStream out, Object object)
+ throws IOException {
throw new IOException();
}
+ @Override
public Object decode(InputStream input) throws IOException {
throw new IOException();
}
@@ -959,10 +975,13 @@ public Object decode(InputStream input) throws IOException {
public void testSetWithIOException() {
mc.setTransCoder(new TransCoder() {
- public int encode(SockOutputStream out, Object object) throws IOException {
+ @Override
+ public int encode(SockOutputStream out, Object object)
+ throws IOException {
throw new IOException();
}
+ @Override
public Object decode(InputStream input) throws IOException {
throw new IOException();
}
@@ -979,7 +998,7 @@ public void testStoreCounterWithAsString() {
mc.storeCounter(key, 3L);
mc.incr(key);
- assertTrue((Long) mc.getCounter(key) == 4);
+ assertTrue(mc.getCounter(key) == 4);
}
public void testStoreCounterWithoutAsString() {
@@ -1012,25 +1031,36 @@ public static class TestErrorHandler implements ErrorHandler {
public boolean tag = false;
- public void handleErrorOnDelete(MemCachedClient client, Throwable error, String cacheKey) {
+ @Override
+ public void handleErrorOnDelete(MemCachedClient client,
+ Throwable error, String cacheKey) {
}
+ @Override
public void handleErrorOnFlush(MemCachedClient client, Throwable error) {
}
- public void handleErrorOnGet(MemCachedClient client, Throwable error, String cacheKey) {
+ @Override
+ public void handleErrorOnGet(MemCachedClient client, Throwable error,
+ String cacheKey) {
tag = true;
}
- public void handleErrorOnGet(MemCachedClient client, Throwable error, String[] cacheKeys) {
+ @Override
+ public void handleErrorOnGet(MemCachedClient client, Throwable error,
+ Serializable[] cacheKeys) {
}
+ @Override
public void handleErrorOnInit(MemCachedClient client, Throwable error) {
}
- public void handleErrorOnSet(MemCachedClient client, Throwable error, String cacheKey) {
+ @Override
+ public void handleErrorOnSet(MemCachedClient client, Throwable error,
+ String cacheKey) {
}
+ @Override
public void handleErrorOnStats(MemCachedClient client, Throwable error) {
}
@@ -1061,17 +1091,23 @@ public Integer getField3() {
return this.field3;
}
+ @Override
public boolean equals(Object o) {
- if (this == o)
+ if (this == o) {
return true;
- if (!(o instanceof TestClass))
+ }
+ if (!(o instanceof TestClass)) {
return false;
+ }
TestClass obj = (TestClass) o;
- return ((this.field1 == obj.getField1() || (this.field1 != null && this.field1.equals(obj.getField1())))
- && (this.field2 == obj.getField2() || (this.field2 != null && this.field2.equals(obj.getField2()))) && (this.field3 == obj
- .getField3() || (this.field3 != null && this.field3.equals(obj.getField3()))));
+ return ((this.field1 == obj.getField1() || (this.field1 != null && this.field1
+ .equals(obj.getField1())))
+ && (this.field2 == obj.getField2() || (this.field2 != null && this.field2
+ .equals(obj.getField2()))) && (this.field3 == obj
+ .getField3() || (this.field3 != null && this.field3
+ .equals(obj.getField3()))));
}
}
diff --git a/src/test/java/com/schooner/MemCached/MemcachedClientUDPTest.java b/src/test/java/com/schooner/MemCached/MemcachedClientUDPTest.java
index 6095007..2f22da3 100644
--- a/src/test/java/com/schooner/MemCached/MemcachedClientUDPTest.java
+++ b/src/test/java/com/schooner/MemCached/MemcachedClientUDPTest.java
@@ -1,9 +1,9 @@
/*******************************************************************************
* Copyright (c) 2009 Schooner Information Technology, Inc.
* All rights reserved.
- *
+ *
* http://www.schoonerinfotech.com/
- *
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
@@ -14,7 +14,7 @@
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
- *
+ *
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
@@ -72,11 +72,13 @@ private static synchronized String initString(int len) {
pool.setInitConn(1);
}
+ @Override
protected void setUp() throws Exception {
super.setUp();
mc = new MemCachedClient("test", false, false);
}
+ @Override
protected void tearDown() throws Exception {
super.tearDown();
assertNotNull(mc);
@@ -240,8 +242,8 @@ public void testIncr() {
mc.addOrIncr("foo", i); // now == 0
assertEquals(mc.get("foo"), new Long(i).toString());
mc.incr("foo"); // foo now == 1
- mc.incr("foo", (long) 5); // foo now == 6
- long j = mc.decr("foo", (long) 2); // foo now == 4
+ mc.incr("foo", 5); // foo now == 6
+ long j = mc.decr("foo", 2); // foo now == 4
assertEquals(j, 4);
}
@@ -285,11 +287,11 @@ public void testAddOrIncr() {
j = mc.addOrIncr("foo"); // foo now == 0
assertEquals(0, j);
j = mc.incr("foo"); // foo now == 1
- j = mc.incr("foo", (long) 5); // foo now == 6
+ j = mc.incr("foo", 5); // foo now == 6
j = mc.addOrIncr("foo", 1); // foo now 7
- j = mc.decr("foo", (long) 3); // foo now == 4
+ j = mc.decr("foo", 3); // foo now == 4
assertEquals(4, j);
}
@@ -355,7 +357,7 @@ public void testGetMulti() {
keys[0] = null;
- Map results = mc.getMulti(keys);
+ Map results = mc.getMulti(keys);
for (int i = 1; i < max; i++) {
assertEquals(results.get(keys[i]), "value" + i);
}
@@ -390,8 +392,9 @@ public void testGetMutiArrayStringArrayIntegerArray() {
public void testSetByteArray() {
byte[] b = new byte[10];
- for (int i = 0; i < 10; i++)
+ for (int i = 0; i < 10; i++) {
b[i] = (byte) i;
+ }
mc.set("foo", b);
assertTrue(Arrays.equals((byte[]) mc.get("foo"), b));
@@ -437,14 +440,15 @@ public void testSetStringObjectInteger() {
public void testMultiKey() {
- String[] allKeys = { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
+ String[] allKeys = { "key1", "key2", "key3", "key4", "key5", "key6",
+ "key7" };
String[] setKeys = { "key1", "key3", "key5", "key7" };
for (String key : setKeys) {
mc.set(key, key);
}
- Map results = mc.getMulti(allKeys);
+ Map results = mc.getMulti(allKeys);
assert allKeys.length == results.size();
for (String key : setKeys) {
@@ -738,7 +742,8 @@ public void testStatsSlabsStringArray() {
}
public void testStatsCacheDumpStringArrayIntegerInteger() {
- Map> res = mc.statsCacheDump(serverlist, 1, 2);
+ Map> res = mc.statsCacheDump(serverlist, 1,
+ 2);
assertFalse(res.isEmpty());
}
@@ -754,10 +759,13 @@ public void testGetWithIOException() {
assertEquals(expect, actual);
mc.setTransCoder(new TransCoder() {
- public int encode(SockOutputStream out, Object object) throws IOException {
+ @Override
+ public int encode(SockOutputStream out, Object object)
+ throws IOException {
throw new IOException();
}
+ @Override
public Object decode(InputStream input) throws IOException {
throw new IOException();
}
@@ -775,10 +783,13 @@ public void testGetsWithIOException() {
assertEquals(expect, actual);
mc.setTransCoder(new TransCoder() {
- public int encode(SockOutputStream out, Object object) throws IOException {
+ @Override
+ public int encode(SockOutputStream out, Object object)
+ throws IOException {
throw new IOException();
}
+ @Override
public Object decode(InputStream input) throws IOException {
throw new IOException();
}
@@ -790,10 +801,13 @@ public Object decode(InputStream input) throws IOException {
public void testSetWithIOException() {
mc.setTransCoder(new TransCoder() {
- public int encode(SockOutputStream out, Object object) throws IOException {
+ @Override
+ public int encode(SockOutputStream out, Object object)
+ throws IOException {
throw new IOException();
}
+ @Override
public Object decode(InputStream input) throws IOException {
throw new IOException();
}
@@ -827,13 +841,15 @@ public Object decode(InputStream input) throws IOException {
public void testCusTransCoder() {
TransCoder coder = new ObjectTransCoder() {
@Override
- public void encode(OutputStream out, Object object) throws IOException {
+ public void encode(OutputStream out, Object object)
+ throws IOException {
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
ObjectOutputStream oOut = new ObjectOutputStream(bOut);
oOut.writeObject(object);
byte[] bytes = bOut.toByteArray();
- for (byte b : bytes)
+ for (byte b : bytes) {
out.write(b);
+ }
}
};
mc.setTransCoder(coder);
@@ -872,17 +888,23 @@ public Integer getField3() {
return this.field3;
}
+ @Override
public boolean equals(Object o) {
- if (this == o)
+ if (this == o) {
return true;
- if (!(o instanceof TestClass))
+ }
+ if (!(o instanceof TestClass)) {
return false;
+ }
TestClass obj = (TestClass) o;
- return ((this.field1 == obj.getField1() || (this.field1 != null && this.field1.equals(obj.getField1())))
- && (this.field2 == obj.getField2() || (this.field2 != null && this.field2.equals(obj.getField2()))) && (this.field3 == obj
- .getField3() || (this.field3 != null && this.field3.equals(obj.getField3()))));
+ return ((this.field1 == obj.getField1() || (this.field1 != null && this.field1
+ .equals(obj.getField1())))
+ && (this.field2 == obj.getField2() || (this.field2 != null && this.field2
+ .equals(obj.getField2()))) && (this.field3 == obj
+ .getField3() || (this.field3 != null && this.field3
+ .equals(obj.getField3()))));
}
}
diff --git a/src/test/java/com/schooner/MemCached/WhalinScenarioTest.java b/src/test/java/com/schooner/MemCached/WhalinScenarioTest.java
index 1ccf580..9e000aa 100644
--- a/src/test/java/com/schooner/MemCached/WhalinScenarioTest.java
+++ b/src/test/java/com/schooner/MemCached/WhalinScenarioTest.java
@@ -1,5 +1,5 @@
/**
- *
+ *
*/
package com.schooner.MemCached;
@@ -16,7 +16,7 @@
/**
* @author newrootwang
- *
+ *
*/
public class WhalinScenarioTest extends TestCase {
@@ -47,17 +47,23 @@ public Integer getField3() {
return this.field3;
}
+ @Override
public boolean equals(Object o) {
- if (this == o)
+ if (this == o) {
return true;
- if (!(o instanceof TestClass))
+ }
+ if (!(o instanceof TestClass)) {
return false;
+ }
TestClass obj = (TestClass) o;
- return ((this.field1 == obj.getField1() || (this.field1 != null && this.field1.equals(obj.getField1())))
- && (this.field2 == obj.getField2() || (this.field2 != null && this.field2.equals(obj.getField2()))) && (this.field3 == obj
- .getField3() || (this.field3 != null && this.field3.equals(obj.getField3()))));
+ return ((this.field1 == obj.getField1() || (this.field1 != null && this.field1
+ .equals(obj.getField1())))
+ && (this.field2 == obj.getField2() || (this.field2 != null && this.field2
+ .equals(obj.getField2()))) && (this.field3 == obj
+ .getField3() || (this.field3 != null && this.field3
+ .equals(obj.getField3()))));
}
}
@@ -102,7 +108,9 @@ public void testMaxConn() {
assertEquals(19, pool.getMaxConn());
final MemCachedClient mc = new MemCachedClient();
Thread[] threads = new Thread[20];
- assertEquals(pool.getInitConn(), SchoonerSockIOPool.getInstance().socketPool.get(hosts[0]).getNumIdle());
+ assertEquals(pool.getInitConn(),
+ SchoonerSockIOPool.getInstance().socketPool.get(hosts[0])
+ .getNumIdle());
for (int i = 0; i < 20; ++i) {
threads[i] = new Thread() {
@Override
@@ -124,7 +132,8 @@ public void run() {
e.printStackTrace();
}
}
- assertTrue(SchoonerSockIOPool.getInstance().socketPool.get(hosts[0]).getNumActive() < 20);
+ assertTrue(SchoonerSockIOPool.getInstance().socketPool.get(hosts[0])
+ .getNumActive() < 20);
mc.flushAll();
pool.shutDown();
}
@@ -137,7 +146,9 @@ public void testMinConn() {
assertEquals(2, pool.getMinConn());
final MemCachedClient mc = new MemCachedClient();
Thread thread = new Thread();
- assertEquals(2, SchoonerSockIOPool.getInstance().socketPool.get(hosts[0]).getMinIdle());
+ assertEquals(2,
+ SchoonerSockIOPool.getInstance().socketPool.get(hosts[0])
+ .getMinIdle());
thread = new Thread() {
@Override
public void run() {
@@ -155,7 +166,9 @@ public void run() {
} catch (InterruptedException e) {
e.printStackTrace();
}
- assertEquals(2, SchoonerSockIOPool.getInstance().socketPool.get(hosts[0]).getNumActive());
+ assertEquals(2,
+ SchoonerSockIOPool.getInstance().socketPool.get(hosts[0])
+ .getNumActive());
mc.flushAll();
pool.shutDown();
}
@@ -167,8 +180,9 @@ public void testSetWeight() {
pool.setWeights(weights);
pool.initialize();
MemCachedClient mc = new MemCachedClient();
- for (int i = 0; i < 100; i++)
+ for (int i = 0; i < 100; i++) {
mc.set("key " + i, "value " + i);
+ }
pool.shutDown();
pool = SockIOPool.getInstance("check");
@@ -177,8 +191,9 @@ public void testSetWeight() {
pool.initialize();
mc = new MemCachedClient("check");
for (int i = 0; i < 100; i++) {
- if (mc.get("key " + i) != null)
+ if (mc.get("key " + i) != null) {
count++;
+ }
}
assertTrue(count <= 95 && count >= 85);
pool.shutDown();
@@ -389,7 +404,7 @@ public void testGetMultiArray() {
values[i] = "value " + i;
mc.set(keys[i], values[i]);
}
- Map rValues = mc.getMulti(keys, null, false);
+ Map rValues = mc.getMulti(keys, null, false);
for (int i = 0; i < 10; i++) {
assertEquals(values[i], rValues.get(keys[i]));
}
@@ -428,10 +443,13 @@ public void testGetWithIOException() {
assertEquals(expect, actual);
mc.setTransCoder(new TransCoder() {
- public int encode(SockOutputStream out, Object object) throws IOException {
+ @Override
+ public int encode(SockOutputStream out, Object object)
+ throws IOException {
throw new IOException();
}
+ @Override
public Object decode(InputStream input) throws IOException {
throw new IOException();
}
@@ -455,10 +473,13 @@ public void testGetsWithIOException() {
assertEquals(expect, actual);
mc.setTransCoder(new TransCoder() {
- public int encode(SockOutputStream out, Object object) throws IOException {
+ @Override
+ public int encode(SockOutputStream out, Object object)
+ throws IOException {
throw new IOException();
}
+ @Override
public Object decode(InputStream input) throws IOException {
throw new IOException();
}
@@ -476,10 +497,13 @@ public void testSetWithIOException() {
final MemCachedClient mc = new MemCachedClient(true, false);
mc.setTransCoder(new TransCoder() {
- public int encode(SockOutputStream out, Object object) throws IOException {
+ @Override
+ public int encode(SockOutputStream out, Object object)
+ throws IOException {
throw new IOException();
}
+ @Override
public Object decode(InputStream input) throws IOException {
throw new IOException();
}
@@ -522,7 +546,7 @@ public void testGetMulti() {
values[i] = "value " + i;
mc.set(keys[i], values[i]);
}
- Map rValues = mc.getMulti(keys);
+ Map rValues = mc.getMulti(keys);
for (int i = 0; i < 10; i++) {
assertEquals(values[i], rValues.get("key/ " + i));
}
@@ -543,7 +567,7 @@ public void testGetMultiBin() {
values[i] = "value " + i;
mc.set(keys[i], values[i]);
}
- Map rValues = mc.getMulti(keys);
+ Map rValues = mc.getMulti(keys);
for (int i = 0; i < 10; i++) {
assertEquals(values[i], rValues.get("key/ " + i));
}
diff --git a/src/test/java/com/whalin/MemCached/test/UnitTests.java b/src/test/java/com/whalin/MemCached/test/UnitTests.java
index 0677fe4..a5a3256 100644
--- a/src/test/java/com/whalin/MemCached/test/UnitTests.java
+++ b/src/test/java/com/whalin/MemCached/test/UnitTests.java
@@ -14,7 +14,7 @@
* library.
*
* @author Kevin Burton
- * @author greg whalin
+ * @author greg whalin
*/
package com.whalin.MemCached.test;
@@ -121,8 +121,8 @@ public static void test12() {
long i = 0;
mc.storeCounter("foo", i);
mc.incr("foo"); // foo now == 1
- mc.incr("foo", (long) 5); // foo now == 6
- long j = mc.decr("foo", (long) 2); // foo now == 4
+ mc.incr("foo", 5); // foo now == 6
+ long j = mc.decr("foo", 2); // foo now == 4
assert j == 4;
assert j == mc.getCounter("foo");
log.error("+ incr/decr test passed");
@@ -174,11 +174,11 @@ public static void test17() {
public static void test18() {
mc.addOrIncr("foo"); // foo now == 0
mc.incr("foo"); // foo now == 1
- mc.incr("foo", (long) 5); // foo now == 6
+ mc.incr("foo", 5); // foo now == 6
mc.addOrIncr("foo"); // foo now 7
- long j = mc.decr("foo", (long) 3); // foo now == 4
+ long j = mc.decr("foo", 3); // foo now == 4
assert j == 4;
assert j == mc.getCounter("foo");
@@ -193,7 +193,7 @@ public static void test19() {
mc.set(keys[i], "value" + i);
}
- Map results = mc.getMulti(keys);
+ Map results = mc.getMulti(keys);
for (int i = 0; i < max; i++) {
assert results.get(keys[i]).equals("value" + i);
}
@@ -201,7 +201,9 @@ public static void test19() {
}
public static void test20(int max, int skip, int start) {
- log.warn(String.format("test 20 starting with start=%5d skip=%5d max=%7d", start, skip, max));
+ log.warn(String.format(
+ "test 20 starting with start=%5d skip=%5d max=%7d", start,
+ skip, max));
int numEntries = max / skip + 1;
String[] keys = new String[numEntries];
byte[][] vals = new byte[numEntries][];
@@ -210,18 +212,22 @@ public static void test20(int max, int skip, int start) {
for (int i = 0; i < numEntries; i++) {
keys[i] = Integer.toString(size);
vals[i] = new byte[size + 1];
- for (int j = 0; j < size + 1; j++)
+ for (int j = 0; j < size + 1; j++) {
vals[i][j] = (byte) j;
+ }
mc.set(keys[i], vals[i]);
size += skip;
}
- Map results = mc.getMulti(keys);
- for (int i = 0; i < numEntries; i++)
+ Map results = mc.getMulti(keys);
+ for (int i = 0; i < numEntries; i++) {
assert Arrays.equals((byte[]) results.get(keys[i]), vals[i]);
+ }
- log.warn(String.format("test 20 finished with start=%5d skip=%5d max=%7d", start, skip, max));
+ log.warn(String.format(
+ "test 20 finished with start=%5d skip=%5d max=%7d", start,
+ skip, max));
}
public static void test21() {
@@ -233,8 +239,9 @@ public static void test21() {
public static void test22() {
byte[] b = new byte[10];
- for (int i = 0; i < 10; i++)
+ for (int i = 0; i < 10; i++) {
b[i] = (byte) i;
+ }
mc.set("foo", b);
assert Arrays.equals((byte[]) mc.get("foo"), b);
@@ -244,20 +251,21 @@ public static void test22() {
public static void test23() {
TestClass tc = new TestClass("foo", "bar", new Integer(32));
mc.set("foo", tc);
- assert tc.equals((TestClass) mc.get("foo"));
+ assert tc.equals(mc.get("foo"));
log.error("+ store/retrieve serialized object test passed");
}
public static void test24() {
- String[] allKeys = { "key1", "key2", "key3", "key4", "key5", "key6", "key7" };
+ String[] allKeys = { "key1", "key2", "key3", "key4", "key5", "key6",
+ "key7" };
String[] setKeys = { "key1", "key3", "key5", "key7" };
for (String key : setKeys) {
mc.set(key, key);
}
- Map results = mc.getMulti(allKeys);
+ Map results = mc.getMulti(allKeys);
assert allKeys.length == results.size();
for (String key : setKeys) {
@@ -293,8 +301,9 @@ public static void runAlTests(MemCachedClient mc) {
test23();
test24();
- for (int i = 0; i < 3; i++)
+ for (int i = 0; i < 3; i++) {
test19();
+ }
test20(8191, 1, 0);
test20(8192, 1, 0);
@@ -321,10 +330,10 @@ public static void runAlTests(MemCachedClient mc) {
/**
* This runs through some simple tests of the MemcacheClient.
- *
+ *
* Command line args: args[0] = number of threads to spawn args[1] = number
* of runs per thread args[2] = size of object to store
- *
+ *
* @param args
* the command line arguments
*/
@@ -342,8 +351,9 @@ public static void main(String[] args) {
Integer[] weights = { 1 };
- if (args.length > 0)
+ if (args.length > 0) {
serverlist = args;
+ }
// initialize the pool for memcache servers
SockIOPool pool = SockIOPool.getInstance("test");
@@ -360,7 +370,7 @@ public static void main(String[] args) {
/**
* Class for testing serializing of objects.
- *
+ *
* @author $Author: $
* @version $Revision: $ $Date: $
*/
@@ -389,17 +399,23 @@ public Integer getField3() {
return this.field3;
}
+ @Override
public boolean equals(Object o) {
- if (this == o)
+ if (this == o) {
return true;
- if (!(o instanceof TestClass))
+ }
+ if (!(o instanceof TestClass)) {
return false;
+ }
TestClass obj = (TestClass) o;
- return ((this.field1 == obj.getField1() || (this.field1 != null && this.field1.equals(obj.getField1())))
- && (this.field2 == obj.getField2() || (this.field2 != null && this.field2.equals(obj.getField2()))) && (this.field3 == obj
- .getField3() || (this.field3 != null && this.field3.equals(obj.getField3()))));
+ return ((this.field1 == obj.getField1() || (this.field1 != null && this.field1
+ .equals(obj.getField1())))
+ && (this.field2 == obj.getField2() || (this.field2 != null && this.field2
+ .equals(obj.getField2()))) && (this.field3 == obj
+ .getField3() || (this.field3 != null && this.field3
+ .equals(obj.getField3()))));
}
}
}
diff --git a/src/test/resources/log4j.properties b/src/test/resources/log4j.properties
new file mode 100644
index 0000000..71131a8
--- /dev/null
+++ b/src/test/resources/log4j.properties
@@ -0,0 +1,7 @@
+log4j.rootLogger=error,console
+log4j.threshold=ALL
+
+log4j.appender.console=org.apache.log4j.ConsoleAppender
+log4j.appender.console.target=System.out
+log4j.appender.console.layout=org.apache.log4j.PatternLayout
+#log4j.appender.console.layout.ConversionPattern=%d{ISO8601} [%t] %p %c{2}: %m%n
diff --git a/src/test/resources/memcached.properties b/src/test/resources/memcached.properties
new file mode 100644
index 0000000..c0ae6bf
--- /dev/null
+++ b/src/test/resources/memcached.properties
@@ -0,0 +1,2 @@
+#memcached server ip and port
+memcache.server=10.1.169.132:12321
\ No newline at end of file
diff --git a/src/test/resources/spring-memcached-Binary.xml b/src/test/resources/spring-memcached-Binary.xml
new file mode 100644
index 0000000..a5b9d6e
--- /dev/null
+++ b/src/test/resources/spring-memcached-Binary.xml
@@ -0,0 +1,34 @@
+
+
+
+
+ test
+
+
+ false
+
+
+ true
+
+
+
+
+
+ test
+
+
+
+ ${memcache.server}
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/test/resources/spring-memcached-TCP.xml b/src/test/resources/spring-memcached-TCP.xml
new file mode 100644
index 0000000..6be6fe9
--- /dev/null
+++ b/src/test/resources/spring-memcached-TCP.xml
@@ -0,0 +1,28 @@
+
+
+
+
+ test
+
+
+
+
+
+ test
+
+
+
+ ${memcache.server}
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/test/resources/spring-memcached-UDP.xml b/src/test/resources/spring-memcached-UDP.xml
new file mode 100644
index 0000000..b6ed7eb
--- /dev/null
+++ b/src/test/resources/spring-memcached-UDP.xml
@@ -0,0 +1,34 @@
+
+
+
+
+ test
+
+
+ false
+
+
+ false
+
+
+
+
+
+ test
+
+
+
+ ${memcache.server}
+
+
+
+
+
+
\ No newline at end of file