springboot整合redis实现缓存

dkws-2019      2022-05-08     257

关键词:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>1.5.2.RELEASE</version>
</dependency>
spring: 
  redis:
    ##默认redis客户端连接为0 可修改
    database: 0
    host: 127.0.0.1
    port: 6379
    cache_name: XiChuanRedis
    ##password:
    pool:
      ##连接池最大空闲连接
      max-idle: 8
      min-idle: 0
      max-active: 8
      max-wait: 1
    timeout: 5000
@Configuration
@EnableCaching//启用缓存
public class RedisConfig extends CachingConfigurerSupport{
 
    @Value("${spring.redis.cache_name}")
    private String cacheName;
 
    @SuppressWarnings("rawtypes")
    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
        // 多个缓存的名称,目前只定义了一个
        rcm.setCacheNames(Arrays.asList(cacheName));
        //设置缓存过期时间(秒)
        rcm.setDefaultExpiration(6000);
        return rcm;
    }
 
    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}
@Component
public classBaseServiceImpl{
 
    @Autowired
    ParamRepository paramRepository;
 
    @Cacheable(value="XiChuanRedis", key="‘sign_test‘")  
    public String getNotFinishedTypeCode(){
        List<Param> params = paramRepository.findBySpiderOver(0);
 
        if(params != null && params.size() > 0){
            return  params.stream()   //先将List按照id进行排序,然后取出最上面的那个,然后取出第一个
                    .sorted(Comparator.comparing(HeiGuangParam::getId))
                    .collect(Collectors.toList())
                    .get(0)
                    .getTypeCode();
        }else{   //如果没有就返回null
            return null;
        }
    }
 
    /**
     * 更新param后,需要将redis中的值移除
     * @param param
     */
    @CacheEvict(value="XiChuanRedis", key="‘sign_test‘")
    public void updateParamByTypeCode(Param param){
        paramRepository.save(param);
    }
}

@Cacheable:如果redis在相同的Cache有相同的key的缓存元素时,就会直接在redis缓存中,根据key读取缓存并返回结果;如果没有,则先调用方法,将返回值写入到redis缓存中,然后返回结果。它的属性有:value、key、condition

value:指定是哪一个Cache,可以指定多个Cache。例如:@Cacheable({"cache1", "cache2"})

key:缓存的key,当在清楚缓存时要与此对应。

1.key为null,则使用默认key。

2.key为一个字符串,例如:@CacheEvict(value="XiChuanRedis", key="‘department_code_‘")。
3.key为方法中的一个方法上的一个属性,例如:

@CacheEvict(value="XiChuanRedis", key="‘code_‘+#departmentCode")
public String getNameByCode(String departmentCode){
}

4.key为方法中的一个方法上的一个实体的属性,例如:

@Cacheable(value="XiChuanRedis", key="‘userid_‘+#user.id")
public User getUserById(Integer id){
}

condition:缓存条件,可以直接为null,例子如下:

@Cacheable(value="XiChuanRedis", key="‘userid_‘+#user.id", condition="#user.id%2==0")
public User getUserById(Integer id){
}

 

精通springboot--整合redis实现缓存

今天我们来讲讲怎么在springboot中整合redis实现对数据库查询结果的缓存。首先第一步要做的就是在pom.xml文件添加spring-boot-starter-data-redis。要整合缓存,必不可少的就是我们要继承一个父类CachingConfigurerSupport。我们先看看这个类... 查看详情

springboot整合redis实现缓存

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>1.5.2.RELEASE</version></dependency& 查看详情

springboot中使用redistemplate实现redis数据缓存

SpringBoot整合Redis数据库实现数据缓存的本质是整合Redis数据库,通过对需要“缓存”的数据存入Redis数据库中,下次使用时先从Redis中获取,Redis中没有再从数据库中获取,这样就实现了Redis做数据缓存。??按照惯例,下面一步一步... 查看详情

springboot整合redis缓存

...非常流行的NoSQL数据库(Redis)来实现我们的缓存需求。SpringBoot整合Redis是非常方便快捷的,我用的是Mybatis,这里就不说Springboot整合Mybatis了网上有很多,同样非常简单。下面进入正题: 查看详情

springboot整合springseesion实现redis缓存

参考技术A使用SpringBoot开发项目时我们经常需要存储Session,因为Session中会存一些用户信息或者登录信息。传统的web服务是将session存储在内存中的,一旦服务挂了,session也就消失了,这时候我们就需要将session存储起来,而Redis就... 查看详情

springboot整合redis缓存

使用springBoot添加redis缓存需要在POM文件里引入<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><dependency 查看详情

32springboot——缓存之整合redis

springboot缓存默认使用ConcurrentMapCacheManager 将数据保存在下面的Map中1、docker中开启Redis 2、添加Redis相关依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-st 查看详情

springboot整合shiro集成redis缓存

简介:由于考虑到项目后期分布式部署,所以缓存由ehcache改为redis,而redis既有单机版部署,也有分布式部署,所以二者需要兼容。1.maven依赖<dependency><groupId>org.crazycake</groupId><artifactId>shiro-redis</artifactId><... 查看详情

springboot----数据层技术的选择+各种整合(缓存,消息队列)+定时任务+发邮件(代码片段)

...NoSQL解决方案Redis安装和启动服务和可能出现的一个小BUGspringboot整合redis切换springboot操作redis客户端的实现技术(jedis或lettuce)MongoDB安装和初始化安装mongodb的可视化工具Robo3T整合整合第三方技术缓存手动实现一个简单的缓存机制spri... 查看详情

springboot(二十七)整合redis之分布式锁

在之前的一篇文章(《Java分布式锁,搞懂分布式锁实现看这篇文章就对了》),已经介绍过几种java分布式锁,今天来个Redis分布式锁的demo。redis现在已经成为系统缓存的必备组件,针对缓存读取更新操作,通常我们希望当缓存过期... 查看详情

springboot(二十四)整合redis

...介绍了Redis的安装和Redis的常用操作。今天主要介绍介绍springboot整合Redis。v应用场景现 查看详情

十八springboot2核心技术——整合redis(代码片段)

SpringBoot集成Redis1.1、目标完善根据学生id查询学生总数的功能,先从redis缓存中查找,如果找不到,再从数据库中查找,然后放到redis缓存中。1.2、实现步骤1.2.1、在pom.xml文件中添加redis依赖<dependencies><!--Sprin... 查看详情

十八springboot2核心技术——整合redis(代码片段)

SpringBoot集成Redis1.1、目标完善根据学生id查询学生总数的功能,先从redis缓存中查找,如果找不到,再从数据库中查找,然后放到redis缓存中。1.2、实现步骤1.2.1、在pom.xml文件中添加redis依赖<dependencies><!--Sprin... 查看详情

springboot2.0实战(23)整合redis之集成缓存springdatacache

SpringBoot2.0实战(23)整合Redis之集成缓存SpringDataCache来源:https://www.toutiao.com/a6803168397090619908/?timestamp=1584450221&app=news_article_lite&group_id=6803168397090619908&req_id=20200317210341 查看详情

缓存springboot整合redis01(代码片段)

eclipse创建springboot要注意,基本的包名一定要与java中的包名一致,这样才能找到springgboot主程序的入口;主程序是在com.lazy.cache;那么test的包也要是com.lazy.cachesrc/main/java com.lazy.cache CacheApplication.javasrc/test/java com.lazy.cahche UserTe 查看详情

springboot整合mybatis,redis,代码

一说明接着上篇讲述redis缓存配置的用法: 二正文首先要使用缓存就必须要开开启缓存,第二步是需要开redis-server下载redis包之后,点击图中两个都可以开启redis怎么看是否启动呢,见下图:  在application中加入连接地址,和相... 查看详情

springboot整合redis,一篇解决缓存的所有问题

前言上一篇博文,我们重点介绍了SpringBoot如何整合Mybatis,JPA等技术,访问我们的关系型数据库,这篇博文我们介绍SpringBoot如何整合Redis来访问非关系型数据库,带你深入了解Redis的自动原理,并结合具体... 查看详情

springboot整合spring@cache和redis(代码片段)

转载请注明出处:https://www.cnblogs.com/wenjunwei/p/10779450.htmlspring基于注解的缓存对于缓存声明,spring的缓存提供了一组java注解:@Cacheable:触发缓存写入。@CacheEvict:触发缓存清除。@CachePut:更新缓存(不会影响到方法的运行)。@Caching:重新... 查看详情