Spring集成Memcached三种方式(一)
转载:http://blog.csdn.net/u013725455/article/details/52102170
Memcached Client目前有3种:
Memcached Client for Java
SpyMemcached
XMemcached
这三种Client一直存在各种争议:
Memcached Client for Java 比 SpyMemcached更稳定、更早、更广泛;
SpyMemcached 比 Memcached Client for Java更高效;
XMemcached 比 SpyMemcache并发效果更好。
方式一:Memcached Client for Java
jar包准备:java_memcached-release_2.6.6.jar
memcached.properties配置文件:配置服务器地址,连接数,超时时间等
#######################Memcached配置#######################
#服务器地址
memcached.server1=127.0.0.1
memcached.port1=11211
#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 ##pool.setServers(servers);
##pool.setWeights(weights);
##pool.setHashingAlg(SockIOPool.CONSISTENT_HASH);
##pool.setInitConn(5);
##pool.setMinConn(5);
##pool.setMaxConn(250);
##pool.setMaxIdle(1000 * 60 * 60 * 6);
##pool.setMaintSleep(30);
##pool.setNagle(false);
##pool.setSocketTO(3000);
##pool.setSocketConnectTO(0);
配置Bean
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.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:/com/springmvc/config/memcached.properties</value>-->
<value>/WEB-INF/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">
<!-- ${memcached.server} -->
<list>
<value>${memcached.server1}:${memcached.port1}</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>
将app-context-memcached.xml配置到app-context.xml中
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<import resource="app-context-memcached.xml" /><!--memcached 缓存配置 -->
<!-- <import resource="app-context-xmemcached.xml" /> -->
<!-- <import resource="app-context-spymemcached.xml" /> -->
<!-- @Component and @Resource -->
<bean
class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<!-- 对com包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.springmvc.imooc" />
<!-- 定时器 -->
<!-- <task:annotation-driven /> -->
<!-- mvc -->
<mvc:annotation-driven />
<!-- Aspect -->
<!-- <aop:aspectj-autoproxy /> -->
</beans>
Memcached工具类:
/*
* 文件名:MemcachedUtils.java
* 版权:Copyright 2007-2016 517na Tech. Co. Ltd. All Rights Reserved.
* 描述: MemcachedUtils.java
* 修改人:peiyu
* 修改时间:2016年8月2日
* 修改内容:新增
*/
package com.springmvc.imooc.util; import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date; import com.danga.MemCached.MemCachedClient; /**
* @author peiyu
*/
public final class MemcachedUtils {
/**
* cachedClient.
*/
private static MemCachedClient cachedClient;
static {
if (cachedClient == null) {
cachedClient = new MemCachedClient("memcachedPool");
}
}
/**
* 构造函数.
*/
private MemcachedUtils() {
}
/**
* 向缓存添加新的键值对。如果键已经存在,则之前的值将被替换.
* @param key 键
* @param value 值
* @return boolean
*/
public static boolean set(String key, Object value) {
return setExp(key, value, null);
}
/**
* 向缓存添加新的键值对。如果键已经存在,则之前的值将被替换.
* @param key 键
* @param value 值
* @param expire 过期时间 New Date(1000*10):十秒后过期
* @return boolean
*/
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 boolean
*/
private static boolean setExp(String key, Object value, Date expire) {
boolean flag = false;
try {
flag = cachedClient.set(key, value, expire);
} catch (Exception e) {
MemcachedLog.writeLog("Memcached set方法报错,key值:" + key + "\r\n" + exceptionWrite(e));
}
return flag;
}
/**
* 仅当缓存中不存在键时,add 命令才会向缓存中添加一个键值对.
* @param key 键
* @param value 值
* @return boolean
*/
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 boolean
*/
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 boolean
*/
private static boolean addExp(String key, Object value, Date expire) {
boolean flag = false;
try {
flag = cachedClient.add(key, value, expire);
} catch (Exception e) {
MemcachedLog.writeLog("Memcached add方法报错,key值:" + key + "\r\n" + exceptionWrite(e));
}
return flag;
}
/**
* 仅当键已经存在时,replace 命令才会替换缓存中的键.
* @param key 键
* @param value 值
* @return boolean
*/
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 boolean
*/
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 boolean
*/
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 boolean
*/
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 boolean
*/
public static boolean delete(String key) {
return deleteExp(key, null);
}
/**
* 删除 memcached 中的任何现有值.
* @param key 键
* @param expire 过期时间 New Date(1000*10):十秒后过期
* @return boolean
*/
public static boolean delete(String key, Date expire) {
return deleteExp(key, expire);
}
/**
* 删除 memcached 中的任何现有值.
* @param key 键
* @param expire 过期时间 New Date(1000*10):十秒后过期
* @return boolean
*/
@SuppressWarnings("deprecation")
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 boolean
*/
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 Exception
* @return boolean
*/
private static String exceptionWrite(Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
pw.flush();
return sw.toString();
}
}
测试:
System.out.println(MemcachedUtils.set("aa", "bb", new Date(1000 * 60)));
Object obj = MemcachedUtils.get("aa");
System.out.println("***************************");
System.out.println(obj.toString());
Spring集成Memcached三种方式(一)的更多相关文章
- spring配置datasource三种方式
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp34 spring配置datasource三种方式 1.使用org.spri ...
- Spring依赖注入三种方式详解
在讲解Spring依赖注入之前的准备工作: 下载包含Spring的工具jar包的压缩包 解压缩下载下来的Spring压缩包文件 解压缩之后我们会看到libs文件夹下有许多jar包,而我们只需要其中的c ...
- spring实例化bean三种方式
我看了这篇博文<https://www.cnblogs.com/zhanglei93/p/6221546.html>,以及自己实践总结了关于spring实例化bean对象的3种方式. 一. ...
- spring配置datasource三种方式及具体信息
1.使用org.springframework.jdbc.datasource.DriverManagerDataSource说明:DriverManagerDataSource建立连接是只要有连接就 ...
- Spring笔记03(Spring创建对象的三种方式)
1.创建对象的三种方式和bean的生命周期的验证: Animal接口代码: package cn.pb.dao; /** * 动物接口 */ public interface Animal { //吃 ...
- Spring创建对象的三种方式以及创建时间
创建对象的三种方式: 1.采用默认的构造函数创建 2.采用静态工厂方法 1.写一个静态工厂方法类 public class HelloWorldFactory { public static Hell ...
- spring配置datasource三种方式 数据库连接池
尊重原创(原文链接):http://blog.csdn.net/kunkun378263/article/details/8506355 1.使用org.springframework.jdbc.da ...
- 创建 Spring容器的三种方式
一.src路径下打包完在war包的classes层级下 1.Spring容器创建的三种方式 创建Bean容器之后创建对象: 其中第三种使用的是BeanFactory对象 2.spring通过配置文件用 ...
- web.xml中配置spring.xml的三种方式
我们知道spring在web.xml中可以有三种方式来配置其xml路径:org.springframework.web.servlet.DispatcherServletorg.springframe ...
随机推荐
- 使用RXTX获取电脑串口
RXTX是javacomm串口通信的一个扩展 RXTX开发所需文件的下载地址:http://rxtx.qbang.org/wiki/index.php/Download 解压之后可以看到支持各个平台的 ...
- 学习笔记-Java编程思想
2016-01-03 Swith(整数选择因子):必须是int或char这样的整数值. Java中不包含goto语句,但是可以通过标识符实现类似的控制.
- IDEA创建maven项目使用命令打包遇到的问题及解决方法
maven项目命令打包springboot项目 首先打开tomcat命令工具(如图所示):
- WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED解决方法
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ WARNING: REMOTE HOST IDENTIFICATION ...
- JKS TO PEM
tomcat 的ssl 会使用到jks,而haproxy的ssl(非tcp代理方式)会使用到pem 如果从tomcat的ssl需要迁移到haproxy的ssl,就需要从jks中读取相关信息生成pem文 ...
- 百度地图api 标注的图标不显示问题
图中郑州PPT设计制作中心前面应该有一个小的标,但是死活就是不显示. 经过百度搜索和测试,终于解决.应该是页面定义的CSS和百度的冲突了,解决办法如下: 在当前页面中,加入 <style typ ...
- 基于Bootstrap、Jquery的自适应导航栏
css代码: .kch_nav{width: 100%; position: fixed;z-index: 999;top:0;left: 0;right: 0;height: 69px;backgr ...
- HDU2896 病毒侵袭
题目大意:给出若干病毒的特征码,不超过500个.每个病毒的特征码长度在20~200之间.现在有若干网站的源代码,要检测网站的源代码中是否包含病毒.网站的个数不超过1000个,每个网站的源代码长度在70 ...
- 单点登录SSO
转载自 http://www.blogjava.net/xcp/archive/2010/04/13/318125.html 摘要:单点登录(SSO)的技术被越来越广泛地运用到各个领域的软件系统当 ...
- vue.js组件
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...