Memcached在SpringMVC上的使用
1、memcached介绍
memcached是一款由Danga Interactive公司开发的高效的分布式缓存服务器,主要是用于解决数据量庞大、访问集中的Web应用出现的数据库服务器负担过重,数据库相应延迟导致的网站显示延迟的情况,通过它可以缓存数据库访问结果,减少应用直接访问数据库的次数,提高系统相应速度。
2、在SpringMVC项目中使用memcached
1)准备工作
下载安装memcached服务器,安装过程Linux参考http://www.runoob.com/memcached/memcached-install.html,Window参考http://www.runoob.com/memcached/window-install-memcached.html
2)创建SpringMVC项目
3)添加maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>groupId</groupId>
<artifactId>spring-memcached</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java-version>1.8</java-version>
<spring-version>4.1.5.RELEASE</spring-version>
<log4j-version>2.8</log4j-version>
</properties> <repositories>
<repository>
<id>aliyun</id>
<name>aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
</repositories> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency> <!-- json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.12</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version>
</dependency>
<!-- end json --> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency> <dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>2.2.0</version>
</dependency> </dependencies> <build>
<finalName>spring-memcached</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
4)applicationContext.xml配置
memcached.properties
memcached.connectionPoolSize=10
memcached.failureMode=true #server1
server1.memcached.host:127.0.0.1
server1.memcached.port:11211
server1.memcached.weight=4
applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byName">
<description>Spring公共配置</description>
<context:annotation-config/> <context:component-scan base-package="com.weidai.memcached">
<!-- 在装载spring mvc 时会加载这些Controller 因此在这边排除加载 如果不排除会造成事务失效 -->
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan> <!-- 载入类路径下的properties属性文件 -->
<context:property-placeholder location="classpath*:config/*.properties" />
<bean id="memcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder"
p:connectionPoolSize="${memcached.connectionPoolSize}" p:failureMode="${memcached.failureMode}" >
<constructor-arg>
<list>
<bean class="java.net.InetSocketAddress">
<constructor-arg>
<value>${server1.memcached.host}</value>
</constructor-arg>
<constructor-arg>
<value>${server1.memcached.port}</value>
</constructor-arg>
</bean>
</list>
</constructor-arg>
<constructor-arg>
<list>
<value>${server1.memcached.weight}</value>
</list>
</constructor-arg>
<property name="commandFactory">
<bean class="net.rubyeye.xmemcached.command.TextCommandFactory" />
</property>
<property name="sessionLocator">
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator" />
</property> <property name="transcoder">
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
</property>
</bean> <bean id="memcachedClient" factory-bean="memcachedClientBuilder" factory-method="build" destroy-method="shutdown"> </bean>
</beans>
5)memcached客户端API选择xmemcached示例如下
package com.weidai.memcached.cache; import net.rubyeye.xmemcached.MemcachedClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public abstract class MemcachedBasis<T> {
@Autowired
protected MemcachedClient memcachedClient;
/** 数据失效时间 **/
protected int EXPIRE_TIME = 3600*24;
/** 基础数据失效时间 **/
protected int DATA_EXPIRE_TIME = this.EXPIRE_TIME*7; protected static final String PREFIX = "SPRINGMEMCACHEDDEMO"; protected static final String SEPERATOR = "_"; protected abstract String getDataId();
public abstract String generateKey(String type, String ... params); public Boolean set(String key, T model){
Boolean result = Boolean.FALSE;
try {
result = memcachedClient.set(key, this.EXPIRE_TIME, model);
}catch(Exception ex){
}
return result;
} public<T> T get(String key){
T model = null;
try {
model = memcachedClient.get(key);
}catch(Exception ex){
}
return model;
} }
Memcached在SpringMVC上的使用的更多相关文章
- springmvc上传文件,抄别人的
SpringMVC中的文件上传 分类: SpringMVC 2012-05-17 12:55 26426人阅读 评论(13) 收藏 举报 stringuserinputclassencoding 这是 ...
- memcached在windows7上的安装问题
memcached在windows7上的安装问题 错误: 通过cmd命令行进入到C:\memcached(下载后的解压目录) 运行 memcached.exe -d install 报错“ f ...
- SpringMVC上传下载
springmvc上传和下载功能 写在一个简单的示例在线基准码 1.导入的必要性jar包:ant.jar.commons-fileupload.jar.connom-io.jar. 当然spring ...
- 2. SpringMVC 上传文件操作
1.创建java web项目:SpringMVCUploadDownFile 2.在项目的WebRoot下的WEB-INF的lib包下添加如下jar文件 com.springsource.com.mc ...
- SpringMVC上传压缩文件,解压文件,并检测上传文件中是否有index.html
SpringMVC上传压缩文件,解压文件,并检测上传文件中是否有index.html 说明: 1.环境:SpringMVC+Spring+Tomcat7+JDK1.7 2.支持 zip和rar格式的压 ...
- SpringMVC 上传下载 异常处理
SpringMVC 上传下载 异常处理 上一章节对SpringMVC的表单验证进行了详细的介绍,本章节介绍SpringMVC文件的上传和下载(重点),国际化以及异常处理问题.这也是SpringMVC系 ...
- SpringMVC上传文件大小的设置
在spring-mvc.xml(springmvc的配置文件)里: <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> <bean id ...
- 使用springMVC上传文件
control层实现功能: @RequestMapping(value="upload2") public String upLoad2(HttpServletRequest re ...
- SpringMVC上传文件(图片)并保存到本地
SpringMVC上传文件(图片)并保存到本地 小记一波~ 基本的MVC配置就不展示了,这里给出核心代码 在spring-mvc的配置文件中写入如下配置 <bean id="multi ...
随机推荐
- Python工资高还是Java?
说起来,随着人工智能和大数据逐渐进入人们的眼中,越来越多的人看到互联网未来大好发展趋势,而想要学习一门技术来进入其中,以期分一杯羹.但是,作为人工智能和大数据的重要编程语言,Python和Java,该 ...
- java.sql.SQLException: Access denied for user 'scott'@'localhost' (using password: YES)
今天用eclipse连接一下数据库,出现此异常. java.sql.SQLException: Access denied for user 'scott'@'localhost' (using pa ...
- VMware中为Linux安装vm-tools
1.虚拟机中选择安装VMware-tools,或者重新安装 2.在/mnt目录下建立cdrom文件夹 mkdir /mnt.cdrom 3.把/dev/cdrom光驱挂载到刚才建的文件夹上 mount ...
- iOS项目之获取WebView的高度
获取高度值的方法: - (void)webViewDidFinishLoad:(UIWebView *)webView { // 获取webView的高度 CGFloat webViewHeight ...
- 【一些容易忘记的node的npm命令】【收集】
更新npm到最新版本 npm update -g npm 安装依赖包时命令的一些区别 npm install xxx -g //(全局安装) npm install xxx --save-dev // ...
- yum无法安装的pdksh
yum无法安装的pdksh,本地pdksh-5.2.14-37.el5_8.1.x86_64.rar,点击下载.
- MySQL基础和JDBC
第一章 命令行工具 mysqladmin:MySQL服务器管理工具 mysql:MySQL客服端链接工具 mysqldump 演示链接到服务器host=127.0.0.1,用户名为root,密码为空 ...
- 使用Flask部署机器学习模型
Introduction A lot of Machine Learning (ML) projects, amateur and professional, start with an aplomb ...
- git 管理和存储二进制大文件
git 管理二进制文件 本文档将逐步带你体验 git 的大文件管理方式. 环境: windows10 64位 cmd git版本: git version 2.18.0.windows.1 创建到推送 ...
- 【转载】URL编码与两次encodeURI
当使用地址栏提交查询参数时,如果不编码,非英文字符会按照操作系统的字符集进行编码提交到服务器,服务器会按照配置的字符集进行解码,所以如果两者不一致就会导致乱码. encodeURI函数采用UTF-8对 ...