package net.csdn.blog.chaijunkun.service.impl;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.ResourceBundle;
import java.util.concurrent.ExecutionException;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import net.csdn.blog.chaijunkun.service.MemcacheService;
import net.spy.memcached.AddrUtil;
import net.spy.memcached.ConnectionFactoryBuilder;
import net.spy.memcached.ConnectionFactoryBuilder.Locator;
import net.spy.memcached.DefaultHashAlgorithm;
import net.spy.memcached.FailureMode;
import net.spy.memcached.MemcachedClient;
import net.spy.memcached.ConnectionFactoryBuilder.Protocol;
import net.spy.memcached.internal.OperationFuture;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
@Service
public class MemcacheServiceImpl implements MemcacheService {
private static Logger logger= Logger.getLogger(MemcacheServiceImpl.class);
/**
* 指定配置文件
*/
private String CONFIG_FILE = "memcached";
/**
* 读取配置文件
*/
private ResourceBundle rb= ResourceBundle.getBundle(CONFIG_FILE);
/**
* memcached客户端实例
*/
private MemcachedClient mcc = null;
/**
* 缓存服务器列表 空格分隔 host1:port1 host2:port2
*/
private String servers= null;
/**
* I/O线程是否驻留 默认开启
*/
private boolean isDaemon;
/**
* 操作超时时间 单位:毫秒
*/
private long opTimeout;
/**
* 抛出超时异常的门限值 单位:毫秒
*/
private int timeoutExceptionThreshold;
/**
* 重试连接缓存服务器延时时间 单位:毫秒
*/
private long maxReconnectDelay;
/**
* 队列最大阻塞时间 单位:毫秒
*/
private long opQueueMaxBlockTime;
/**
* 本系统中默认的缓存时间 单位:秒 最长30天
*/
private int defaultExpSec;
/**
* 加载配置信息
* @throws IOException
*/
@PostConstruct
public void init(){
//Serevers 服务器列表
//服务器列表以空格分隔
//host1:port1 host2:port2 ....
this.servers= rb.getString("servers");
//I/O线程是否驻留
//启动驻留后该线程一次初始化 服务于所有数据传输
//不启动驻留 仅当有请求时建立线程 空闲超时后会自动退出
this.isDaemon= getBoolParam("isDaemon", true);
//操作超时时间
this.opTimeout= getIntParam("opTimeout");
this.timeoutExceptionThreshold= getIntParam("timeoutExceptionThreshold");
this.maxReconnectDelay= getLongParam("timeoutExceptionThreshold", 15000L);
this.opQueueMaxBlockTime= getLongParam("opQueueMaxBlockTime", 1000L);
this.defaultExpSec= getIntParam("defaultExpSec");
if (this.defaultExpSec> 2592000){
logger.warn("默认缓存有效期超出30天,恢复默认设置");
this.defaultExpSec= 2592000;
}
logger.info("缓存配置文件加载完成");
List<InetSocketAddress> serverList= AddrUtil.getAddresses(servers);
try{
ConnectionFactoryBuilder connFactBuilder= new ConnectionFactoryBuilder();
//使用二进制协议
connFactBuilder.setProtocol(Protocol.TEXT);
//当葱缓存中获取数据发生错误时 终止获取
connFactBuilder.setFailureMode(FailureMode.Cancel);
//负载均衡策略 一致性策略
connFactBuilder.setLocatorType(Locator.CONSISTENT);
//采用的哈希算法Ketama算法
connFactBuilder.setHashAlg(DefaultHashAlgorithm.KETAMA_HASH);
connFactBuilder.setDaemon(this.isDaemon);
connFactBuilder.setOpTimeout(this.opTimeout);
connFactBuilder.setTimeoutExceptionThreshold(this.timeoutExceptionThreshold);
connFactBuilder.setMaxReconnectDelay(this.maxReconnectDelay);
connFactBuilder.setOpQueueMaxBlockTime(this.opQueueMaxBlockTime);
//按照设定生成Memcache客户端
mcc= new MemcachedClient(connFactBuilder.build(), serverList);
}catch(Exception e){
logger.error("配置缓存服务器出错,请检查", e);
}
}
@PreDestroy
public void destory(){
mcc.shutdown();
logger.info("缓存连接已关闭");
}
/**
* 从配置文件中获取int型配置值 可设置默认值
* @param paramKey 配置键
* @param defaultVal 默认值
* @return int 返回的配置值
*/
private int getIntParam(String paramKey, int defaultVal){
String value= null;
try{
value= rb.getString(paramKey);
if (value==null){
return defaultVal;
}else{
return Integer.parseInt(value);
}
}catch (Exception e) {
return defaultVal;
}
}
/**
* 从配置文件中获取int型配置值 默认值为0
* @param paramKey 配置键
* @return int 返回的配置值
*/
private int getIntParam(String paramKey){
return getIntParam(paramKey, 0);
}
/**
* 从配置文件中获取long型配置值 可设置默认值
* @param paramKey 配置键
* @param defaultVal 默认值
* @return long 返回的配置值
*/
private long getLongParam(String paramKey, long defaultVal){
String value= null;
try{
value= rb.getString(paramKey);
if (value==null){
return defaultVal;
}else{
return Long.parseLong(value);
}
}catch (Exception e) {
return defaultVal;
}
}
/**
* 从配置文件中获取boolean型配置值 可设置默认值
* @param paramKey 配置键
* @param defaultVal 默认值
* @return boolean 返回的配置值
*/
private boolean getBoolParam(String paramKey, boolean defaultVal){
String value= null;
try{
value= rb.getString(paramKey);
if (value==null){
return defaultVal;
}else{
return Boolean.parseBoolean(value);
}
}catch (Exception e) {
return defaultVal;
}
}
/**
* 构造Memcache服务并设置缓存服务器连接池参数
*/
public MemcacheServiceImpl() {
logger.info("正在构建Memcache缓存操作服务");
}
public boolean add(String key, Object value) {
return this.add(key, value, defaultExpSec);
}
public boolean add(String key, Object value, int aliveSeconds) {
try{
return mcc.set(key, aliveSeconds, value).get();
}catch(InterruptedException intEx){
logger.error(String.format("缓存%s时被中断操作 ", key), intEx);
return false;
}catch (ExecutionException exeEx) {
logger.error(String.format("缓存%s时执行异常,请检查缓存服务器", key));
return false;
}
}
public boolean replace(String key, Object value) {
return this.replace(key, value, defaultExpSec);
}
public boolean replace(String key, Object value, int aliveSeconds) {
try{
return mcc.replace(key, aliveSeconds, value).get();
}catch(InterruptedException intEx){
logger.error(String.format("替换缓存%s时被中断操作 ", key), intEx);
return false;
}catch (ExecutionException exeEx) {
logger.error(String.format("替换缓存%s时执行异常,请检查缓存服务器", key));
return false;
}
}
public Object get(String key) {
try{
return mcc.get(key);
}catch(Exception e){
return null;
}
}
public <T> T get(String key, Class<T> clazz) {
try{
return clazz.cast(this.get(key));
}catch (ClassCastException exp) {
return null;
}
}
public boolean remove(String key){
OperationFuture<Boolean> resultFuture= mcc.delete(key);
try{
return resultFuture.get();
}catch(InterruptedException intEx){
logger.error(String.format("删除缓存%s时被中断操作 ", key), intEx);
return false;
}catch (ExecutionException exeEx) {
logger.error(String.format("删除缓存%s时执行异常,请检查缓存服务器", key));
return false;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
.zip (65个子文件)
缓存验证码
memcached
memcached.exe 84KB
memcached-1.2.6.bat 52B
CacheCaptchaDemo
WebContent
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 2KB
pages
verify.jsp 438B
classes
spring-servlet.xml 2KB
memcached.properties 2KB
log4j.xml 1017B
net
csdn
blog
chaijunkun
misc
DefaultRipple.class 590B
MyWordRenderer.class 4KB
StringUnicodeSerializer.class 3KB
controller
CaptchaController.class 3KB
service
MemcacheService.class 529B
CaptchaService.class 442B
impl
MemcacheServiceImpl.class 8KB
CaptchaServiceImpl.class 6KB
util
ObjectUtil.class 1KB
ResponseUtil.class 4KB
StringUtil.class 665B
JSONUtil.class 5KB
XXTEAUtil.class 3KB
log4j.dtd 7KB
applicationContext.xml 2KB
lib
spring-webmvc-3.2.1.RELEASE.jar 625KB
spring-web-3.2.1.RELEASE.jar 610KB
standard.jar 384KB
spring-beans-3.2.1.RELEASE.jar 593KB
commons-logging-1.1.1.jar 59KB
spring-context-3.2.1.RELEASE.jar 838KB
jstl.jar 20KB
commons-codec-1.6.jar 227KB
spymemcached-2.8.4.jar 392KB
spring-expression-3.2.1.RELEASE.jar 191KB
jackson-all-1.9.11.jar 1.07MB
spring-core-3.2.1.RELEASE.jar 843KB
log4j-1.2.17.jar 478KB
kaptcha-2.3.2.jar 421KB
js
jquery-1.8.2.min.js 91KB
index.jsp 1KB
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 337B
org.eclipse.wst.common.component 582B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 503B
org.eclipse.jdt.core.prefs 364B
.project 1KB
resource
spring-servlet.xml 2KB
memcached.properties 2KB
log4j.xml 1017B
log4j.dtd 7KB
applicationContext.xml 2KB
.classpath 804B
java
net
csdn
blog
chaijunkun
misc
MyWordRenderer.java 3KB
DefaultRipple.java 404B
StringUnicodeSerializer.java 2KB
controller
CaptchaController.java 3KB
service
MemcacheService.java 466B
CaptchaService.java 760B
impl
MemcacheServiceImpl.java 7KB
CaptchaServiceImpl.java 4KB
util
StringUtil.java 500B
XXTEAUtil.java 4KB
JSONUtil.java 4KB
ObjectUtil.java 773B
ResponseUtil.java 3KB
共 65 条
- 1
Jack_Chai
- 粉丝: 563
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页