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上的使用的更多相关文章

  1. springmvc上传文件,抄别人的

    SpringMVC中的文件上传 分类: SpringMVC 2012-05-17 12:55 26426人阅读 评论(13) 收藏 举报 stringuserinputclassencoding 这是 ...

  2. memcached在windows7上的安装问题

    memcached在windows7上的安装问题   错误: 通过cmd命令行进入到C:\memcached(下载后的解压目录) 运行 memcached.exe -d install   报错“ f ...

  3. SpringMVC上传下载

    springmvc上传和下载功能 写在一个简单的示例在线基准码 1.导入的必要性jar包:ant.jar.commons-fileupload.jar.connom-io.jar. 当然spring ...

  4. 2. SpringMVC 上传文件操作

    1.创建java web项目:SpringMVCUploadDownFile 2.在项目的WebRoot下的WEB-INF的lib包下添加如下jar文件 com.springsource.com.mc ...

  5. SpringMVC上传压缩文件,解压文件,并检测上传文件中是否有index.html

    SpringMVC上传压缩文件,解压文件,并检测上传文件中是否有index.html 说明: 1.环境:SpringMVC+Spring+Tomcat7+JDK1.7 2.支持 zip和rar格式的压 ...

  6. SpringMVC 上传下载 异常处理

    SpringMVC 上传下载 异常处理 上一章节对SpringMVC的表单验证进行了详细的介绍,本章节介绍SpringMVC文件的上传和下载(重点),国际化以及异常处理问题.这也是SpringMVC系 ...

  7. SpringMVC上传文件大小的设置

    在spring-mvc.xml(springmvc的配置文件)里: <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> <bean id ...

  8. 使用springMVC上传文件

    control层实现功能: @RequestMapping(value="upload2") public String upLoad2(HttpServletRequest re ...

  9. SpringMVC上传文件(图片)并保存到本地

    SpringMVC上传文件(图片)并保存到本地 小记一波~ 基本的MVC配置就不展示了,这里给出核心代码 在spring-mvc的配置文件中写入如下配置 <bean id="multi ...

随机推荐

  1. 使用Python在自己博客上进行自动翻页

    先上一张代码及代码运行后的输出结果的图! 下面上代码: # coding=utf-8 import os import time from selenium import webdriver #打开火 ...

  2. TCP之种种连接异常

    1. connect出错: (1) 若TCP客户端没有收到syn分节的响应,则返回ETIMEOUT错误:调用connect函数时,内核发送一个syn,若无响应则等待6s后再发送一个,若仍然无响应则等待 ...

  3. mysql的并发控制

    并发即指在同一时刻,多个操作并行执行.MySQL对并发的处理主要应用了两种机制——是"锁"和"多版本控制". 1.并发控制 MySQL提供两个级别的并发控制:服 ...

  4. Spring错误——Spring xml注释——org.xml.sax.SAXParseException; lineNumber: 24; columnNumber: 10; cvc-complex-type.2.3: 元素 'beans' 必须不含字符 [子级], 因为该类型的内容类型为“仅元素”。

    背景:配置spring xml,注释xml中文件元素 错误: Caused by: org.xml.sax.SAXParseException; lineNumber: 24; columnNumbe ...

  5. adb shell 命令之----pm

    常用的用法: 查看已经安装的包: pm list packages 查看已经安装的包以及apk路径(-3:只看第三方应用: -s:只看系统应用) -f: see their associated fi ...

  6. 异常:unity3d ArgumentException: The Assembly System.Configuration is referenced by System.Data.

    异常:ArgumentException: The Assembly System.Configuration is referenced by System.Data. But the dll is ...

  7. JavaScript形而上的单例模式

    什么是单例模式? 单例模式是指,类多次实例化返回的对象是同一个. 反例 var tt = function(name){ this.name = name; }; var t1 = new tt('t ...

  8. JDK下载-安装-配置

    1.JDK的下载 jdk1.8版本   下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-213 ...

  9. React项目中使用HighCharts

    大家都知道BizCharts是基于react封装的一套图表工具,而HighCharts是基于jQuery的.但是由于本人对BizCharts甚是不熟,所以在react项目开发中选择了HighChart ...

  10. C# wnform 请求http ( get , post 两种方式 )

    1.Get请求 string strURL = "http://localhost/WinformSubmit.php?tel=11111&name=张三";System. ...