当前位置: 首页 > news >正文

广西建设科技与建筑节能协会网站百度大搜推广开户

广西建设科技与建筑节能协会网站,百度大搜推广开户,成都企业网站建站,php动态页面目录 前言 集成 maven依赖 CacheManagerConfig配置 redis配置 使用 Springboot 集成使用缓存 Cacheable CacheEvict 前言 现有项目中经常遇到的缓存集成问题,Springboot提供了统一的接口抽象与缓存管理器,可集成多种缓存类型,如 Co…

目录

前言

集成

maven依赖

CacheManagerConfig配置

redis配置

使用

Springboot 集成使用缓存

@Cacheable

@CacheEvict


前言

现有项目中经常遇到的缓存集成问题,Springboot提供了统一的接口抽象与缓存管理器,可集成多种缓存类型,如  ConcurrentMap 缓存、redis缓存,并可以随时无缝替换底层缓存存储。

集成

maven依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.3.0</version>
</dependency>

CacheManagerConfig配置

下面配置了2个,一个是localCacheManager,底层用的是ConcurrentMap,另一个是 RedisCacheManager,底层用的是redis。

通过 @Primary 注解,可以控制那个是主缓存。

@Configuration
@EnableCaching
@EnableConfigurationProperties(CacheProperties.class)
public class CacheManagerConfig {public static final String CACHE_MANAGER_LOCAL = "localCacheManager";public static final String CACHE_MANAGER_REDIS = "redisCacheManager";@Autowiredprivate CacheProperties cacheProperties;@Bean(name = CACHE_MANAGER_LOCAL)public CacheManager localCacheManager() {return new ConcurrentMapCacheManager();}@Primary@Bean(name = CACHE_MANAGER_REDIS)public RedisCacheManager redisCacheManager() {GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(cacheProperties.getRedis().getTimeToLive()).serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringRedisSerializer)).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(genericJackson2JsonRedisSerializer));return RedisCacheManager.builder(jedisConnectionFactory()).cacheDefaults(config).build();}
}

redis配置

配置文件

spring:redis:host: 192.168.1.252port: 6379database: 1password: 123456cache:redis:time-to-live: 600s

redisconfig

    /*** jedis连接工厂* @return*/@Beanpublic JedisConnectionFactory jedisConnectionFactory() {RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();redisStandaloneConfiguration.setDatabase(redisProperties.getDatabase());redisStandaloneConfiguration.setPort(redisProperties.getPort());redisStandaloneConfiguration.setHostName(redisProperties.getHost());redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration);return factory;}

redisTemplate

 @Bean(name = "redisTemplate")public RedisTemplate<String, Object> redisTemplate() {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(jedisConnectionFactory());Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);ObjectMapper objectMapper = new ObjectMapper();GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();redisTemplate.setDefaultSerializer(genericJackson2JsonRedisSerializer);objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(objectMapper);redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);redisTemplate.afterPropertiesSet();return redisTemplate;}

使用

注入cacheManager,并使用

    @Autowiredprivate CacheManager cacheManager;public void test() {Cache ca = cacheManager.getCache("userCache");ca.put("key", "value");ca.get("key");}

Springboot 集成使用缓存

@Cacheable

注解为 

@Cacheable 标记当前方法, 可将数据缓存与缓存,一般用于查询类方法

    @Cacheable(value = "firm", key = "#id", condition="#id!=null")public FirmBo getFirm(Long id) {return get(id);}

@CacheEvict

销毁缓存,一般用于编辑、新增或删除时候销毁缓存,下次查询时,会重新走数据库

​@CacheEvict(value = "person", key = "#person.id", condition = "#person.id!=null")public PersonBo savePerson(PersonBo person) {save(person);}

redis缓存存储实例

http://www.ds6.com.cn/news/89663.html

相关文章:

  • 青岛有没有专门做淘宝网站seo技术快速网站排名
  • 网络营销有什么新的变化浙江seo技术培训
  • 淄博做网站的推广软件下载
  • 做时时彩网站微信平台苏州seo
  • 做装修网站多少钱深圳seo推广
  • 厦门大型服装商城网站建设淘宝seo是什么意思
  • 做网站公司需要什么职位靠谱的免费建站
  • 简洁企业网站优化关键词的方法包括
  • 网站开发需要经费么在哪里可以免费自学seo课程
  • 猎头公司是什么优化大师绿色版
  • wordpress functions.php深圳百度关键字优化
  • 网站建设最新外文翻译免费广告
  • 做网站维护有什么要求关键词爱站网
  • 如何建立一个网站详细步骤自动外链网址
  • 做商城网站需要什么条件seo文章外包
  • jquery电子商务网站模板推广普通话手抄报
  • 怎么攻击织梦网站芒果视频怎样下载到本地
  • 个人主体可以做网站吗百度一下首页官网
  • 用模板做网站会被盗吗搜索引擎优化策略有哪些
  • php 自动做网站点击量广东深圳龙华区
  • 无锡盘古南宁seo排名首页
  • 做网站seo的公司哪家好网站做seo教程
  • 网站建设费可以计入办公费用么自助建站系统代理
  • 自己网站给别人网站做外链2020 惠州seo服务
  • wordpress 高端石家庄关键词优化平台
  • 网站页头尺寸760关键词排名查询
  • 广州网站建设阿里云南京seo代理
  • 售房网站开发 .net收录情况
  • 没有网站做分类信息群发seo外链软件
  • 企业管理系统平台新一代数字化办公平台网站排名优化多少钱