com.nervousync.cache.provider.impl.MemcachedProviderImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cache-impl-memcached Show documentation
Show all versions of cache-impl-memcached Show documentation
Cache implement provider by memcached using xmemcached, development by Nervousync Studio (NSYC)
The newest version!
/*
* Licensed to the Nervousync Studio (NSYC) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.nervousync.cache.provider.impl;
import java.io.IOException;
import java.util.List;
import com.nervousync.cache.configs.CacheServer;
import com.nervousync.cache.provider.CacheProvider;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.auth.AuthInfo;
import net.rubyeye.xmemcached.command.BinaryCommandFactory;
import net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator;
import net.rubyeye.xmemcached.utils.AddrUtil;
/**
* @author Steven Wee [email protected]
* @version $Revision: 1.0 $ $Date: Apr 25, 2017 3:50:54 PM $
*/
public final class MemcachedProviderImpl extends CacheProvider {
/**
* Memcached client object
*/
private MemcachedClient memcachedClient = null;
/*
* (non-Javadoc)
* @see com.nervousync.cache.provider.CacheProvider#initializeConnection(java.util.List)
*/
@Override
public void initializeConnection(List serverConfigList) {
int[] serverWeightList = new int[serverConfigList.size()];
int index = 0;
StringBuilder serverAddresses = new StringBuilder();
for (CacheServer memcachedServer : serverConfigList) {
serverAddresses.append(" " + memcachedServer.getServerAddress() + ":" + memcachedServer.getServerPort());
serverWeightList[index] = memcachedServer.getServerWeight();
}
MemcachedClientBuilder clientBuilder =
new XMemcachedClientBuilder(AddrUtil.getAddresses(serverAddresses.toString().trim()), serverWeightList);
clientBuilder.setCommandFactory(new BinaryCommandFactory());
if (serverConfigList.size() > 1) {
clientBuilder.setSessionLocator(new KetamaMemcachedSessionLocator());
}
for (CacheServer memcachedServer : serverConfigList) {
if (memcachedServer.getServerUserName() != null && memcachedServer.getServerUserName().length() > 0
&& memcachedServer.getServerPassword() != null && memcachedServer.getServerPassword().length() > 0) {
clientBuilder.addAuthInfo(AddrUtil.getOneAddress(memcachedServer.getServerAddress() + ":" + memcachedServer.getServerPort()),
AuthInfo.typical(memcachedServer.getServerUserName(), memcachedServer.getServerPassword()));
}
}
try {
this.memcachedClient = clientBuilder.build();
} catch (IOException e) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Initialize memcached client error! ", e);
}
}
}
/*
* (non-Javadoc)
* @see com.nervousync.cache.provider.CacheProvider#set(java.lang.String, java.lang.Object, int)
*/
@Override
public void set(String key, Object value, int expiry) {
try {
this.memcachedClient.set(key, expiry, value);
} catch (Exception e) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Set data error! ", e);
}
}
}
/*
* (non-Javadoc)
* @see com.nervousync.cache.provider.CacheProvider#add(java.lang.String, java.lang.Object, int)
*/
@Override
public void add(String key, Object value, int expiry) {
try {
this.memcachedClient.add(key, expiry, value);
} catch (Exception e) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Set data error! ", e);
}
}
}
/*
* (non-Javadoc)
* @see com.nervousync.cache.provider.CacheProvider#replace(java.lang.String, java.lang.Object, int)
*/
@Override
public void replace(String key, Object value, int expiry) {
try {
this.memcachedClient.replace(key, expiry, value);
} catch (Exception e) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Set data error! ", e);
}
}
}
/*
* (non-Javadoc)
* @see com.nervousync.cache.provider.CacheProvider#touch(java.lang.String, int)
*/
@Override
public void touch(String key, int expiry) {
try {
this.memcachedClient.touch(key, expiry);
} catch (Exception e) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Set data error! ", e);
}
}
}
/*
* (non-Javadoc)
* @see com.nervousync.cache.provider.CacheProvider#delete(java.lang.String)
*/
@Override
public void delete(String key) {
try {
this.memcachedClient.delete(key);
} catch (Exception e) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Set data error! ", e);
}
}
}
/*
* (non-Javadoc)
* @see com.nervousync.cache.provider.CacheProvider#get(java.lang.String)
*/
@Override
public Object get(String key) {
try {
return this.memcachedClient.get(key);
} catch (Exception e) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Get data error! ", e);
}
}
return null;
}
/*
* (non-Javadoc)
* @see com.nervousync.cache.provider.CacheProvider#destroy()
*/
@Override
public void destroy() {
if (this.memcachedClient != null) {
if (!this.memcachedClient.isShutdown()) {
try {
this.memcachedClient.shutdown();
} catch (IOException e) {
this.logger.error("Shutdown memcached client error! ");
if (this.logger.isDebugEnabled()) {
this.logger.debug("Stack message: ", e);
}
}
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy