评论

收藏

[NoSQL] Memcached之——与Spring集成(单服务器)

数据库 数据库 发布于:2021-07-01 10:23 | 阅读数:217 | 评论:0

转载请注明出处:http://blog.csdn.net/l1028386804/article/details/48229145
  Memcached客户端与Spring集成,客户端使用的是Memcached-Java-Client-release_2.6.3
1. 配置Maven
<!-- Memcached client -->
<dependency> 
<groupId>com.danga</groupId>
<artifactId>memcached-java-client</artifactId>
<version>2.6.3</version>
</dependency>
2. Properties配置
#######################Memcached配置#######################
#服务器地址
memcached.server=127.0.0.1:11211
#初始化时对每个服务器建立的连接数目
memcached.initConn=20
#每个服务器建立最小的连接数
memcached.minConn=10
#每个服务器建立最大的连接数
memcached.maxConn=50
#自查线程周期进行工作,其每次休眠时间
memcached.maintSleep=3000
#Socket的参数,如果是true在写数据时不缓冲,立即发送出去
memcached.nagle=false
#Socket阻塞读取数据的超时时间
memcached.socketTO=3000
3. Spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util" 
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-3.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- properties config   -->
  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="order" value="1"/>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="locations">
    <list>
<value>classpath:config/memcached.properties</value>
    </list>
    </property>
  </bean>
  
<!-- Memcached配置 -->
  <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"
factory-method="getInstance" init-method="initialize" destroy-method="shutDown">
<property name="servers">
<list>
<value>${memcached.server}</value>
</list>
</property>
<property name="initConn">
<value>${memcached.initConn}</value>
</property>
<property name="minConn">
<value>${memcached.minConn}</value>
</property>
<property name="maxConn">
<value>${memcached.maxConn}</value>
</property>
<property name="maintSleep">
<value>${memcached.maintSleep}</value>
</property>
<property name="nagle">
<value>${memcached.nagle}</value>
</property>
<property name="socketTO">
<value>${memcached.socketTO}</value>
</property>
</bean>
</beans>
4. Memcached工具类
package com.wyy.snail.core.util;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Date;
import org.apache.log4j.Logger;
import com.danga.MemCached.MemCachedClient;
import com.snail.base.utils.DateUtils;
/**
 * 
 * @ClassName: MemcachedUtils
 * @Description: Memcached工具类
 * @author liuyazhuang
 * @date 2015-6-18 下午5:28:08
 * 
 */
public class MemcachedUtils {
private static final Logger logger = Logger.getLogger(MemcachedUtils.class);
private static MemCachedClient cachedClient;
static {
if (cachedClient == null)
cachedClient = new MemCachedClient();
}
private MemcachedUtils() {
}
/**
 * 向缓存添加新的键值对。如果键已经存在,则之前的值将被替换。
 * 
 * @param key
 *      键
 * @param value
 *      值
 * @return
 */
public static boolean set(String key, Object value) {
return setExp(key, value, null);
}
/**
 * 向缓存添加新的键值对。如果键已经存在,则之前的值将被替换。
 * 
 * @param key
 *      键
 * @param value
 *      值
 * @param expire
 *      过期时间 New Date(1000*10):十秒后过期
 * @return
 */
public static boolean set(String key, Object value, Date expire) {
return setExp(key, value, expire);
}
/**
 * 向缓存添加新的键值对。如果键已经存在,则之前的值将被替换。
 * 
 * @param key
 *      键
 * @param value
 *      值
 * @param expire
 *      过期时间 New Date(1000*10):十秒后过期
 * @return
 */
private static boolean setExp(String key, Object value, Date expire) {
boolean flag = false;
try {
flag = cachedClient.set(key, value, expire);
} catch (Exception e) {
// 记录Memcached日志
MemcachedLog.writeLog("Memcached set方法报错,key值:" + key + "\r\n" + exceptionWrite(e));
}
return flag;
}
/**
 * 仅当缓存中不存在键时,add 命令才会向缓存中添加一个键值对。
 * 
 * @param key
 *      键
 * @param value
 *      值
 * @return
 */
public static boolean add(String key, Object value) {
return addExp(key, value, null);
}
/**
 * 仅当缓存中不存在键时,add 命令才会向缓存中添加一个键值对。
 * 
 * @param key
 *      键
 * @param value
 *      值
 * @param expire
 *      过期时间 New Date(1000*10):十秒后过期
 * @return
 */
public static boolean add(String key, Object value, Date expire) {
return addExp(key, value, expire);
}
/**
 * 仅当缓存中不存在键时,add 命令才会向缓存中添加一个键值对。
 * 
 * @param key
 *      键
 * @param value
 *      值
 * @param expire
 *      过期时间 New Date(1000*10):十秒后过期
 * @return
 */
private static boolean addExp(String key, Object value, Date expire) {
boolean flag = false;
try {
flag = cachedClient.add(key, value, expire);
} catch (Exception e) {
// 记录Memcached日志
MemcachedLog.writeLog("Memcached add方法报错,key值:" + key + "\r\n" + exceptionWrite(e));
}
return flag;
}
/**
 * 仅当键已经存在时,replace 命令才会替换缓存中的键。
 * 
 * @param key
 *      键
 * @param value
 *      值
 * @return
 */
public static boolean replace(String key, Object value) {
return replaceExp(key, value, null);
}
/**
 * 仅当键已经存在时,replace 命令才会替换缓存中的键。
 * 
 * @param key
 *      键
 * @param value
 *      值
 * @param expire
 *      过期时间 New Date(1000*10):十秒后过期
 * @return
 */
public static boolean replace(String key, Object value, Date expire) {
return replaceExp(key, value, expire);
}
/**
 * 仅当键已经存在时,replace 命令才会替换缓存中的键。
 * 
 * @param key
 *      键
 * @param value
 *      值
 * @param expire
 *      过期时间 New Date(1000*10):十秒后过期
 * @return
 */
private static boolean replaceExp(String key, Object value, Date expire) {
boolean flag = false;
try {
flag = cachedClient.replace(key, value, expire);
} catch (Exception e) {
MemcachedLog.writeLog("Memcached replace方法报错,key值:" + key + "\r\n" + exceptionWrite(e));
}
return flag;
}
/**
 * get 命令用于检索与之前添加的键值对相关的值。
 * 
 * @param key
 *      键
 * @return
 */
public static Object get(String key) {
Object obj = null;
try {
obj = cachedClient.get(key);
} catch (Exception e) {
MemcachedLog.writeLog("Memcached get方法报错,key值:" + key + "\r\n" + exceptionWrite(e));
}
return obj;
}
/**
 * 删除 memcached 中的任何现有值。
 * 
 * @param key
 *      键
 * @return
 */
public static boolean delete(String key) {
return deleteExp(key, null);
}
/**
 * 删除 memcached 中的任何现有值。
 * 
 * @param key
 *      键
 * @param expire
 *      过期时间 New Date(1000*10):十秒后过期
 * @return
 */
public static boolean delete(String key, Date expire) {
return deleteExp(key, expire);
}
/**
 * 删除 memcached 中的任何现有值。
 * 
 * @param key
 *      键
 * @param expire
 *      过期时间 New Date(1000*10):十秒后过期
 * @return
 */
private static boolean deleteExp(String key, Date expire) {
boolean flag = false;
try {
flag = cachedClient.delete(key, expire);
} catch (Exception e) {
MemcachedLog.writeLog("Memcached delete方法报错,key值:" + key + "\r\n" + exceptionWrite(e));
}
return flag;
}
/**
 * 清理缓存中的所有键/值对
 * 
 * @return
 */
public static boolean flashAll() {
boolean flag = false;
try {
flag = cachedClient.flushAll();
} catch (Exception e) {
MemcachedLog.writeLog("Memcached flashAll方法报错\r\n" + exceptionWrite(e));
}
return flag;
}
/**
 * 返回异常栈信息,String类型
 * 
 * @param e
 * @return
 */
private static String exceptionWrite(Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
pw.flush();
return sw.toString();
}
/**
 * 
 * @ClassName: MemcachedLog
 * @Description: Memcached日志记录
 * @author liuyazhuang
 * @date 2015-6-18 下午5:01:37
 * 
 */
private static class MemcachedLog {
private final static String MEMCACHED_LOG = "D:\\memcached.log";
private final static String LINUX_MEMCACHED_LOG = "/usr/local/logs/memcached.log";
private static FileWriter fileWriter;
private static BufferedWriter logWrite;
// 获取PID,可以找到对应的JVM进程
private final static RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
private final static String PID = runtime.getName();
/**
 * 初始化写入流
 */
static {
try {
String osName = System.getProperty("os.name");
if (osName.indexOf("Windows") == -1) {
fileWriter = new FileWriter(MEMCACHED_LOG, true);
} else {
fileWriter = new FileWriter(LINUX_MEMCACHED_LOG, true);
}
logWrite = new BufferedWriter(fileWriter);
} catch (IOException e) {
logger.error("memcached 日志初始化失败", e);
closeLogStream();
}
}
/**
 * 写入日志信息
 * 
 * @param content
 *      日志内容
 */
public static void writeLog(String content) {
try {
logWrite.write("[" + PID + "] " + "- [" + DateUtils.formateTime(new Date().getTime()) + "]\r\n"
+ content);
logWrite.newLine();
logWrite.flush();
} catch (IOException e) {
logger.error("memcached 写入日志信息失败", e);
}
}
/**
 * 关闭流
 */
private static void closeLogStream() {
try {
fileWriter.close();
logWrite.close();
} catch (IOException e) {
logger.error("memcached 日志对象关闭失败", e);
}
}
}
}
5. 使用工具类操作Memcached
@Test
public void testMemcachedSpring() {
MemcachedUtils.set("aa", "bb", new Date(1000 * 60));
Object obj = MemcachedUtils.get("aa");
System.out.println("***************************");
System.out.println(obj.toString());
}


  
关注下面的标签,发现更多相似文章