application-cache.xml的配置

在web.xml中引入了这个配置文件

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value> classpath*:application-cache.xml,
</param-value>
</context-param>
<?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:context="http://www.springframework.org/schema/context" xmlns:cache="http://www.springframework.org/schema/cache"
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
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd"> <!-- 引入properties配置文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath*:application.properties</value>
</list>
</property>
</bean>
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
<cache:annotation-driven /> <!-- 缓存管理 -->
<bean name="cacheManager" class="com.google.code.ssm.spring.ExtendedSSMCacheManager">
<property name="caches">
<set>
<bean class="com.google.code.ssm.spring.SSMCache">
<constructor-arg name="cache" index="0" ref="MyCache" />
<!-- 5 minutes -->
<constructor-arg name="expiration" index="1" value="${default.memcache.expiration}" />
<!-- @CacheEvict(..., "allEntries" = true) doesn't work -->
<constructor-arg name="allowClear" index="2" value="false" />
</bean>
</set>
</property>
</bean> <!-- 缓存配置 -->
<bean name="MyCache" class="com.google.code.ssm.CacheFactory">
<property name="cacheName" value="MyCache" />
<property name="cacheClientFactory" ref="cacheClientFactory"/>
<property name="addressProvider">
<bean class="com.google.code.ssm.config.DefaultAddressProvider">
<property name="address" value="${default.memcache.address}" />
</bean>
</property>
<property name="configuration">
<bean class="com.google.code.ssm.providers.CacheConfiguration">
<!-- 是否使用哈希 -->
<property name="consistentHashing" value="true" />
</bean>
</property>
<property name="defaultSerializationType"
value="#{T(com.google.code.ssm.api.format.SerializationType).valueOf(@defaultSerializationTypeAsString)}" />
</bean> <bean name="cacheClientFactory" class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" /> <bean name="defaultSerializationTypeAsString" class="java.lang.String">
<constructor-arg value="#{systemProperties['ssm.defaultSerializationType']?:'JSON'}" />
</bean>
</beans>

application.properties的配置 下面那个地址是我本机的地址

#memcached memcache server address
default.memcache.address=192.168.3.11:11211
#expire time unit:seconds
default.memcache.expiration=43200
    @Override
@Cacheable(value = "MyCache",key="T(com.base.util.Constant).AREA_FULL_QUERYFORNAV")
public List<Area> queryAllFirstArea(AreaPage page) {
return dao.queryAllFirstArea(page);
}

@Cacheaable的key是唯一的,当第一次访问的时候,会执行dao的方法,第二次访问的时候就不会访问数据库了,访问缓存去了。

    /**
* 清空缓存
*/
@Override
@CacheEvict(value = "MyCache",key="T(com.base.util.Constant).AREA_FULL_QUERYFORNAV")
public void clearCache() {
System.out.println("清空缓存成功!!!"); }

@CacheEvict 注解清空缓存,当下次要查询这个key的缓存时,会查询数据库,将查询的结果缓存起来,下次再查询就直接查缓存了。

要使用这个还得安装mecache。我安装的是window版的。mecache下载安装地址.

解压后把里面的所有文件放到一个地方。我放到地方是:E:\mecache

(1)在终端(也即 cmd 命令界面)下输入 E:\memcached\memcached.exe -d install 令名来执行安装!

(2) 再次在终端输入: E:\memcached\memcached.exe -d start 来启动 memcache 服务。(以后 memcached 将作为 windows 的一个服务每次开机时

自动启动。这样服务器端已经安装完毕了)

(3)检验是否安装成功,只需要在 CMD 下输入 telnet 127.0.0.1 11211 就可以知道(memcache 默认端口是 11211)。

现在已经是可以访问的了!!

spring memcache 缓存的更多相关文章

  1. 注释驱动的 Spring cache 缓存介绍

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  2. [转]注释驱动的 Spring cache 缓存介绍

    原文:http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ 概述 Spring 3.1 引入了激动人心的基于注释(an ...

  3. 注释驱动的 Spring cache 缓存介绍--转载

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  4. Spring cache 缓存

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  5. springboot使用memcache缓存

    Memcached简介 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的 ...

  6. 使用Memcache缓存mysql数据库操作的原理和缓存过程浅析

    转载自脚本之家 http://www.jb51.net/article/51831.htm  作者:忙碌的松鼠 对于大型网站如facebook,ebay等网站,如果没有Memcache做为中间缓存层, ...

  7. memcached基于socket访问memcache缓存服务器

    memcached基于socket访问memcache缓存服务器 操作memcache常用三种方法: .memcache基于php_memcache.dll扩展(php扩展) .memcached基于 ...

  8. phalcon: 缓存片段,文件缓存,memcache缓存

    几种缓存,需要用到前端配置,加后端实例配合着用 片段缓存: public function indexAction() { //渲染页面 $this->view->setTemplateA ...

  9. 01-08-05【Nhibernate (版本3.3.1.4000) 出入江湖】NHibernate二级缓存:第三方MemCache缓存

    一.准备工作 [1]根据操作系统(位数)选择下载相应版本的MemCache, MemCache的下载和安装,参看: http://www.cnblogs.com/easy5weikai/p/37606 ...

随机推荐

  1. nginx在window上无法启动的问题

    内容列表: 简要介绍 下载安装 配置测试 一.简要介绍 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP ...

  2. 博客搬到CSDN了,以后就老实的呆在这儿吧~~

    几年前读书的时候就自己在做独立的个人博客网站,重做 + 改版好多次,域名也换了好几个- 163fly.com.godbz.com.zhouz.me ... 都是我曾经用过的域名,都放弃了- 发现到头来 ...

  3. 【IIS】windows2008 ii7 设置访问网站提示帐号密码登录

    3个步骤: 1.添加windows身份验证: windows2008默认是不启用的,需要我们自己去启动,在管理工具 - 服务器管理- 角色 ,拉下去,下面有个[添加角色服务],安全性- Windows ...

  4. CC版本添加 LUCI

    BB版本的时候,在 feeds/luci/contrib/package/luci下的Makefile文件添加上需要编译的 luci-application即可. 在CC版里改为:Feeds/luci ...

  5. percentiles of live data capture

    For example, every ten seconds I got a server response time, I want to know the 90 percentile respon ...

  6. Python学习日志(一)

    1.os - Normal Method: os.name() : os.getcwd(): 给出当前的目录,python当前的工作目录 os.listdir(): 返回 os.remove():删除 ...

  7. Myeclipse启动错误

    问题描述: Errors occurred during the build.Errors running builder 'DeploymentBuilder' on project 'szoa'. ...

  8. React学习笔记-04 props

    props实现从父组件与子组件的通信. 可以通过getDefaultProps初始化props. React 提供了propsTypes来验证props的类型 官方API: propTypes:fun ...

  9. javascript函数作用域实践

    在es6之前,JavaScript是没有块级作用域的,只有函数作用域,也就是说是一个function里面定义的变量外面取不到的:而if for是条件判断的语句,不是函数,由于缺少块级作用域,所以条件中 ...

  10. android studio的lib和jniLibs

    在android studio 中添加jar和so时,将jar文件直接拷贝到 项目目录\app\libs下即可,将so文件按照平台分类目录放到 项目目录\app\src\main\jniLibs\平台 ...