• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

LinkL1

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

Springboot整合Redis简单应用

依赖:

<!--        redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

 

application配置文件:

# redis配置
# 指定redis的主机地址
spring.redis.host=192.168.159.128   // 这个是redis的运行主机地址,本机配置的就是本机地址,虚拟机配置的就是虚拟机地址
#logging.level.com.example.demo_crud.mapper=debug
#debug=true
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1ms
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=5000
# Redis数据库索引(默认为0)
spring.redis.database=0

  

新建RedisConfig配置文件进行redis序列化配置:

/**
 * @Author: L
 * @Date: 2022/3/30 13:26
 * @Description: *
 */
@Configuration
public class RedisConfig {
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        //初始化一个RedisCacheWriter
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
        //设置CacheManager的值序列化方式为json序列化
        RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer();
        RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair
                .fromSerializer(jsonSerializer);
        RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig()
                .serializeValuesWith(pair);
        //设置默认超过期时间是30秒
        defaultCacheConfig.entryTtl(Duration.ofSeconds(30));
        //初始化RedisCacheManager
        return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
    }
}

  

对比较常用的数据查询操作增加redis缓存:

 

// getall 获取所有商品数据
public List<Goods> getAll() {
    // 将返回商品数组转换为jsonString
    String goodsliststring = JSON.toJSONString(goodsMapper.getAll());
    // 岁redis数据库进行KV操作
    ValueOperations<String, String> valueops = redisTemplate.opsForValue();
    // 将所有商品数据放入缓存
    valueops.set(goodsListKey,goodsliststring);
    // 获取redis数据库中的数据
    String goodsListJson = valueops.get(goodsListKey);
    // 判断redis中是否有数据,如果有数据直接在redis中拿,没有则去数据库查询
    if(!StringUtils.isEmpty(goodsListJson)){
        return  goodsMapper.getAll();
    }
    return goodsMapper.getAll();
}

 

  

 

  

在进行更新或者删除操作时加上对redis的清除操作,实现redis缓存的清除更新:

  

// 根据ID删除数据库中信息
public boolean deleteById(int id){
  // 自己定义需要的redis清除缓存操作
    redisTemplate.delete(redisTemplate.keys("goods*")); // 删除正则匹配goods*系列key的缓存
    boolean flag = false;
    try{
        userMapper.deleteById(id);
        flag = true;
    }catch (Exception e){
        e.printStackTrace();
    }
    return flag;
}

  

posted on 2022-03-30 14:38  LinkL1  阅读(77)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3