zoukankan      html  css  js  c++  java
  • simple-spring-memcached简介

    memcached是一款非常优秀的分布式缓存工具,有效提升了按主键检索数据的性能问题。

    而simple-spring-memcached组件通过与spring框架整合,让memcached的调用变得更加简单。
    simple-spring-memcached本质上是采用了AOP的方式来实现缓存的调用和管理,其核心组件声明了一些Advice,当遇到相应的切入点时,会执行这些Advice来对memcached加以管理。


    切入点是通过标签的方式来进行声明的,在项目开发时,通常在DAO的方法上加以相应的标签描述,来表示组件对该方法的拦截

    • SingleCache类 操作单个POJO的Cache数据,由ParameterValueKeyProvider和CacheKeyMethod来标识组装key
    • MultiCache类 操作List型的Cache数据,由ParameterValueKeyProvider和CacheKeyMethod来标识组装key
    • AssignCache类 指定key操作Cache数据,由annotation中的 assignedKey 指定key

    组件所提供的切入点主要包括以下几种:
         ReadThroughSingleCache、ReadThroughMultiCache、ReadThroughAssignCache
        当遇到查询方法声明这些切入点时,组件首先会从缓存中读取数据,取到数据则跳过查询方法,直接返回。
        取不到数据在执行查询方法,并将查询结果放入缓存,以便下一次获取。

    InvalidateSingleCache、InvalidateMultiCache、InvalidateAssignCache
    当遇到删除方法声明这些切入点时,组件会删除缓存中的对应实体

    UpdateSingleCache、UpdateMultiCache、UpdateAssignCache
    当遇到更新方法声明这些切入点是,组件会更新缓存中对应的实体,以便下次从缓存中读取出的数据状态是最新的

    项目的优点:

      1. 与spring集成
      2. 支持两种Memcached java client (spymemcached 和 xmemcached)
      3. 使用注解的方式,代码的入侵性小
      4. annotation 丰富,可以满足大部分的需求。

    simple-spring-memcached的使用:

    1、在项目中pom.xml 添加对memcached Maven的依赖

    <dependency>
                    <groupId>com.googlecode.xmemcached</groupId>
                    <artifactId>xmemcached</artifactId>
                    <version>1.3.3</version>
                </dependency>

    如此,memcached 需要的jar 包便自动下载到本地了。

    2、在spring中,如下配置

      <?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:aop="http://www.springframework.org/schema/aop"
             xmlns:context="http://www.springframework.org/schema/context"
             xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
          
          <import resource="classpath:simplesm-context.xml"/>
      
          <!--开启自动代理-->
          <aop:aspectj-autoproxy/>
          <context:annotation-config/>
          
          <bean name="defaultCache" class="com.google.code.ssm.CacheFactory">
              <property name="cacheClientFactory">
                  <bean class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl"/>
              </property>
              <property name="addressProvider">
                  <bean class="com.google.code.ssm.config.DefaultAddressProvider">
                      <property name="address" value="${memcached.server}:${memcached.port}"/>
                  </bean>
              </property>
              <property name="configuration">
                  <bean class="com.google.code.ssm.providers.xmemcached.XMemcachedConfiguration">
                      <property name="consistentHashing" value="false"/>
                      <property name="connectionPoolSize" value="30"/>
                      <property name="optimizeGet" value="true"/>
                  </bean>
              </property>
              <property name="cacheName">
                  <value>appCache</value>
              </property>
          </bean>
      </beans>
    <import resource="classpath:simplesm-context.xml"/>  :导入simple-spring-memcached的核心配置文件,用来加载组件核心的Advice,供程序调度使用

    <aop:aspectj-autoproxy/> :基于AOP的代理,让代理机制起到作用

    ${memcached.server}:memcached服务的IP,${memcached.port}" :端口号

    memcached比较常用的java客户端有两种,spymemcached和xmemcached,xmemcached支持多线程

    3、读取缓存

    @ReadThroughSingleCache(namespace = "canvas:SkuDetailVos:",expiration = 120)
        @CacheName("appCache")
        public List<SkuDetailVo> assembleSkuDetails( String skuIds, @ParameterValueKeyProvider String cacheKey) {
    
        }

    namespace 定义好命名空间,防止缓存数据出现冲突。expiration 定义过期的时间,单位为秒,@cacheName 定义的是应用程序的cache ,避免与其他cache冲突。@ParameterValueKeyProvider String cacheKey 定义了生成key 的方式。

    4、多个key 的缓存

    @ReadThroughSingleCache(namespace = "canvas:goodsDataDo:", expiration = 60)
        @CacheName("appCache")
        public GoodsDataDo assembleGoodsData(@ParameterValueKeyProvider(order = 1) ItemSkusWithStoreAndPriceDo itemSkusWithStoreAndPrice,
                                             @ParameterValueKeyProvider(order = 2) GoodsDO goodsDO,
                                             String catName) {

    需要注意的是,方法参数多个key 作为cachekey 时,要为ParameterValueKeyProvider 指定order 的值。

    @CacheKeyMethod
        public String cacheKey() {
            return "store_price_" + this.getSkuId().toString();
        }
    在类ItemSkusWithStoreAndPriceDo 中指定cachekey 具体生成

    5、删除缓存

    @InvalidateSingleCache(namespace = "goodscenter:ZeroPurchaseDo")
        @CacheName("appCache")
        public void updateZeroPurchaseDo(@ParameterValueKeyProvider ZeroPurchaseDo zeroPurchaseDo) {
         
     }

    @InvalidateSingleCache 当遇到这个时,删除缓存

    @CacheKeyMethod
        public String getCacheKey() {
            return String.valueOf(termId);
        }

    ZeroPurchaseDo  类中 ,用@CacheKeyMethod 定义cacheKey 的生成方法。

    6、更新缓存

    @UpdateSingleCache(namespace = "uic:updateMembers", expiration = 3600)  
        @Override  
        public void updateMembers(@ParameterValueKeyProvider @ParameterDataUpdateContent Members members) {  
            
        }
  • 相关阅读:
    c# 泛型总结
    透过字节码分析java基本类型数组的内存分配方式。
    c#索引器
    redis在asp.net 中的应用
    Unity3D shaderLab
    Unity3d Asset Store 打不开
    C# 类型转换的开销
    [转]权重算法
    Coroutine的原理以及实现
    在Unity3D里使用WinForm
  • 原文地址:https://www.cnblogs.com/pan2011/p/3650151.html
Copyright © 2011-2022 走看看