洞察纵观鸿蒙next版本,如何凭借FinClip加强小程序的跨平台管理,确保企业在数字化转型中的高效运营和数据安全?
739
2022-11-28
spring缓存cache的使用详解
目录spring缓存cache的使用springcache配置缓存存活时间
spring缓存cache的使用
在spring配置文件中添加schema和spring对缓存注解的支持:
xmlns:aop="http://springframework.org/schema/aop" xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:mvc="http://springframework.org/schema/mvc" xmlns:context="http://springframework.org/schema/context" xmlns:tx="http://springframework.org/schema/tx" xmlns:p="http://springframework.org/schema/p" xmlns:cache="http://springframework.org/schema/cache" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd http://springframework.org/schema/context http://wwnhIrkQw.springframework.org/schema/context/spring-context-3.0.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.0.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd http://springframework.org/schema/cache http://springframework.org/schema/cache/spring-cache.xsd" default-autowire="byName"> 在spring配置文件中加入缓存管理器: class="org.springframework.cache.support.SimpleCacheManager"> p:name="hardwareCache"/> p:name="bannerCache"/> 然后在代码的service的impl层加上如下注解即可把数据缓存起来: @Cacheable(value="bannerCache") 其中@Cacheable表示spring将缓存该方法获取到的数据,(缓存是基于key-value方式实现的),key为该方法的参数,value为返回的数据,当你连续访问该方法时你会发现只有第一次会访问数据库. 其他次数只是查询缓存.减轻了数据库的压力. 当更新了数据库的数据,需要让缓存失效时,使用下面的注解: 这个注解表示让appCache缓存的所有数据都失效。 @CacheEvict(value = "appCache", allEntries = true) springcache配置缓存存活时间 Spring Cache @Cacheable本身不支持key expiration的设置,以下代码可自定义实现Spring Cache的expiration,针对Redis、SpringBoot2.0。 直接上代码: @Service @Configuration public class CustomCacheMng{ private Logger logger = LoggerFactory.getLogger(this.getClass()); // 指明自定义cacheManager的bean name @Cacheable(value = "test",key = "'obj1'",cacheManager = "customCacheManager") public User cache1(){ User user = new User().setId(1); logger.info("1"); return user; } @Cacheable(value = "test",key = "'obj2'") public User cache2(){ User user = new User().setId(1); logger.info("2"); return user; } // 自定义的cacheManager,实现存活2天 @Bean(name = "customCacheManager") public CacheManager cacheManager( RedisTemplate, ?> redisTemplate) { RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWrhttp://iter(redisTemplate.getConnectionFactory()); RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(2)); return new RedisCacheManager(writer, config); } // 提供默认的cacheManager,应用于全局 @Bean @Primary public CacheManager defaultCacheManager( RedisTemplate, ?> redisTemplate) { RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWriter(redisTemplate.getConnectionFactory()); RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); return new RedisCacheManager(writer, confinhIrkQg); } }
xmlns:aop="http://springframework.org/schema/aop"
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://springframework.org/schema/mvc"
xmlns:context="http://springframework.org/schema/context"
xmlns:tx="http://springframework.org/schema/tx"
xmlns:p="http://springframework.org/schema/p"
xmlns:cache="http://springframework.org/schema/cache"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans-3.0.xsd
http://springframework.org/schema/context
http://wwnhIrkQw.springframework.org/schema/context/spring-context-3.0.xsd
http://springframework.org/schema/mvc
http://springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://springframework.org/schema/aop
http://springframework.org/schema/aop/spring-aop-3.0.xsd
http://springframework.org/schema/tx
http://springframework.org/schema/tx/spring-tx.xsd
http://springframework.org/schema/cache
http://springframework.org/schema/cache/spring-cache.xsd"
default-autowire="byName">
在spring配置文件中加入缓存管理器:
class="org.springframework.cache.support.SimpleCacheManager"> p:name="hardwareCache"/> p:name="bannerCache"/> 然后在代码的service的impl层加上如下注解即可把数据缓存起来: @Cacheable(value="bannerCache") 其中@Cacheable表示spring将缓存该方法获取到的数据,(缓存是基于key-value方式实现的),key为该方法的参数,value为返回的数据,当你连续访问该方法时你会发现只有第一次会访问数据库. 其他次数只是查询缓存.减轻了数据库的压力. 当更新了数据库的数据,需要让缓存失效时,使用下面的注解: 这个注解表示让appCache缓存的所有数据都失效。 @CacheEvict(value = "appCache", allEntries = true) springcache配置缓存存活时间 Spring Cache @Cacheable本身不支持key expiration的设置,以下代码可自定义实现Spring Cache的expiration,针对Redis、SpringBoot2.0。 直接上代码: @Service @Configuration public class CustomCacheMng{ private Logger logger = LoggerFactory.getLogger(this.getClass()); // 指明自定义cacheManager的bean name @Cacheable(value = "test",key = "'obj1'",cacheManager = "customCacheManager") public User cache1(){ User user = new User().setId(1); logger.info("1"); return user; } @Cacheable(value = "test",key = "'obj2'") public User cache2(){ User user = new User().setId(1); logger.info("2"); return user; } // 自定义的cacheManager,实现存活2天 @Bean(name = "customCacheManager") public CacheManager cacheManager( RedisTemplate, ?> redisTemplate) { RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWrhttp://iter(redisTemplate.getConnectionFactory()); RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(2)); return new RedisCacheManager(writer, config); } // 提供默认的cacheManager,应用于全局 @Bean @Primary public CacheManager defaultCacheManager( RedisTemplate, ?> redisTemplate) { RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWriter(redisTemplate.getConnectionFactory()); RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); return new RedisCacheManager(writer, confinhIrkQg); } }
class="org.springframework.cache.support.SimpleCacheManager">
p:name="hardwareCache"/> p:name="bannerCache"/> 然后在代码的service的impl层加上如下注解即可把数据缓存起来: @Cacheable(value="bannerCache") 其中@Cacheable表示spring将缓存该方法获取到的数据,(缓存是基于key-value方式实现的),key为该方法的参数,value为返回的数据,当你连续访问该方法时你会发现只有第一次会访问数据库. 其他次数只是查询缓存.减轻了数据库的压力. 当更新了数据库的数据,需要让缓存失效时,使用下面的注解: 这个注解表示让appCache缓存的所有数据都失效。 @CacheEvict(value = "appCache", allEntries = true) springcache配置缓存存活时间 Spring Cache @Cacheable本身不支持key expiration的设置,以下代码可自定义实现Spring Cache的expiration,针对Redis、SpringBoot2.0。 直接上代码: @Service @Configuration public class CustomCacheMng{ private Logger logger = LoggerFactory.getLogger(this.getClass()); // 指明自定义cacheManager的bean name @Cacheable(value = "test",key = "'obj1'",cacheManager = "customCacheManager") public User cache1(){ User user = new User().setId(1); logger.info("1"); return user; } @Cacheable(value = "test",key = "'obj2'") public User cache2(){ User user = new User().setId(1); logger.info("2"); return user; } // 自定义的cacheManager,实现存活2天 @Bean(name = "customCacheManager") public CacheManager cacheManager( RedisTemplate, ?> redisTemplate) { RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWrhttp://iter(redisTemplate.getConnectionFactory()); RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(2)); return new RedisCacheManager(writer, config); } // 提供默认的cacheManager,应用于全局 @Bean @Primary public CacheManager defaultCacheManager( RedisTemplate, ?> redisTemplate) { RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWriter(redisTemplate.getConnectionFactory()); RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig(); return new RedisCacheManager(writer, confinhIrkQg); } }
p:name="hardwareCache"/>
p:name="bannerCache"/>
p:name="bannerCache"/>
然后在代码的service的impl层加上如下注解即可把数据缓存起来:
@Cacheable(value="bannerCache")
其中@Cacheable表示spring将缓存该方法获取到的数据,(缓存是基于key-value方式实现的),key为该方法的参数,value为返回的数据,当你连续访问该方法时你会发现只有第一次会访问数据库. 其他次数只是查询缓存.减轻了数据库的压力.
当更新了数据库的数据,需要让缓存失效时,使用下面的注解:
这个注解表示让appCache缓存的所有数据都失效。
@CacheEvict(value = "appCache", allEntries = true)
springcache配置缓存存活时间
Spring Cache @Cacheable本身不支持key expiration的设置,以下代码可自定义实现Spring Cache的expiration,针对Redis、SpringBoot2.0。
直接上代码:
@Service
@Configuration
public class CustomCacheMng{
private Logger logger = LoggerFactory.getLogger(this.getClass());
// 指明自定义cacheManager的bean name
@Cacheable(value = "test",key = "'obj1'",cacheManager = "customCacheManager")
public User cache1(){
User user = new User().setId(1);
logger.info("1");
return user;
}
@Cacheable(value = "test",key = "'obj2'")
public User cache2(){
User user = new User().setId(1);
logger.info("2");
return user;
}
// 自定义的cacheManager,实现存活2天
@Bean(name = "customCacheManager")
public CacheManager cacheManager(
RedisTemplate, ?> redisTemplate) {
RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWrhttp://iter(redisTemplate.getConnectionFactory());
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(2));
return new RedisCacheManager(writer, config);
}
// 提供默认的cacheManager,应用于全局
@Bean
@Primary
public CacheManager defaultCacheManager(
RedisTemplate, ?> redisTemplate) {
RedisCacheWriter writer = RedisCacheWriter.lockingRedisCacheWriter(redisTemplate.getConnectionFactory());
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
return new RedisCacheManager(writer, confinhIrkQg);
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~