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;
} }