MemcachedClient

/**

 * 

 */

package com.xcj.common.memcached;

 

import java.io.IOException;

 

import net.spy.memcached.AddrUtil;

import net.spy.memcached.ConnectionFactoryBuilder;

import net.spy.memcached.MemcachedClient;

import net.spy.memcached.ConnectionFactoryBuilder.Protocol;

 

import com.xcj.common.util.properties.ReadPropertiesUtil;

 

/**

 * <b>function:</b> MemberCache缓存单例实例只打开一个实例

 * 

 * @project xcjframe

 * @package com.xcj.common.memcached

 * @fileName com.xcj.*

 * @createDate Apr 15, 2014 12:26:24 PM

 * @author nyy

 */

public class MemcachedClientFactory {

private volatile static MemcachedClient instance;

 

private MemcachedClientFactory() {

};

/**

  * <b>function:</b> 防止Mc创建更多的实例,故采用单例模式<br>

  * 放到正式环境阿里云服务区则把注释的放开

  * @project xcjframe

  * @package com.xcj.common.memcached  

  * @fileName com.xcj.common.memcached.MemcachedClientFactory 

  * @createDate Apr 15, 2014 2:49:34 PM

  * @return MemcachedClient

  * @author su_jian

*/

public static MemcachedClient getInstance() throws IOException {

if (instance == null) {

synchronized (MemcachedClient.class) {

if (instance == null) {

//TODO 放到正式正式服务器放开 username passowrd 

// AuthDescriptor ad = new AuthDescriptor(new String[]{"PLAIN"}, new PlainCallbackHandler("2a1e34a8d37011e3", "Xcj1qaz2wsx")); // 用户名,密码 

instance = new MemcachedClient(

new ConnectionFactoryBuilder().setProtocol(

Protocol.BINARY) // 指定使用Binary协议

.setOpTimeout(800222312)// 设置超时时间为100ms

//TODO 放到正式正式服务器放开

// .setAuthDescriptor(ad)

.build(), AddrUtil

.getAddresses(ReadPropertiesUtil.getProperties("memcached.ip")+":"+ReadPropertiesUtil.getProperties("memcached.port")));

}

}

}

return instance;

}

 

}

//---------------------------------------------------------

package com.xcj.common.memcached;

 

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import java.util.concurrent.ExecutionException;

 

import net.spy.memcached.MemcachedClient;

import net.spy.memcached.internal.OperationFuture;

 

import com.xcj.common.util.properties.ReadPropertiesUtil;

 

/**

 * <b>function:</b> Membercache缓存工具类

 * 

 * @project xcjframe

 * @package com.xcj.common.memcached

 * @fileName com.xcj.common.memcached.MemcachedUtil

 * @createDate Apr 15, 2014 2:45:52 PM

 * @author su_jian

 */

public class MemcachedUtil {

 

  /**

   * <b>function:</b> Membercache 将对象放到缓存工具类

   * @project xcjframe

   * @package com.xcj.common.memcached  

   * @fileName com.xcj.*** 

   * @createDate Apr 15, 2014 3:58:13 PM

   * @return MemcachedClient

   * @author su_jian

   */

@SuppressWarnings("unchecked")

public static MemcachedClient putMemcacheIntance(HashMap<String, String> map) {

MemcachedClient mc = null;

try {

// 指定验证机制,推荐PLAIN,

// 部分客户端存在协议BUG,只能使用PLAIN协议(PlainCallbackHandler)

mc = MemcachedClientFactory.getInstance();

OperationFuture future = null;

for (Map.Entry entry : map.entrySet()) {

future = mc.set("" + entry.getKey() + "",Integer.valueOf(ReadPropertiesUtil.getProperties("memcached.exptime")), entry

.getValue()); // 异步接口,注意!返回的是Future

}

future.get(); // future.get() 确保之前(mc.set())操作已经结束

} catch (IOException e) {

e.printStackTrace();

} catch (InterruptedException e) {

e.printStackTrace();

} catch (ExecutionException e) {

e.printStackTrace();

} finally {

if (mc != null) {

}

// mc.shutdown(); // 关闭,释放资源

}

return mc;

}

 

/**

  * <b>function:</b> Membercache 将对象放到缓存工具类 设置缓存时间

  * @project xcjframe

  * @package com.xcj.common.memcached  

  * @fileName com.xcj.*** 

  * @createDate Apr 15, 2014 3:58:13 PM

  * @return MemcachedClient

  * @author nyy

  */

@SuppressWarnings("unchecked")

public static MemcachedClient putMemcacheIntance(HashMap<String, String> map,Integer time) {

MemcachedClient mc = null;

try {

// 指定验证机制,推荐PLAIN,

// 部分客户端存在协议BUG,只能使用PLAIN协议(PlainCallbackHandler)

mc = MemcachedClientFactory.getInstance();

OperationFuture future = null;

 

for (Map.Entry entry : map.entrySet()) {

future = mc.set("" + entry.getKey() + "",time, entry.getValue()); // 异步接口,注意!返回的是Future

}

future.get(); // future.get() 确保之前(mc.set())操作已经结束

} catch (IOException e) {

e.printStackTrace();

} catch (InterruptedException e) {

e.printStackTrace();

} catch (ExecutionException e) {

e.printStackTrace();

} finally {

if (mc != null) {

}

// mc.shutdown(); // 关闭,释放资源

}

return mc;

}

 

/*

* 根据key来删除缓存

*/

public static boolean deteleKey(String key){

try{

MemcachedClient mc = null;

mc.delete(key);

return true;

}catch(Exception ex){

ex.printStackTrace();

return false;

}

}

 

 

/**

  * <b>function:</b> Membercache 将对象放到缓存工具类

  * @project xcjframe

  * @package com.xcj.common.memcached  

  * @fileName com.xcj.*** 

  * @createDate Apr 15, 2014 3:58:13 PM

  * @return MemcachedClient

  * @author su_jian

  */

@SuppressWarnings("unchecked")

public static void putMemcacheIntance(Object obj) {

MemcachedClient mc = null;

try {

// 指定验证机制,推荐PLAIN,

// 部分客户端存在协议BUG,只能使用PLAIN协议(PlainCallbackHandler)

mc = MemcachedClientFactory.getInstance();

OperationFuture future = null;

future = mc.add(obj+"_xcj",Integer.valueOf(ReadPropertiesUtil.getProperties("memcached.exptime")), obj); // 异步接口,注意!返回的是Future

future.get(); // future.get() 确保之前(mc.set())操作已经结束

} catch (IOException e) {

e.printStackTrace();

} catch (InterruptedException e) {

e.printStackTrace();

} catch (ExecutionException e) {

e.printStackTrace();

} finally {

if (mc != null) {

}

// mc.shutdown(); // 关闭,释放资源

}

}

 

 

/**

* <b>function:</b>刷新缓存没有时间

* @project xcjframe

* @package com.xcj.common.memcached

* @fileName com.xcj.***

* @createDate May 12, 2014 2:38:14 PM

* @return void

* @author su_jian

*/

public static void flushMemcache() {

MemcachedClient mc = null;

try {

mc = MemcachedClientFactory.getInstance();

mc.flush();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* <b>function:</b>刷新缓存延迟时间

* @project xcjframe

* @param  int 延迟时间

* @package com.xcj.common.memcached

* @fileName com.xcj.***

* @createDate May 12, 2014 2:38:14 PM

* @return void

* @author su_jian

*/

public static void flushMemcache(int delay) {

MemcachedClient mc = null;

try {

mc = MemcachedClientFactory.getInstance();

mc.flush(delay);

} catch (IOException e) {

e.printStackTrace();

}

}

 

}

 

 

//-------------------------------------

@SuppressWarnings("unchecked")

public List<Dict> getDictList(String typeCode) {

MemcachedClient mc;

List<Dict> list = null;

try {

mc = MemcachedClientFactory.getInstance();

list = (List) mc.get(typeCode);

if (list == null) {

HashMap map = dictService.getDictMap();

MemcachedUtil.putMemcacheIntance(map);

list = (List) mc.get(typeCode);

}else{

System.out.print("111111111111111111");

}

} catch (IOException e) {

e.printStackTrace();

}

return list;

}

 

//------------------------------------------

public void setSession(String sessionId, String token, Integer userId){

HashMap map = new HashMap();

map.put("sessionId", sessionId);

map.put("token", token);

map.put("date",DateUtil.getCurrentTimeByDate());

map.put("userId", userId);

MemcachedUtil.putMemcacheIntance(map,10000);

 

 

}

 

 

你可能感兴趣的:(memcached)