码迷,mamicode.com
首页 > 编程语言 > 详细

spring memcache 缓存

时间:2015-01-21 22:11:28      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:

application-cache.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: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的方法,第二次访问的时候就不会访问数据库了,访问缓存去了。

 

要使用这个还得安装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 缓存

标签:

原文地址:http://www.cnblogs.com/hjy9420/p/4239940.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!