• Spring缓存机制----EhCache缓存实现的配置


    1. 首先,需要添加连个jar包:ehcache-core-2.6.11.jar和slf4j-api-1.7.21.jar
    2. 在类加载路径下添加一个ehcache.xml配置文件,文件内容如下:
    <?xml version="1.0" encoding="utf-8"?>
    <ehcache>
        <diskStore path="java.io.tmpdir"/>
        <!-- 配置默认的缓存区 -->
        <defaultCache 
            maxElementsInMemory="10000"        
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"/>
        
        <!-- 配置名为users的缓存区 -->
        <cache name="users"
            maxElementsInMemory="10000"
            eternal="false"
            overflowToDisk="true"
            timeToIdleSeconds="300"
            timeToLiveSeconds="600" />
        
        <!-- 
         maxElementsInMemory : 设置属性中最多可以放多少个对象 
         eternal:设置缓存是否永久有效
         timeToIdleSeconds:设置缓存的对象多少秒没有被使用就会清理掉
         timeToLiveSeconds:设置缓存的对象在过期之前可以缓存多少秒
         diskPersistent:设置缓存是否被持久化到硬盘中,保存路径由<diskStore.../>元素指定
         -->
    
    </ehcache>

      3.在spring的配置文件bean.xml中进行配置:

    <cache:annotation-driven />
    <context:component-scan base-package="com.XXX.XXX"/>
        <!-- 配置EhCache的CacheManager 通过configLocation指定ehcache.xml文件的位置 -->
        <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
            p:configLocation="classpath:ehcache.xml"
            p:shared="false"/>
        <!-- 配置基于EhCache的缓存管理器,并将EhCache的CacheManager注入该缓存管理器Bean中 -->
        <bean id="cacheManager"
            class="org.springframework.cache.ehcache.EhCacheCacheManager"
            p:cacheManager-ref="ehCacheManager"/>

    上面代码中<cache:annotation-driven />的作用是启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 

    <cache:annotation-driven/>有一个cache-manager属性用来指定当前所使用的CacheManager对应的bean的名称,默认是cacheManager,所以当我们的CacheManager的id为cacheManager时我们可以不指定该参数,否则就需要我们指定了。
    <cache:annotation-driven/>还可以指定一个mode属性,可选值有proxy和aspectj。默认是使用proxy。当mode为proxy时,只有缓存方法在外部被调用的时候Spring Cache才会发生作用,这也就意味着如果一个缓存方法在其声明对象内部被调用时Spring Cache是不会发生作用的。而mode为aspectj时就不会有这种问题。另外使用proxy时,只有public方法上的@Cacheable等标注才会起作用,如果需要非public方法上的方法也可以使用Spring Cache时把mode设置为aspectj。
    此外,<cache:annotation-driven/>还可以指定一个proxy-target-class属性,表示是否要代理class,默认为false。我们前面提到的@Cacheable、@cacheEvict等也可以标注在接口上,这对于基于接口的代理来说是没有什么问题的,但是需要注意的是当我们设置proxy-target-class为true或者mode为aspectj时,是直接基于class进行操作的,定义在接口上的@Cacheable等Cache注解不会被识别到,那对应的Spring Cache也不会起作用了。
    需要注意的是<cache:annotation-driven/>只会去寻找定义在同一个ApplicationContext下的@Cacheable等缓存注解。

      4.使用@Cacheable执行缓存

    import org.springframework.cache.annotation.CacheEvict;
    import org.springframework.cache.annotation.Cacheable;
    import org.springframework.stereotype.Service;
    
    @Service("userService")
    @Cacheable(value="users")
    public class UserServiceImpl implements UserService{
    
        public User getUsersByNameAndAge(String name,int age){
            System.out.println("--正在执行findUsersByNameAndAge()查询方法--");
            return new User(name,age);
        }
        
        public User getAnotherUser(String name,int age){
            System.out.println("--正在执行findAnotherUser()查询方法--");
            return new User(name,age);
        }
        
    }

      5.测试

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class SpringTest {
    
        public static void main(String[] args) {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
            UserService us = ctx.getBean("userService",UserService.class);
            User u1 = us.getUsersByNameAndAge("孙悟空", 500);
            User u2 = us.getAnotherUser("孙悟空", 500);
                    System.out.println(u1==u2);
    }

    上面结果输出true,表示第二次是从缓存中获取对象

  • 相关阅读:
    docker
    perl有用的字符串匹配
    nginx 反向代理 kuberntes service 出现 502 问题排查
    应用应用容器化部署最佳实践
    监控系统设计
    kubernetes events 事件机制源码分析
    golang gc 原理和内存分配
    golang 使用 websocket 实现后台消息分组推送
    kubeadm 创建的 k8s 集群的升级
    在 kubernetes pod 中构建 docker image
  • 原文地址:https://www.cnblogs.com/zest/p/5876666.html
Copyright © 2020-2023  润新知