本文实例讲述了春天实战之缓存使用条件操作。分享给大家供大家参考,具体如下:
& lt; & # 63; xml version=" 1.0 " encoding=" GBK " & # 63;比; & lt;豆类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:缓存=" http://www.springframework.org/schema/cache " xmlns:上下文=" http://www.springframework.org/schema/context " xsi: schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd”比; & lt;环境:component-scan 基本包=" org.crazyit.app。服务”/比; & lt;缓存:注解驱动的 缓存管理器="缓存管理器"/比; & lt; !——配置EhCache的缓存管理器通过configLocation指定EhCache。xml文件的位置——比; & lt; bean id=" ehCacheManager " 类=" org.springframework.cache.ehcache.EhCacheManagerFactoryBean " p: configLocation="类路径:ehcache。xml”p:共享=" false "/比; & lt; !——配置基于EhCache的缓存管理器并将EhCache的缓存管理器注入该缓存管理器Bean——比; & lt; bean id=盎捍婀芾砥鳌? 类=" org.springframework.cache.ehcache.EhCacheCacheManager " p: cacheManager-ref=癳hCacheManager”比; & lt;/bean> & lt;/beans> >之前
& lt; & # 63; xml version=" 1.0 " encoding=" gbk " & # 63;比; & lt; ehcache> & lt; diskStore路径=" . io .tmpdir”/比; & lt; !——配置默认的缓存区——比; & lt; defaultCache maxElementsInMemory=" 10000 " 永恒的=" false " timeToIdleSeconds=" 120 " timeToLiveSeconds=" 120 " maxElementsOnDisk=" 10000000 " diskExpiryThreadIntervalSeconds=" 120 " memoryStoreEvictionPolicy=" LRU "/比; & lt; !——配置名为用户的缓存区——比; & lt;缓存名称=坝没А? maxElementsInMemory=" 10000 " 永恒的=" false " overflowToDisk=" true " timeToIdleSeconds=" 300 " timeToLiveSeconds=" 600 "/比; & lt;/ehcache> >之前
包org.crazyit.app.domain; 公开课用户 { 私人字符串名称; 私人int年龄; 公共用户() {} 公共用户(字符串名称,int年龄) { 超级(); this.name=名称; 这一点。年龄=年龄; } 公共字符串getName () { 返回名称; } 公共空间setName(字符串名称) { this.name=名称; } 公共int getAge () { 返回年龄; } 公共空间setAge (int年龄) { 这一点。年龄=年龄; } } >之前
1接口类
包org.crazyit.app.service; 进口org.crazyit.app.domain.User; 公共接口UserService { 用户getUsersByNameAndAge(字符串名称,int年龄); 用户getAnotherUser(字符串名称,int年龄); } >之前2实现类
包org.crazyit.app.service.impl; 进口org.crazyit.app.service.UserService; 进口org.crazyit.app.domain.User; 进口org.springframework.stereotype.Service; 进口org.springframework.cache.annotation.Cacheable; 进口org.springframework.context.annotation.Scope; @ service(“userService”) @Cacheable(值=" https://www.yisu.com/zixun/users ",条件=# age<100) 公开课UserServiceImpl UserService实现 { 公共用户getUsersByNameAndAge(字符串名称,int年龄) { System.out.println(”——正在执行findUsersByNameAndAge()查询方法——“); 返回新用户(姓名、年龄); } 公共用户getAnotherUser(字符串名称,int年龄) { System.out.println(”——正在执行findAnotherUser()查询方法——“); 返回新用户(姓名、年龄); } } >之前
李包; 进口org.crazyit.app.service.UserService; 进口org.crazyit.app.domain.User; 进口org.springframework.context.ApplicationContext; 进口org.springframework.context.support.ClassPathXmlApplicationContext; 公开课SpringTest { 公共静态void main (String [] args) { ApplicationContext ctx=新ClassPathXmlApplicationContext(“它指明”);=ctx UserService我们。getBean (“userService UserService.class);//调用方法时年龄参数不小于100年,因此不会缓存,//所以下面两次方法调用都会真正执行这些方法。 用户u1=etUsersByNameAndAge(“孙悟空",500); 用户u2=etAnotherUser(“孙悟空",500); system . out。println (u1==u2);//输出假的//调用方法时年龄参数小于100年,因此会缓存,//所以下面第二次方法调用时不会真正执行该方法,而是直接使用缓存数据。 用户u3=etUsersByNameAndAge(“孙悟空”,50); 用户u4=etAnotherUser(“孙悟空”,50); system . out。println (u3==u4);//输出正确的 } }春天实战之缓存使用条件操作示例