当前位置: 首页 > news >正文

[ Redis ] SpringBoot集成使用Redis(补充)

目录

四. springBoot 集成使用 redis

(1). 概述

(2). 搭建

1. 添加 redis 依赖

2. 配置连接 redis

3. 注入 RedisTemplate

4. 测试

5. 后端测试redis


四. springBoot 集成使用 redis

(1). 概述

         Jedis 是 Redis 官方推出的一款面向 Java 的客户端,提供了很多接口供 Java 语言调用。可以在 Redis 官网下载.

         Spring-data-redis 是 spring 大家族的一部分 ,提供了在 srping 应用中通过简单的配置访问 redis 服务,对 reids 底层开发包(Jedis, JRedis, and RJC)进行了高度封装,RedisTemplate 提供了 redis 各种操作.

spring-data-redis 针对 Redis 提供了如下功能:

1.连接池自动管理,提供了一个高度封装的“RedisTemplate”类.

2.针对 jedis 客户端中大量 api 进行了归类封装,将同一类型操作封装为operation 接口.

ValueOperations:简单 K-V 操作

SetOperations:set 类型数据操作

ZSetOperations:zset 类型数据操作

HashOperations:针对 map 类型的数据操作

ListOperations:针对 list 类型的数据操作

3.将事务操作封装,有容器控制。

4.针对数据的“序列化/反序列化”,提供了多种可选择策略(RedisSerializer)

       JdkSerializationRedisSerializer:POJO 对象的存取场景,使用 JDK 本身序列化机制.

       StringRedisSerializer:Key 或者 value 为字符串的场景,根据指定的charset 对数据的字节序列编码成 string,是“new String(bytes, charset)”和 “string.getBytes(charset)”的直接封装。是最轻量级和高效的策略。

       JacksonJsonRedisSerializer:jackson-json 工具提供了 javabean 与 json之间的转换能力,可以将 pojo 实例序列化成 json 格式存储在 redis 中,也可以将 json 格式的数据转换成 pojo 实例。

(2). 搭建

1. 添加 redis 依赖

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-redis</artifactId>

</dependency>

2. 配置连接 redis

spring:

   redis:

      host: 192.168.31.100

      port: 6379

      password: 111

      database: 0

      pool:

          max-active: 8 # 连接池最大连接数(使用负值表示没有限制)

          max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)

          max-idle: 8 # 连接池中的最大空闲连接

          min-idle: 0 # 连接池中的最小空闲连接

          timeout: 5000ms # 连接超时时间(毫秒)

3. 注入 RedisTemplate

@Autowired

RedisTemplate redisTemplate;

4. 测试

redisTemplate.opsForValue().set("name", "aa");

redisTemplate.opsForValue().set("users", users,10*1000, TimeUnit.MILLISECONDS);

redisTemplate.hasKey("name");

redisTemplate.opsForValue().get("name");

redisTemplate.delete("users");序列化键值

redisTemplate.setKeySerializer(new StringRedisSerializer());

redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<User>(User.class));

需要被 Redis 缓存的类,必须实现序列化接口

 5. 后端测试redis

将上述命令转为后端的方法

package com.ffyc.news.web;import com.fasterxml.jackson.databind.ObjectMapper;
import com.ffyc.news.model.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.concurrent.TimeUnit;@RestController
@RequestMapping(path = "/redistest")
public class RedisTest {@Autowiredprivate RedisTemplate redisTemplate;@GetMapping(path = "testhash")public  void testHash(){HashOperations hashOperations = redisTemplate.opsForHash();hashOperations.put("newsid1","dz",10);//点赞hashOperations.put("newsid1","fw",20);//访问hashOperations.put("newsid1","sc",5);//收藏System.out.println(hashOperations.get("newsid","sc"));}@GetMapping(path = "/teststring")public  void  testString(){//序列化,也可以添加配置类进行序列化 省去每次都添加序列化代码//redisTemplate.setKeySerializer(new StringRedisSerializer());//key键进行序列化//redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));//value值进行序列化System.out.println(redisTemplate.delete("age"));//删除key键System.out.println(redisTemplate.hasKey("a"));//判断键key是否存在//获取String类型操作方法,  ValueOperations封装操作String类型的方法ValueOperations valueOperations = redisTemplate.opsForValue();valueOperations.set("k","kk");System.out.println(valueOperations.get("k"));valueOperations.set("count",0);valueOperations.increment("count");valueOperations.increment("count");valueOperations.increment("count");//自增valueOperations.decrement("count");//自减System.out.println(valueOperations.get("count"));Menu menu = new Menu();menu.setId(1);menu.setName("新闻管理");valueOperations.set("menu",menu,10, TimeUnit.MINUTES);//(键,对象,失效时间,时间单位)}
}

注意: 添加redis序列化 避免redis中的数据格式与Java传输的数据格式不一致,操作失败

package com.ffyc.news.config;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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
//redis序列化键值
@Configuration
public class RedisConfig {/*** 序列化键,值* @param connectionFactory* @return*/@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(connectionFactory);//序列化策略Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);StringRedisSerializer redisSerializer = new StringRedisSerializer();//key序列化为普通的redisTemplate.setKeySerializer(redisSerializer);//key 单一的字符串redisTemplate.setHashKeySerializer(redisSerializer);//将String,Hash结构值都序列化为json字符串redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);//value {}redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);return redisTemplate;}
}

如果开机redis没有启动,需要自己打开. 地址如下

D:\development\redis

http://www.dtcms.com/a/528817.html

相关文章:

  • GitHub等平台形成的开源文化正在重塑伊朗人
  • 贵州省建设厅网站造价工程信息网东港建站公司
  • UE5 蓝图-17:主 mainUI 界面蓝图,构成与尺寸分析;界面菜单栏里按钮 Ul_menuButtonsUl 蓝图的构成记录,
  • 公司企业网站免费建设网站建设需要技术
  • SQL MID() 函数详解
  • SQL187 每份试卷每月作答数和截止当月的作答总数。
  • 三河建设局网站做学校网站用什么模版
  • 装修网站建设服务商wordpress 编辑图片无法显示
  • 建设网站要求有哪些营销型网站建设搭建方法
  • jQuery noConflict() 方法详解
  • JavaScript 性能优化系列(六)接口调用优化 - 6.4 错误重试策略:智能重试机制,提高请求成功率
  • 绘画基础知识学习
  • 自己的服务器做网站要备案做网站用到ps么
  • 第 4 篇:SSM 分布式落地:状态持久化与并行状态(含 Redis/MySQL 实战)
  • STM32全栈智慧鱼缸——硬件选型、接线图、软件流程图与完整源码
  • 【11408学习记录】考研数学概率论攻坚:事件的独立性与独立重复试验核心精讲
  • linux下文件操作函数
  • 电商网站建设与维护意味着什么公众号登录怎么退出
  • 专业的营销型网站培训中心wordpress 美化网站
  • 【Java数据结构】——常见力扣题综合
  • 网站长期建设运营计划书江门营销网站建设
  • ProcDump 学习笔记(6.7):监视异常(未处理/首机会/消息过滤/进程终止)
  • C++编程实践——Linux下的CPU控制
  • NTRU 公钥加密系统详解
  • 深入浅出 VGGNet:经典卷积神经网络解析
  • 盐城整站优化柳州做网站去哪家公司好
  • 协程:实战与系统集成(高级篇)
  • 芯片验证基石UVM:高效验证的方法论与挑战
  • 旅游网站开发的作用seo快排技术教程
  • 3DS-GBA-GBC-NDS-switch梦可宝精灵游戏合集 -全汉化游戏