Redis概述

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

Redis是一个开源的、高性能的键值存储数据库系统,主要用于缓存数据和实现分布式数据结构,如发布订阅模式、列表、集合和有序集合等。它特别适合于需要快速读写操作的场景,例如Web应用中的会话管理、排行榜等。 **环境搭建步骤:** 1. **下载安装包**: 根据你的操作系统(Windows, Linux或Mac),访问Redis官网(https://redis.io/download)下载适合的版本。推荐选择稳定版,如果是开发测试,也可以尝试最新的开发分支。 2. **解压并配置**: 解压缩下载的文件,然后进入目录,找到`redis.conf`文件。这是一个配置文件,你可以修改其中的设置来适应你的需求,比如端口、最大内存、密码保护等。 3. **启动服务**: 运行`redis-server`命令(在Linux/Mac上可能是`redis-server redis.conf`),按照提示启动服务。首次运行可能会自动创建默认的数据目录。 4. **验证安装**: 打开一个新的终端窗口,使用`redis-cli`工具连接到服务器,输入`INFO`命令查看服务器状态,确保安装成功。 5. **安全考虑**: 如果在生产环境中,强烈建议启用密码认证,可以通过编辑`redis.conf`设置`requirepass`选项来添加密码。 **相关问题--:** 1. Redis的主要用途是什么? 2. 如何通过命令行管理Redis? 3. 如何在配置文件中设置密码保护? 4. 开发环境中如何优雅地停止和重启Redis服务?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值