import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
-import mtvi.util.cache.distributed.Memcache;
import net.spy.memcached.AddrUtil;
import net.spy.memcached.BinaryConnectionFactory;
import net.spy.memcached.KetamaConnectionFactory;
import net.spy.memcached.MemcachedClient;
+
import org.apache.commons.lang.StringUtils;
+
import com.mtvnet.platform.MTVNGenericService;
import com.mtvnet.platform.MTVNServiceException;
@@ -35,296 +36,283 @@
public class SpyMemcache extends MTVNGenericService implements Memcache {
- // Distributed spymemcached client instance
- private MemcachedClient client;
- private String[] servers;
- private int cacheOperationTimeoutSeconds;
- private long defaultExpirationTimeSeconds;
- private String connectionType;
- private String spyLoggerClassName;
+ // Distributed spymemcached client instance
+ private MemcachedClient client;
+ private String[] servers;
+ private int cacheOperationTimeoutSeconds;
+ private long defaultExpirationTimeSeconds;
+ private String connectionType;
+ private String spyLoggerClassName;
- /**
- * default constructor injected
- */
- public SpyMemcache(MemcachedClient client) {
- if(client==null)
- this.client=client;
-
- }
+ /**
+ * Constructor with no arguments for class to be used as nucleus component
+ */
+ public SpyMemcache() {
- public void doStartService() throws MTVNServiceException {
+ }
- super.doStartService();
- setupClient();
- }
+ /**
+ * default constructor injected
+ */
+ public SpyMemcache(MemcachedClient client) {
+ if (client == null)
+ this.client = client;
- /**
- * Cleans up asynchronous-refresh threads
- */
- public void doStopService() throws MTVNServiceException {
- shutdown();
- }
+ }
- /**
- * Get a memcached client connected to server
- */
- public void setupClient() throws MTVNServiceException {
- System.setProperty("net.spy.compat.log.LoggerImpl",getSpyLoggerClassName());
- try {
- if (getConnectionType().equalsIgnoreCase("default")) {
- // Create a defaultConnectionFactory.
- client = new MemcachedClient(AddrUtil.getAddresses(StringUtils
- .join(getServers(), ' ')));
- if (isLoggingDebug()) {
- logDebug("Msg: SimpleConnection : " + client);
- }
- } else if (getConnectionType().equalsIgnoreCase("binary")) {
- // Create a binaryConnectionFactory with the default parameters.
- client = new MemcachedClient(new BinaryConnectionFactory(),
- AddrUtil.getAddresses(StringUtils.join(getServers(),
- ' ')));
- if (isLoggingDebug()) {
- logDebug("Msg: BinaryConnection : " + client);
- }
- } else {
- // Create a KetamaConnectionFactory with the default parameters.
- client = new MemcachedClient(new KetamaConnectionFactory(),
- AddrUtil.getAddresses(StringUtils.join(getServers(),
- ' ')));
- if (isLoggingDebug()) {
- logDebug("Msg: KetamaConnection : " + client);
- }
- }
- } catch (IOException e) {
- if (isLoggingError()) {
- throw new MTVNServiceException("fail to get"
- + getConnectionType() + "connection client", e
- .getCause());
- }
- }
+ public void doStartService() throws MTVNServiceException {
- }
+ super.doStartService();
+ setupClient();
+ }
- /**
- * Method returns the Object against the key or a null value if key value is
- * not available.
- *
- * @param key
- * value in the cache
- * @return the Object
- */
+ /**
+ * Cleans up asynchronous-refresh threads
+ */
+ public void doStopService() throws MTVNServiceException {
+ shutdown();
+ }
- public Object get(String key) {
- Object myObj = null;
- Future<Object> future = client.asyncGet(key);
- try {
- myObj = future.get(getCacheOperationTimeoutSeconds(),
- TimeUnit.SECONDS);
- } catch (TimeoutException e) { // java.util.concurrent.TimeoutException
- logError("Timeout attempting get item from memcache after "
- + getCacheOperationTimeoutSeconds() + " seconds");
- } catch (Exception e) {
- logError("Failed to get an item from memcache", e.getCause());
- }
- if (isLoggingDebug()) {
- logDebug("Msg: Get myObj " + myObj);
- }
- return myObj;
- }
+ /**
+ * Get a memcached client connected to server
+ */
+ private void setupClient() throws MTVNServiceException {
+ System.setProperty("net.spy.compat.log.LoggerImpl", getSpyLoggerClassName());
+ try {
+ if (getConnectionType().equalsIgnoreCase("default")) {
+ // Create a defaultConnectionFactory.
+ client = new MemcachedClient(AddrUtil.getAddresses(StringUtils.join(getServers(),
+ ' ')));
+ if (isLoggingDebug()) {
+ logDebug("Configuring memcache client with default connection protocol ");
+ }
+ } else if (getConnectionType().equalsIgnoreCase("binary")) {
+ // Create a binaryConnectionFactory with the default parameters.
+ client = new MemcachedClient(new BinaryConnectionFactory(), AddrUtil
+ .getAddresses(StringUtils.join(getServers(), ' ')));
+ if (isLoggingDebug()) {
+ logDebug("Configuring memcache client with binary connection protocol");
+ }
+ } else {
+ // Create a KetamaConnectionFactory with the default parameters.
+ client = new MemcachedClient(new KetamaConnectionFactory(), AddrUtil
+ .getAddresses(StringUtils.join(getServers(), ' ')));
+ if (isLoggingDebug()) {
+ logDebug("Configuring memcache client with Ketama connection protocol");
+ }
+ }
+ } catch (IOException e) {
+ if (isLoggingError()) {
+ throw new MTVNServiceException("fail to get " + getConnectionType()
+ + " connection client", e.getCause());
+ }
+ }
- /**
- * Add an object to the cache if it does not exist.
- *
- * @param key
- * value in the cache
- * @param the
- * Object of key value pair
- * @param expiration
- * the time in seconds value will be there in cache
- * @return boolean for sucess/failure to add in cache
- */
+ }
- public boolean add(String key, Object value, int expiration) {
- Future<Boolean> future = client.add(key, expiration, value);
- try {
- return future.get(getCacheOperationTimeoutSeconds(),
- TimeUnit.SECONDS);
- } catch (TimeoutException e) { // java.util.concurrent.TimeoutException
- if (isLoggingError()) {
- logError("Timeout attempting add item in memcache after "
- + getCacheOperationTimeoutSeconds() + " seconds");
- }
- } catch (Exception e) {
- if (isLoggingError()) {
- logError("Failed to add item in memcache", e.getCause());
- }
- }
- return false;
- }
+ /**
+ * Method returns the Object against the key or a null value if key value is
+ * not available.
+ *
+ * @param key value in the cache
+ * @return the Object
+ */
- /**
- * Delete the given key from the cache.
- *
- * @param key
- * value in the cache that needs to delete
- * @return boolean for sucess/failure to delete in cache
- */
+ public Object get(String key) {
+ Object myObj = null;
+ Future<Object> future = client.asyncGet(key);
+ try {
+ myObj = future.get(getCacheOperationTimeoutSeconds(), TimeUnit.SECONDS);
+ } catch (TimeoutException e) { // java.util.concurrent.TimeoutException
+ logError("Timeout attempting get item from memcache after "
+ + getCacheOperationTimeoutSeconds() + " seconds");
+ } catch (Exception e) {
+ logError("Failed to get an item from memcache", e.getCause());
+ }
+ if (isLoggingDebug()) {
+ logDebug("Msg: Get myObj " + myObj);
+ }
+ return myObj;
+ }
- public boolean delete(String key) {
- Future<Boolean> future = client.delete(key);
- try {
- return future.get(getCacheOperationTimeoutSeconds(),
- TimeUnit.SECONDS);
- } catch (TimeoutException e) { // java.util.concurrent.TimeoutException
- if (isLoggingError()) {
- logError("Timeout attempting delete item from memcache after "
- + getCacheOperationTimeoutSeconds() + " seconds");
- }
- } catch (Exception e) {
- if (isLoggingError()) {
- logError("Failed to delete an item in memcache", e.getCause());
- }
- }
- return false;
- }
+ /**
+ * Add an object to the cache if it does not exist.
+ *
+ * @param key value in the cache
+ * @param the Object of key value pair
+ * @param expiration the time in seconds value will be there in cache
+ * @return boolean for sucess/failure to add in cache
+ */
- /**
- * Set an object in the cache regardless of any existing value.
- *
- * @param key
- * value in the cache
- * @param the
- * Object of key value pair
- * @param cacheTimeMillis
- * @return boolean for sucess/failure to set in cache
- */
+ public boolean add(String key, Object value, int expiration) {
+ Future<Boolean> future = client.add(key, expiration, value);
+ try {
+ return future.get(getCacheOperationTimeoutSeconds(), TimeUnit.SECONDS);
+ } catch (TimeoutException e) { // java.util.concurrent.TimeoutException
+ if (isLoggingError()) {
+ logError("Timeout attempting add item in memcache after "
+ + getCacheOperationTimeoutSeconds() + " seconds");
+ }
+ } catch (Exception e) {
+ if (isLoggingError()) {
+ logError("Failed to add item in memcache ", e.getCause());
+ }
+ }
+ return false;
+ }
- public boolean set(String key, Object value, long cacheTimeMillis) {
- int expiration = (int) (cacheTimeMillis / 1000);
- if (isLoggingDebug()) {
- logDebug("Msg: In Set method: ");
- }
- Future<Boolean> future = client.set(key, expiration, value);
- try {
- return future.get(getCacheOperationTimeoutSeconds(),
- TimeUnit.SECONDS);
- } catch (TimeoutException e) { // java.util.concurrent.TimeoutException
- if (isLoggingError()) {
- logError("Timeout attempting set item in memcache after "
- + getCacheOperationTimeoutSeconds() + " seconds");
- }
- } catch (Exception e) {
- if (isLoggingError()) {
- logError("Failed to set an item in memcache", e.getCause());
- }
- }
- return false;
- }
+ /**
+ * Delete the given key from the cache.
+ *
+ * @param key value in the cache that needs to delete
+ * @return boolean for sucess/failure to delete in cache
+ */
- /**
- * Set an object in the cache regardless of any existing value.
- *
- * @param key
- * value in the cache
- * @param the
- * Object of key value pair
- *
- * @return boolean for sucess/failure to set in cache
- */
+ public boolean delete(String key) {
+ Future<Boolean> future = client.delete(key);
+ try {
+ return future.get(getCacheOperationTimeoutSeconds(), TimeUnit.SECONDS);
+ } catch (TimeoutException e) { // java.util.concurrent.TimeoutException
+ if (isLoggingError()) {
+ logError("Timeout attempting delete item from memcache after "
+ + getCacheOperationTimeoutSeconds() + " seconds");
+ }
+ } catch (Exception e) {
+ if (isLoggingError()) {
+ logError("Failed to delete an item in memcache ", e.getCause());
+ }
+ }
+ return false;
+ }
- public boolean set(String key, Object o) {
- return set(key, o, getDefaultExpirationTimeSeconds() * 1000);
- }
+ /**
+ * Set an object in the cache regardless of any existing value.
+ *
+ * @param key value in the cache
+ * @param the Object of key value pair
+ * @param cacheTimeMillis
+ * @return boolean for sucess/failure to set in cache
+ */
- /**
- * Replace an object with the given value if there is already a value for
- * the given key.
- *
- * @param key
- * value in the cache
- * @param the
- * Object of key value pair
- * @param expiration
- * the time in seconds value will be there in cache
- * @return boolean for sucess/failure to replace value in cache
- */
+ public boolean set(String key, Object value, long cacheTimeMillis) {
+ int expiration = (int) (cacheTimeMillis / 1000);
+ if (isLoggingDebug()) {
+ logDebug("Msg: In Set method: ");
+ }
+ Future<Boolean> future = client.set(key, expiration, value);
+ try {
+ return future.get(getCacheOperationTimeoutSeconds(), TimeUnit.SECONDS);
+ } catch (TimeoutException e) { // java.util.concurrent.TimeoutException
+ if (isLoggingError()) {
+ logError("Timeout attempting set item in memcache after "
+ + getCacheOperationTimeoutSeconds() + " seconds");
+ }
+ } catch (Exception e) {
+ if (isLoggingError()) {
+ logError("Failed to set an item in memcache ", e.getCause());
+ }
+ }
+ return false;
+ }
- public boolean replace(String key, Object value, int expiration) {
- Future<Boolean> future = client.replace(key, expiration, value);
- try {
- return future.get(getCacheOperationTimeoutSeconds(),
- TimeUnit.SECONDS);
- } catch (TimeoutException e) { // java.util.concurrent.TimeoutException
- if (isLoggingError()) {
- logError("Timeout attempting replace item in memcache after "
- + getCacheOperationTimeoutSeconds() + " seconds");
- }
- } catch (Exception e) {
- if (isLoggingError()) {
- logError("Failed to replace an item in memcache", e.getCause());
- }
- }
- return false;
- }
+ /**
+ * Set an object in the cache regardless of any existing value.
+ *
+ * @param key value in the cache
+ * @param the Object of key value pair
+ *
+ * @return boolean for sucess/failure to set in cache
+ */
- /**
- * Array of distributed MemCached Server e.g "
server1.mydomain.com:11211",
- * "
server2.mydomain.com:11211", "
server3.mydomain.com:11211"
- */
+ public boolean set(String key, Object o) {
+ return set(key, o, getDefaultExpirationTimeSeconds() * 1000);
+ }
- public String[] getServers() {
- return servers;
- }
+ /**
+ * Replace an object with the given value if there is already a value for
+ * the given key.
+ *
+ * @param key value in the cache
+ * @param the Object of key value pair
+ * @param expiration the time in seconds value will be there in cache
+ * @return boolean for sucess/failure to replace value in cache
+ */
- public void setServers(String[] servers) {
- this.servers = servers;
- }
+ public boolean replace(String key, Object value, int expiration) {
+ Future<Boolean> future = client.replace(key, expiration, value);
+ try {
+ return future.get(getCacheOperationTimeoutSeconds(), TimeUnit.SECONDS);
+ } catch (TimeoutException e) { // java.util.concurrent.TimeoutException
+ if (isLoggingError()) {
+ logError("Timeout attempting replace item in memcache after "
+ + getCacheOperationTimeoutSeconds() + " seconds ");
+ }
+ } catch (Exception e) {
+ if (isLoggingError()) {
+ logError("Failed to replace an item in memcache", e.getCause());
+ }
+ }
+ return false;
+ }
- public String getConnectionType() {
- return connectionType;
- }
+ /**
+ * Array of distributed MemCached Server e.g "
server1.mydomain.com:11211",
+ * "
server2.mydomain.com:11211", "
server3.mydomain.com:11211"
+ */
- public void setConnectionType(String connectionType) {
- this.connectionType = connectionType;
- }
+ public String[] getServers() {
+ return servers;
+ }
- public int getCacheOperationTimeoutSeconds() {
- return cacheOperationTimeoutSeconds;
- }
+ public void setServers(String[] servers) {
+ this.servers = servers;
+ }
- public void setCacheOperationTimeoutSeconds(int cacheOperationTimeoutSeconds) {
- this.cacheOperationTimeoutSeconds = cacheOperationTimeoutSeconds;
- }
+ public String getConnectionType() {
+ return connectionType;
+ }
- public long getDefaultExpirationTimeSeconds() {
- return defaultExpirationTimeSeconds;
- }
+ public void setConnectionType(String connectionType) {
+ this.connectionType = connectionType;
+ }
- public void setDefaultExpirationTimeSeconds(
- long defaultExpirationTimeSeconds) {
- this.defaultExpirationTimeSeconds = defaultExpirationTimeSeconds;
- }
+ public int getCacheOperationTimeoutSeconds() {
+ return cacheOperationTimeoutSeconds;
+ }
- public void setSpyLoggerClassName(String spyLoggerClassName) {
- this.spyLoggerClassName = spyLoggerClassName;
- }
- public String getSpyLoggerClassName() {
- return spyLoggerClassName;
- }
+ public void setCacheOperationTimeoutSeconds(int cacheOperationTimeoutSeconds) {
+ this.cacheOperationTimeoutSeconds = cacheOperationTimeoutSeconds;
+ }
- /**
- * Clear the spymemcache client
- */
- public void clear() {
- client.flush();
- }
+ public long getDefaultExpirationTimeSeconds() {
+ return defaultExpirationTimeSeconds;
+ }
- /**
- * shutdown the spymemcache client
- */
+ public void setDefaultExpirationTimeSeconds(long defaultExpirationTimeSeconds) {
+ this.defaultExpirationTimeSeconds = defaultExpirationTimeSeconds;
+ }
- public void shutdown() {
- client.shutdown();
- }
+ public void setSpyLoggerClassName(String spyLoggerClassName) {
+ this.spyLoggerClassName = spyLoggerClassName;
+ }
+ public String getSpyLoggerClassName() {
+ return spyLoggerClassName;
+ }
+
+ /**
+ * Clear the spymemcache client
+ */
+ public void clear() {
+ client.flush();
+ }
+
+ /**
+ * shutdown the spymemcache client
+ */
+
+ public void shutdown() {
+ client.shutdown();
+ }
+
}