Redis概述

本文详细介绍SpringBoot结合Redis的配置与使用,包括在pom文件中添加依赖、配置application.yml、整合Redis实现缓存操作及使用Spring缓存注解进行高效数据存取。通过具体代码示例,展示如何实现数据的缓存、更新与删除。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 概述

Redis【入门】就这一篇!
https://www.jianshu.com/p/56999f2b8e3b

redis 在windows 上启动等常用操作
https://blog.csdn.net/luuvyjune/article/details/81016295

基于token的登录管理(多设备登录、单设备登录)
https://blog.csdn.net/xxssyyyyssxx/article/details/83743739

2. Redis配置文件

在这里插入图片描述
redis.properties

redis.host=127.0.0.1
redis.port=6379
#redis.password=123456
redis.maxIdle=100
redis.maxActive=300
redis.maxWait=1000
redis.testOnBorrow=true
redis.timeout=100000

spring-redis.xml

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:util="http://www.springframework.org/schema/util"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:task="http://www.springframework.org/schema/task" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util-4.3.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <!-- 引入配置文件 -->  
    <bean id="redisPropertyConfigurer"  
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="location" value="classpath:redis.properties" />  
        <property name="ignoreUnresolvablePlaceholders" value="true" />   
    </bean>  
  
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>
    
    <!-- redis服务器中心 -->
    <bean id="connectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="poolConfig" ref="poolConfig" />
        <property name="port" value="${redis.port}" />
        <property name="hostName" value="${redis.host}" />
    <!--     <property name="password" value="${redis.password}" /> -->
        <property name="timeout" value="${redis.timeout}"></property>
        <property name="database" value="2"/>
    </bean>
    
    <!-- 复杂对象处理 -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <!--存储string时key需要的序列化配置-->
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
        <!--存储Map时key需要的序列化配置 (set hash zset list) -->
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <!--存储Map时value需要的序列化配置-->
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
    </bean>
    
    <!-- 字符串处理 -->
   <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="connectionFactory"></property>
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
        </property>
        <!--存储Map时key需要的序列化配置 (set hash zset list) -->
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <!--存储Map时value需要的序列化配置-->
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
        <property name="stringSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
    </bean>
    
    <bean id="redisUtil" class="com.zhiruan.util.redis.RedisUtil">
        <property name="redisTemplate" ref="redisTemplate" />
    </bean>
    
    <bean id="stringRedisUtil" class="com.zhiruan.util.redis.StringRedisUtil">
        <property name="stringRedisTemplate" ref="stringRedisTemplate" />
    </bean>
</beans>

web.xml

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-mybatis.xml,classpath:spring-redis.xml</param-value>
</context-param>

3.springboot结合redis

IDEA搭建SpringBoot+Mybatis+Redis
SpringBoot+MySQL+MyBatis+druid+redis整合 实现功能:初始化商品列表从数据库读取加载至redis中
如何spingboot中简单的使用redis缓存存取和查询数据
Docker 安装 Redis + Spring Boot 整合 Redis
Spring Boot 整合 Redis 实现缓存操作
springboot整合redis缓存的增删改查
SpringBoot学习——springboot整合Redis实现数据缓存

4. 如何spingboot中简单的使用redis缓存存取和查询数据

1.在pom文件中添加依赖

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

2.在application.yml文件中添加redis的配置
在这里插入图片描述
3.在service层中注入redisTemplate
没加缓存时候的代码:

   /**
     * 根据ID查询实体
     * @param id
     * @return
     */
    public Article findById(String id) {
        return articleDao.findById(id).get();
    }

加了缓存之后的代码:

 /**
     * 根据ID查询实体
     * @param id
     * @return
     */
    public Article findById(String id) {
        //先从缓存中查询当前对象
        Article article = (Article)redisTemplate.opsForValue().get("article_"+id);
        //如果没有取到,就从数据库从查询,然后存入缓存中
        if(article == null){
            article = articleDao.findById(id).get();
            redisTemplate.opsForValue().set("article_"+id,article);
        }
        return article;
    }

第一次查询在控制台会打印出相应的sql语句,第二次查询就不会打印了,这就说明第二次没有访问数据库而是访问的缓存

在这里插入图片描述
4.在修改和删除的方法中要添加上删除redis中数据的方法

/**
 * 修改
 * @param article
 */
public void update(Article article) {
    redisTemplate.delete("article_"+ article.getId());
    articleDao.save(article);
}

/**
 * 删除
 * @param id
 */
public void deleteById(String id) {
    redisTemplate.delete("article_"+ id);
    articleDao.deleteById(id);
}

5. SpringBoot整合Redis并使用Spring缓存注解进行缓存操作

https://blog.csdn.net/justry_deng/article/details/89285317
SpringBoot使用Spring缓存注解
https://blog.csdn.net/justry_deng/article/details/89283664
springboot+redis实现缓存数据

https://www.cnblogs.com/hhhshct/p/9002648.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值