一、背景

销售CRM(项目A)将负责管理项目信息系统(项目B)的支付与权限

上级要求为避免频繁调用CRM接口,中间放一级缓存,但要做到缓存中保证最新数据

因项目B已使用memcache作缓存,所以决定使用memcache

二、设计

1、统一系统交互方式,确保系统与系统间调用只有一个接口,接口参数为(String action, String data),以action区别调用是什么方法

2、封装简单的memcache工具类,可执行简单的增删改查

3、memcache单独一个日志文件,方便查找原因

4、项目A,采用AOP拦截,在对数据进行增删改时,对缓存中数据进行同步操作

5、为项目B再次封装memcache工具类的查询方法,确保在缓存查不到数据时,调用项目A的接口,将数据放于缓存

本文主要讲解memcache与spring的集成,这些设计不在讲解范围内(貌似讲了一堆废话,哈哈)

三、memcache与Spring集成

memcache决定使用memcached client java客户端,原因是其使用较为广泛,单纯求稳妥

在项目/src/main/resources/property下有三个配置文件,分别对应开发环境、预上线、正式线

修改配置文件,增加

#######################设置Memcached服务器参数#######################
#设置服务器地址
memcached.server=172.16.231.230:11211
#容错
memcached.failOver=true
#设置初始连接数
memcached.initConn=20
#设置最小连接数
memcached.minConn=10
#设置最大连接数
memcached.maxConn=250
#设置连接池维护线程的睡眠时间
memcached.maintSleep=3000
#设置是否使用Nagle算法(Socket的参数),如果是true在写数据时不缓冲,立即发送出去
memcached.nagle=false
#设置socket的读取等待超时时间
memcached.socketTO=3000
#设置连接心跳监测开关
memcached.aliveCheck=true
#######################设置Memcached服务器参数#######################

因预上线、与线上环境为封闭环境,memcache服务器地址在不同配置文件中各有不同

可直接在spring的配置文件中增加memcache的配置,在这选择在/src/main/resources中增加memcached-context.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://code.alibabatech.com/schema/dubbo    http://code.alibabatech.com/schema/dubbo/dubbo.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/task  http://www.springframework.org/schema/task/spring-task-3.1.xsd"><!-- 加载初始化配置文件 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:property/init.properties</value></list></property></bean>--><!-- Memcached配置 -->  <bean id="memcachedPool" class="com.whalin.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>

因在spring配置文件中已加载了初始化配置文件,所以在这不需要再加载,如果没有加载过,应放开注释

修改web.xml,增加memcached-content.xml

<!-- spring配置文件 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:applicationContext.xml,classpath*:memcached-content.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><listener><listener-class>com.glodon.gcxx.common.ServletContextInit</listener-class></listener>

至此,配完了。。。。 是的,配完了,下面是工具类的代码

package com.glodon.gcxx.Interaction.memcached;import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;import com.whalin.MemCached.MemCachedClient;public class MemcachedUtil {private static final Logger logger = Logger.getLogger(MemcachedUtil.class);private static MemCachedClient cachedClient;static {if (cachedClient == null)cachedClient = new MemCachedClient();}/*** @Description 添加新的键值对,默认最少使用算法淘汰。如果键已经存在,则之前的值将被替换。* @author liuy-8* @date 2015年5月29日 下午2:09:17 * @param key 键* @param value 值* @return 是否成功*/public static boolean set(String key, Object value) {return setExp(key, value, null);}/*** @Description 添加新的键值对。如果键已经存在,则之前的值将被替换。* @author liuy-8* @date 2015年5月29日 下午2:10:08 * @param key 键* @param value 值* @param time 过期时间,单位ms* @return 是否成功*/public static boolean set(String key, Object value, int time) {return setExp(key, value, new Date(time));}/*** @Description 添加新的键值对。如果键已经存在,则之前的值将被替换。* @author liuy-8* @date 2015年5月29日 下午2:13:18 * @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日志logger.error("Memcached set方法报错,key值:" + key + "\r\n" + exceptionWrite(e));}return flag;}/*** @Description 仅当缓存中不存在键时,add 命令才会向缓存中添加一个键值对,默认最少使用算法淘汰* @author liuy-8* @date 2015年5月29日 下午2:13:50 * @param key 键* @param value 值* @return 是否成功*/public static boolean add(String key, Object value) {return addExp(key, value, null);}/*** @Description 仅当缓存中不存在键时,add 命令才会向缓存中添加一个键值对* @author liuy-8* @date 2015年5月29日 下午2:27:40 * @param key 键* @param value 值* @param time 过期时间,单位毫秒* @return 是否成功*/public static boolean add(String key, Object value, int time) {return addExp(key, value, new Date(time));}/*** @Description 仅当缓存中不存在键时,add 命令才会向缓存中添加一个键值对* @author liuy-8* @date 2015年5月29日 下午2:30:29 * @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日志logger.error("Memcached add方法报错,key值:" + key + "\r\n" + exceptionWrite(e));}return flag;}/*** @Description 仅当键已经存在时,replace 命令才会替换缓存中的键,默认最少使用算法淘汰* @author liuy-8* @date 2015年5月29日 下午2:42:22 * @param key 键* @param value 值* @return 是否成功*/public static boolean replace(String key, Object value) {return replaceExp(key, value, null);}/*** @Description 仅当键已经存在时,replace 命令才会替换缓存中的键。* @author liuy-8* @date 2015年5月29日 下午3:07:51 * @param key 键* @param value 值* @param time 过期时间,单位毫秒* @return 是否成功*/public static boolean replace(String key, Object value, int time) {return replaceExp(key, value, new Date(time));}/*** @Description 仅当键已经存在时,replace 命令才会替换缓存中的键。* @author liuy-8* @date 2015年5月29日 下午3:09:22 * @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) {logger.error("Memcached replace方法报错,key值:" + key + "\r\n" + exceptionWrite(e));}return flag;}/*** @Description 命令用于检索与之前添加的键值对相关的值* @author liuy-8* @date 2015年5月29日 下午3:09:45 * @param key 键* @return 得到与键对应的值*/public static Object get(String key) {Object obj = null;try {obj = cachedClient.get(key);} catch (Exception e) {logger.error("Memcached get方法报错,key值:" + key + "\r\n" + exceptionWrite(e));}return obj;}/*** @Description 删除 memcached 中的任何现有值。* @author liuy-8* @date 2015年5月29日 下午3:10:57 * @param key 键* @return 是否成功*/public static boolean delete(String key) {return deleteExp(key, null);}/*** @Description 删除 memcached 中的任何现有值。* @author liuy-8* @date 2015年5月29日 下午3:33:39 * @param key 键* @param time 阻塞时间,单位ms,禁止使用同样的键保存新数据,set方法除外* @return 是否成功*/public static boolean delete(String key, int time) {return deleteExp(key, new Date(time));}/*** @Description 方法描述说明* @author liuy-8* @date 2015年5月29日 下午3:37:52 * @param key 键* @param expire 阻塞时间,单位ms,禁止使用同样的键保存新数据,set方法除外* @return 是否成功*/private static boolean deleteExp(String key, Date expire) {boolean flag = false;try {flag = cachedClient.delete(key, expire);} catch (Exception e) {logger.error("Memcached delete方法报错,key值:" + key + "\r\n" + exceptionWrite(e));}return flag;}/*** @Description 清理缓存中的所有键/值对* @author liuy-8* @date 2015年5月29日 下午3:38:26 * @return 是否成功*/public static boolean flushAll() {boolean flag = false;try {flag = cachedClient.flushAll();} catch (Exception e) {logger.error("Memcached flashAll方法报错\r\n" + exceptionWrite(e));}return flag;}/*** @Description 返回异常栈信息,String类型* @author liuy-8* @date 2015年5月29日 下午3:38:52 * @param e 异常* @return 异常信息*/private static String exceptionWrite(Exception e) {StringWriter sw = null;PrintWriter pw = null;String errorInfo = "";try {sw = new StringWriter();pw = new PrintWriter(sw);e.printStackTrace(pw);pw.flush();errorInfo = sw.toString();} finally {//关闭PrintWriterif(pw != null)pw.close();//关闭StringWriterif(sw != null){try {sw.close();} catch (IOException e1) {e1.printStackTrace();}}}return errorInfo;}}

转载于:https://www.cnblogs.com/xjyh/p/4545856.html

memcached与spring集成相关推荐

  1. 从零开始学 Java - Spring 集成 Memcached 缓存配置(二)

    Memcached 客户端选择 上一篇文章 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)中我们讲到这篇要谈客户端的选择,在 Java 中一般常用的有三个: Memc ...

  2. Spring集成Memcached三种方式(一)

    Spring集成Memcached三种方式(一) 转载:http://blog.csdn.net/u013725455/article/details/52102170 Memcached Clien ...

  3. Spring集成spymemcached

    Spring集成spymemcached Memcached的安装部署我就不介绍了! 首先下载spymemcached,下载地址: jar:https://spymemcached.googlecod ...

  4. Spring集成Shiro框架实战

    文章目录 一:什么是Shiro框架 二:Shiro框架简介 1.Shiro基础功能点介绍 2.Shiro的工作原理 3.Shiro的内部工作结构 4.Shiro的身份认证流程 三:Spring集成Sh ...

  5. spring集成shiro详解

    最近项目中要用到shiro作为权限管理,以前都是用自定义的或者spring security,所以就开始看了一些网上的文章,但是感觉都写得很零散.而且大多数都只是给了几行代码,我们得项目相对比较复杂, ...

  6. Spring集成Redis方案(spring-data-redis)(基于Jedis的单机模式)(待实践)

    说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...

  7. windows api中文文档_Web服务开发:Spring集成Swagger,3步自动生成API文档

    目录: 1,Spring Boot集成Swagger 2,Swagger接口文档页面 3,常见问题和解决方法 在Sping开发REST接口服务时,API文档是不可缺少的一个重要部分.Swagger框架 ...

  8. Liferay7 BPM门户开发之5: Activiti和Spring集成

    参考文档: https://github.com/jbarrez/spring-boot-with-activiti-example https://github.com/sxyx2008/sprin ...

  9. spring集成xmemcached

    2019独角兽企业重金招聘Python工程师标准>>> spring集成xmemcached <dependency><groupId>com.googlec ...

最新文章

  1. java基础 4 继承(1)访问权限与作用域
  2. 两个月的打卡活动圆满结束,今天公布兑奖详情
  3. Hibernate:组合模式解决树的映射
  4. 02-1.CSS边框,边界,布局相关笔记
  5. 人力资源SaaS软件“乐才Joy HR”获数百万元战略融资
  6. python 文件名随自变量变化_Python 求解自行车前后轮轨迹问题
  7. 最不适合做数据分析的6种性格,看看你占了几个?
  8. python selenium --鼠标事件
  9. CRF模型——打通crf模型的任督二脉(一)
  10. 联想智能云教室安win7_联想智能云教室解决方案-联想商用.PDF
  11. win10开始菜单 此计算机,如何将win10电脑中的开始菜单设置成智能版
  12. 在Js和Java自动生成账号的方法
  13. 【笔记】封神榜游戏设计VR
  14. android旋转的列表,Android利用layer-list实现ProgressBar顺时针及逆时针旋转
  15. python金融量化书籍_超强干货 | Python金融数据量化分析教程+机器学习电子书
  16. Windows系统错误提示:缺少词库文件winabc.cwd缺少基础表文件winabc.ovl
  17. 关于pytorch官网教程中的What is torch.nn really?(三)
  18. C语言>>百钱百鸡问题。鸡翁一,值钱五;鸡母一,值钱三;鸡雏三,值钱一;百钱买百鸡,翁、母、雏各几何 ?
  19. Android之MVVM简单例子
  20. SwitchyOmega使用

热门文章

  1. MUI+H5手机上传照片 支持多图片上传和拍照上传
  2. C#教程01:关于C#
  3. Matlab直线一级倒立摆控制方法研究
  4. centos安装mysql5.7.19_Linux下Centos7安装Mysql5.7.19的详细教程
  5. ajax 在新选卡打开,JavaScript在新窗口中打开,而不是选项卡
  6. 小鑫の日常系列故事(十)——排名次_JAVA
  7. Java中利用socket实现简单的服务端与客户端的通信(基础级)
  8. 《设计模式》一书PPT浏览及下载地址
  9. Java深度历险(二)——Java类的加载、链接和初始化
  10. Http Server : 一个差生的逆袭