iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >springboot整合redis配置
  • 185
分享到

springboot整合redis配置

redisspringbootjava 2023-08-23 06:08:18 185人浏览 八月长安
摘要

1、引入pom依赖 org.springframework.boot spring-boot-starter-data-Redis 2、增加配置类 RedisCo

1、引入pom依赖

        <dependency>            <groupId>org.springframework.bootgroupId>            <artifactId>spring-boot-starter-data-RedisartifactId>        dependency>

2、增加配置类 RedisConfig

package com.ruoyi.framework.config;import org.springframework.cache.annotation.CachinGConfigurerSupport;import org.springframework.cache.annotation.EnableCaching;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.StringRedisSerializer;import com.fasterxml.jackson.annotation.JSONAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;@Configuration@EnableCachingpublic class RedisConfig extends CachingConfigurerSupport{    @Bean    @SuppressWarnings(value = { "unchecked", "rawtypes" })    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)    {        RedisTemplate<Object, Object> template = new RedisTemplate<>();        template.setConnectionFactory(connectionFactory);        Fastjson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);        ObjectMapper mapper = new ObjectMapper();        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);        serializer.setObjectMapper(mapper);        template.setValueSerializer(serializer);        // 使用StringRedisSerializer来序列化和反序列化redis的key值        template.seTKEySerializer(new StringRedisSerializer());        template.afterPropertiesSet();        return template;    }}

3、自定义 redis 工具

package org.demo.common.util;import java.util.List;import java.util.Map;import java.util.Set;import java.util.concurrent.TimeUnit;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.stereotype.Component;import org.springframework.util.CollectionUtils;@Componentpublic class RedisUtil {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;@Autowiredprivate StringRedisTemplate stringRedisTemplate;public boolean expire(String key, long time) {try {if (time > 0) {redisTemplate.expire(key, time, TimeUnit.SECONDS);}return true;} catch (Exception e) {e.printStackTrace();return false;}}public long getExpire(String key) {return redisTemplate.getExpire(key, TimeUnit.SECONDS);}public boolean hasKey(String key) {try {return redisTemplate.hasKey(key);} catch (Exception e) {e.printStackTrace();return false;}}@SuppressWarnings("unchecked")public void del(String... key) {if (key != null && key.length > 0) {if (key.length == 1) {redisTemplate.delete(key[0]);} else {redisTemplate.delete(CollectionUtils.arrayToList(key));}}}// ============================String=============================public Object get(String key) {return key == null ? null : redisTemplate.opsForValue().get(key);}public boolean set(String key, Object value) {try {redisTemplate.opsForValue().set(key, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}public boolean set(String key, Object value, long time) {try {if (time > 0) {redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);} else {set(key, value);}return true;} catch (Exception e) {e.printStackTrace();return false;}}public long incr(String key, long delta) {if (delta < 0) {throw new RuntimeException("递增因子必须大于0");}return redisTemplate.opsForValue().increment(key, delta);}public long decr(String key, long delta) {if (delta < 0) {throw new RuntimeException("递减因子必须大于0");}return redisTemplate.opsForValue().increment(key, -delta);}// ================================Map=================================public Object hget(String key, String item) {return redisTemplate.opsForHash().get(key, item);}public Map<Object, Object> hmget(String key) {return redisTemplate.opsForHash().entries(key);}public boolean hmset(String key, Map<String, Object> map) {try {redisTemplate.opsForHash().putAll(key, map);return true;} catch (Exception e) {e.printStackTrace();return false;}}public boolean hmset(String key, Map<String, Object> map, long time) {try {redisTemplate.opsForHash().putAll(key, map);if (time > 0) {expire(key, time);}return true;} catch (Exception e) {e.printStackTrace();return false;}}public boolean hset(String key, String item, Object value) {try {redisTemplate.opsForHash().put(key, item, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}public boolean hset(String key, String item, Object value, long time) {try {redisTemplate.opsForHash().put(key, item, value);if (time > 0) {expire(key, time);}return true;} catch (Exception e) {e.printStackTrace();return false;}}public void hdel(String key, Object... item) {redisTemplate.opsForHash().delete(key, item);}public boolean hHasKey(String key, String item) {return redisTemplate.opsForHash().hasKey(key, item);}public double hincr(String key, String item, double by) {return redisTemplate.opsForHash().increment(key, item, by);}public double hdecr(String key, String item, double by) {return redisTemplate.opsForHash().increment(key, item, -by);}// ============================set=============================public Set<Object> sGet(String key) {try {return redisTemplate.opsForSet().members(key);} catch (Exception e) {e.printStackTrace();return null;}}public boolean sHasKey(String key, Object value) {try {return redisTemplate.opsForSet().isMember(key, value);} catch (Exception e) {e.printStackTrace();return false;}}public long sSet(String key, Object... values) {try {return redisTemplate.opsForSet().add(key, values);} catch (Exception e) {e.printStackTrace();return 0;}}public long sSetAndTime(String key, long time, Object... values) {try {Long count = redisTemplate.opsForSet().add(key, values);if (time > 0) {expire(key, time);}return count;} catch (Exception e) {e.printStackTrace();return 0;}}public long sGetSetSize(String key) {try {return redisTemplate.opsForSet().size(key);} catch (Exception e) {e.printStackTrace();return 0;}}public long setRemove(String key, Object... values) {try {Long count = redisTemplate.opsForSet().remove(key, values);return count;} catch (Exception e) {e.printStackTrace();return 0;}}// ===============================list=================================public List<Object> lGet(String key, long start, long end) {try {return redisTemplate.opsForList().range(key, start, end);} catch (Exception e) {e.printStackTrace();return null;}}public long lGetListSize(String key) {try {return redisTemplate.opsForList().size(key);} catch (Exception e) {e.printStackTrace();return 0;}}public Object lGetIndex(String key, long index) {try {return redisTemplate.opsForList().index(key, index);} catch (Exception e) {e.printStackTrace();return null;}}public boolean lSet(String key, Object value) {try {redisTemplate.opsForList().rightPush(key, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}public boolean lSet(String key, Object value, long time) {try {redisTemplate.opsForList().rightPush(key, value);if (time > 0) {expire(key, time);}return true;} catch (Exception e) {e.printStackTrace();return false;}}public boolean lSet(String key, List<Object> value) {try {redisTemplate.opsForList().rightPushAll(key, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}public boolean lSet(String key, List<Object> value, long time) {try {redisTemplate.opsForList().rightPushAll(key, value);if (time > 0) {expire(key, time);}return true;} catch (Exception e) {e.printStackTrace();return false;}}public boolean lUpdateIndex(String key, long index, Object value) {try {redisTemplate.opsForList().set(key, index, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}public long lRemove(String key, long count, Object value) {try {Long remove = redisTemplate.opsForList().remove(key, count, value);return remove;} catch (Exception e) {e.printStackTrace();return 0;}}}

4、yml文件配置

spring:  # redis 配置  redis:    # 地址    host: 127.0.0.1    # 端口,默认为6379    port: 6379    # 密码    passWord: 123456    # 连接超时时间    timeout: 10s    lettuce:      pool:        max-active: 8   #最大连接数据库连接数,设 0 为没有限制        max-idle: 8     #最大等待连接中的数量,设 0 为没有限制        max-wait: -1ms  #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。        min-idle: 0     #最小等待连接中的数量,设 0 为没有限制

来源地址:https://blog.csdn.net/qq_19891197/article/details/128942999

--结束END--

本文标题: springboot整合redis配置

本文链接: https://www.lsjlt.com/news/378341.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • springboot整合redis配置
    1、引入pom依赖 org.springframework.boot spring-boot-starter-data-redis 2、增加配置类 RedisCo...
    99+
    2023-08-23
    redis spring boot java
  • Springboot redis整合配置的方法
    本文小编为大家详细介绍“Springboot redis整合配置的方法”,内容详细,步骤清晰,细节处理妥当,希望这篇“Springboot redis整合配置的方法”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。1...
    99+
    2023-06-19
  • SpringBoot整合Redis
    SpringBoot中的Redis 在 SpringBoot2.x 之后,原来使用的jedis被替换为了lettuce jedis : 采用的直连,多个线程操作的话,是不安全的,如果想要避免不安全的,使用 jedis pool 连接 池! ...
    99+
    2023-09-07
    redis spring boot java
  • ElasticSearch整合SpringBoot搭建配置
    目录前言项目搭建配置客户端索引API初探 & Index APIping创建索引 & create索引是否存在 & exist删除索引结束语前言 目前正在出...
    99+
    2023-02-22
    ElasticSearch整合SpringBoot ElasticSearch SpringBoot
  • Springboot整合redis步骤
    一、加入依赖 com.github.spt-oss spring-boot-starter-data-redis 2.0.7.0 redis依赖二、添加redis.properties配置文件 # REDIS...
    99+
    2017-06-25
    Springboot整合redis步骤
  • springboot如何整合Redis
    这篇文章主要介绍springboot如何整合Redis,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!引入依赖:在pom文件中添加redis依赖:<dependency>   ...
    99+
    2023-06-19
  • springboot2.5.0和redis整合配置详解
    目录基本概况为什么使用缓存Redis缓存下载Redis1. pom添加依赖2. application.properties 配置文件3. RedisConfig.java 配置类4...
    99+
    2022-11-12
  • springboot整合seata的配置过程
    前言: 小编引入的图片和文字描述都是来自于尚硅谷的视频讲解,在此感谢尚硅谷的老师,同时也结合 seata文档官方文档进行整合项目地址(gitee): https://gitee.co...
    99+
    2022-11-12
  • springboot整合druid及配置依赖
    目录Druid简介配置依赖基本-配置信息扩展-配置 druid 监控功能Druid简介 Java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池。 ...
    99+
    2022-11-12
  • SpringBoot整合Log4j2及配置步骤
    目录SpringBoot整合Log4j2以及配置详解1.加入依赖2.在src.java.main.resources目录下创建log4j2.xml文件 log4j2.xml文件内容如...
    99+
    2023-01-17
    SpringBoot配置Log4j2 SpringBoot整合Log4j2
  • 使用SpringBoot中整合Redis
    目录SpringBoot中整合RedisSpringBoot整合Redis改不了database问题SpringBoot中整合Redis 本次,我们以IDEA + SpringBoo...
    99+
    2022-11-13
  • SpringBoot如何整合Druid、Redis
    这篇文章主要介绍SpringBoot如何整合Druid、Redis,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!1.整合Druid1.1Druid简介Java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,...
    99+
    2023-06-29
  • SpringBoot中怎么整合Redis
    SpringBoot中怎么整合Redis,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。一、安装首先要在本地安装一个redis程序,安装过程十分简单(略过),安装完成后进入到...
    99+
    2023-06-16
  • 使用Springboot整合Apollo配置中心
    Apollo简介 Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理...
    99+
    2022-11-12
  • 介绍SpringBoot 整合 Redis 缓存
    无法支撑这么大的数据访问量,redis使用的时候可以单独利用客户端引入jar包操作即可,实际项目中都是和框架进行整合的比较多,此处演示利用springboot整合1.首先导入使用Maven导入jar包...
    99+
    2023-06-02
  • SpringBoot中如何整合Lettuce redis
    这篇文章主要介绍“SpringBoot中如何整合Lettuce redis”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“SpringBoot中如何整合Lettuce redis”文章能帮助大家解决问...
    99+
    2023-06-08
  • SpringBoot整合Redis之编写RedisConfig
    编写RedisConfig首先我们要明白RedisConfig中需要包含什么,首先看看我们直接使用RedisTemplate的问题,我们就知道RedisConfig要包含什么了,我们...
    99+
    2022-11-13
  • SpringBoot怎么整合Redis缓存
    SpringBoot怎么整合Redis缓存?针对这个问题,今天小编总结了这篇文章,希望能帮助更多想解决这个问题的朋友找到更加简单易行的办法。1、引入缓存依赖<dependency> &...
    99+
    2022-10-18
  • SpringBoot整合Redis案例分析
    这篇文章主要介绍了SpringBoot整合Redis案例分析的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringBoot整合Redis案例分析文章都会有所收获,下面我们一起来看看吧。Springboot整...
    99+
    2023-06-19
  • springboot整合druid及多数据源配置
    前言 本篇主要分两部分 ①springboot整合druid的代码配置,以及druid的监控页面演示;②对实际场景中多数据源的配置使用进行讲解。 一、springboot整合druid的演示demo 可以用idea快速生成一个可运行的dem...
    99+
    2023-10-24
    spring boot java spring
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作