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

从0开始搭建一个生产级SpringBoot2.0.X项目(八)SpringBoot 使用Redis

 前言

最近有个想法想整理一个内容比较完整springboot项目初始化Demo。

SpringBoot使用Redis 缓存数据

一、 pom引入依赖

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

二、 application-dev.yaml 增加redis 连接配置

spring:datasource:driver-class-name: oracle.jdbc.OracleDriverurl: jdbc:oracle:thin:@//127.0.0.1:1521/orcl2username: murg_demopassword: 654321hikari:connection-test-query: SELECT 1 FROM DUALminimum-idle: 10maximum-pool-size: 50idle-timeout: 1200000max-lifetime: 1800000connection-timeout: 180000cache:redis:time-to-live: 1800sredis:host: 127.0.0.1port: 6379password: adminlogging:level:org.springframework.security: debug

三、创建RedisConfig类,规定序列化格式

package com.murg.bootdemo.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.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {//redisTemplate注入到Spring容器@Beanpublic RedisTemplate<String,String> redisTemplate(RedisConnectionFactory factory){RedisTemplate<String,String> redisTemplate=new RedisTemplate<>();//‌自定义序列化方式使用String序列化‌:可以简化操作并提高查找效率。RedisSerializer<String> redisSerializer = new StringRedisSerializer();redisTemplate.setConnectionFactory(factory);//key序列化redisTemplate.setKeySerializer(redisSerializer);//value序列化redisTemplate.setValueSerializer(redisSerializer);//value hashmap序列化redisTemplate.setHashKeySerializer(redisSerializer);//key hashmap序列化redisTemplate.setHashValueSerializer(redisSerializer);return redisTemplate;}
}

四、创建RedisUtils 工具类

package com.murg.bootdemo.util;import com.google.common.collect.HashMultimap;
import org.springframework.context.annotation.Bean;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.TimeUnit;/*** @description: Redis工具类*/
@Component
public class RedisUtils {private  static StringRedisTemplate redisTemplate;public RedisUtils(StringRedisTemplate redisTemplate) {this.redisTemplate = redisTemplate;}/*** 写入缓存** @param key   redis键* @param value redis值* @return 是否成功*/public static boolean set(final String key, String value) {boolean result = false;try {ValueOperations<String, String> operations = redisTemplate.opsForValue();operations.set(key, value);result = true;} catch (Exception e) {e.printStackTrace();}return result;}/*** 写入缓存设置时效时间** @param key   redis键* @param value redis值* @return 是否成功*/public static boolean set(final String key, String value, Long expireTime) {boolean result = false;try {ValueOperations<String, String> operations = redisTemplate.opsForValue();operations.set(key, value);redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);result = true;} catch (Exception e) {e.printStackTrace();}return result;}/*** 批量删除对应的键值对** @param keys Redis键名数组*/public static void removeByKeys(final String... keys) {for (String key : keys) {remove(key);}}/*** 批量删除Redis key** @param pattern 键名包含字符串(如:myKey*)*/public static void removePattern(final String pattern) {Set<String> keys = redisTemplate.keys(pattern);if (keys != null && keys.size() > 0)redisTemplate.delete(keys);}/*** 删除key,也删除对应的value** @param key Redis键名*/public static void remove(final String key) {if (exists(key)) {redisTemplate.delete(key);}}/*** 判断缓存中是否有对应的value** @param key Redis键名* @return 是否存在*/public static Boolean exists(final String key) {return redisTemplate.hasKey(key);}/*** 读取缓存** @param key Redis键名* @return 是否存在*/public static String get(final String key) {String result = null;ValueOperations<String, String> operations = redisTemplate.opsForValue();result = operations.get(key);return result;}/*** 哈希 添加** @param key     Redis键* @param hashKey 哈希键* @param value   哈希值*/public static void hmSet(String key, String hashKey, String value) {HashOperations<String, String, String> hash = redisTemplate.opsForHash();hash.put(key, hashKey, value);}/*** 哈希获取数据** @param key     Redis键* @param hashKey 哈希键* @return 哈希值*/public static String hmGet(String key, String hashKey) {HashOperations<String, String, String> hash = redisTemplate.opsForHash();return hash.get(key, hashKey);}/*** 判断hash是否存在键** @param key     Redis键* @param hashKey 哈希键* @return 是否存在*/public static boolean hmHasKey(String key, String hashKey) {HashOperations<String, String, String> hash = redisTemplate.opsForHash();return hash.hasKey(key, hashKey);}/*** 删除hash中一条或多条数据** @param key      Redis键* @param hashKeys 哈希键名数组* @return 删除数量*/public static long hmRemove(String key, String... hashKeys) {HashOperations<String, String, String> hash = redisTemplate.opsForHash();return hash.delete(key, hashKeys);}/*** 获取所有哈希键值对** @param key Redis键名* @return 哈希Map*/public static Map<String, String> hashMapGet(String key) {HashOperations<String, String, String> hash = redisTemplate.opsForHash();return hash.entries(key);}/*** 保存Map到哈希** @param key Redis键名* @param map 哈希Map*/public static void hashMapSet(String key, Map<String, String> map) {HashOperations<String, String, String> hash = redisTemplate.opsForHash();hash.putAll(key, map);}/*** 列表-追加值** @param key   Redis键名* @param value 列表值*/public static void lPush(String key, String value) {ListOperations<String, String> list = redisTemplate.opsForList();list.leftPush(key, value);}/*** 列表-删除值** @param key   Redis键名* @param value 列表值*/public static void lRemove(String key, String value) {ListOperations<String, String> list = redisTemplate.opsForList();list.remove(key, 0, value);}/*** 列表-获取指定范围数据** @param key   Redis键名* @param start 开始行号(start:0,end:-1查询所有值)* @param end   结束行号* @return 列表*/public static List<String> lRange(String key, long start, long end) {ListOperations<String, String> list = redisTemplate.opsForList();return list.range(key, start, end);}/*** 集合添加** @param key   Redis键名* @param value 值*/public static void add(String key, String value) {SetOperations<String, String> set = redisTemplate.opsForSet();set.add(key, value);}/*** 集合获取** @param key Redis键名* @return 集合*/public static Set<String> setMembers(String key) {SetOperations<String, String> set = redisTemplate.opsForSet();return set.members(key);}/*** 有序集合添加** @param key   Redis键名* @param value 值* @param score 排序号*/public static void zAdd(String key, String value, double score) {ZSetOperations<String, String> zSet = redisTemplate.opsForZSet();zSet.add(key, value, score);}/*** 有序集合-获取指定范围** @param key        Redis键* @param startScore 开始序号* @param endScore   结束序号* @return 集合*/public static Set<String> rangeByScore(String key, double startScore, double endScore) {ZSetOperations<String, String> zset = redisTemplate.opsForZSet();return zset.rangeByScore(key, startScore, endScore);}/*** 模糊查询Redis键名** @param pattern 键名包含字符串(如:myKey*)* @return 集合*/public static Set<String> keys(String pattern) {return redisTemplate.keys(pattern);}/*** 获取多个hashMap** @param keySet* @return List<Map < String, String>> hashMap列表*/public static List hashMapList(Collection<String> keySet) {return redisTemplate.executePipelined(new SessionCallback<String>() {@Overridepublic <K, V> String execute(RedisOperations<K, V> operations) throws DataAccessException {HashOperations hashOperations = operations.opsForHash();for (String key : keySet) {hashOperations.entries(key);}return null;}});}/*** 保存多个哈希表(HashMap)(Redis键名可重复)** @param batchMap Map<Redis键名,Map<键,值>>*/public static void batchHashMapSet(HashMultimap<String, Map<String, String>> batchMap) {// 设置5秒超时时间redisTemplate.expire("max", 25, TimeUnit.SECONDS);redisTemplate.executePipelined(new RedisCallback<List<Map<String, String>>>() {@Overridepublic List<Map<String, String>> doInRedis(RedisConnection connection) throws DataAccessException {Iterator<Map.Entry<String, Map<String, String>>> iterator = batchMap.entries().iterator();while (iterator.hasNext()) {Map.Entry<String, Map<String, String>> hash = iterator.next();// 哈希名,即表名byte[] hashName = redisTemplate.getStringSerializer().serialize(hash.getKey());Map<String, String> hashValues = hash.getValue();Iterator<Map.Entry<String, String>> it = hashValues.entrySet().iterator();// 将元素序列化后缓存,即表的多条哈希记录Map<byte[], byte[]> hashes = new HashMap<byte[], byte[]>();while (it.hasNext()) {// hash中一条key-value记录Map.Entry<String, String> entry = it.next();byte[] key = redisTemplate.getStringSerializer().serialize(entry.getKey());byte[] value = redisTemplate.getStringSerializer().serialize(entry.getValue());hashes.put(key, value);}// 批量保存connection.hMSet(hashName, hashes);}return null;}});}/*** 保存多个哈希表(HashMap)(Redis键名不可以重复)** @param dataMap Map<Redis键名,Map<哈希键,哈希值>>*/public static void batchHashMapSet(Map<String, Map<String, String>> dataMap) {// 设置5秒超时时间redisTemplate.expire("max", 25, TimeUnit.SECONDS);redisTemplate.executePipelined(new RedisCallback<List<Map<String, String>>>() {@Overridepublic List<Map<String, String>> doInRedis(RedisConnection connection) throws DataAccessException {Iterator<Map.Entry<String, Map<String, String>>> iterator = dataMap.entrySet().iterator();while (iterator.hasNext()) {Map.Entry<String, Map<String, String>> hash = iterator.next();// 哈希名,即表名byte[] hashName = redisTemplate.getStringSerializer().serialize(hash.getKey());Map<String, String> hashValues = hash.getValue();Iterator<Map.Entry<String, String>> it = hashValues.entrySet().iterator();// 将元素序列化后缓存,即表的多条哈希记录Map<byte[], byte[]> hashes = new HashMap<byte[], byte[]>();while (it.hasNext()) {// hash中一条key-value记录Map.Entry<String, String> entry = it.next();byte[] key = redisTemplate.getStringSerializer().serialize(entry.getKey());byte[] value = redisTemplate.getStringSerializer().serialize(entry.getValue());hashes.put(key, value);}// 批量保存connection.hMSet(hashName, hashes);}return null;}});}/*** 保存多个哈希表(HashMap)列表(哈希map的Redis键名不能重复)** @param list Map<Redis键名,Map<哈希键,哈希值>>* @see RedisUtils*.batchHashMapSet()**/public static void batchHashMapListSet(List<Map<String, Map<String, String>>> list) {// 设置5秒超时时间redisTemplate.expire("max", 25, TimeUnit.SECONDS);redisTemplate.executePipelined(new RedisCallback<List<Map<String, String>>>() {@Overridepublic List<Map<String, String>> doInRedis(RedisConnection connection) throws DataAccessException {for (Map<String, Map<String, String>> dataMap : list) {Iterator<Map.Entry<String, Map<String, String>>> iterator = dataMap.entrySet().iterator();while (iterator.hasNext()) {Map.Entry<String, Map<String, String>> hash = iterator.next();// 哈希名,即表名byte[] hashName = redisTemplate.getStringSerializer().serialize(hash.getKey());Map<String, String> hashValues = hash.getValue();Iterator<Map.Entry<String, String>> it = hashValues.entrySet().iterator();// 将元素序列化后缓存,即表的多条哈希记录Map<byte[], byte[]> hashes = new HashMap<byte[], byte[]>();while (it.hasNext()) {// hash中一条key-value记录Map.Entry<String, String> entry = it.next();byte[] key = redisTemplate.getStringSerializer().serialize(entry.getKey());byte[] value = redisTemplate.getStringSerializer().serialize(entry.getValue());hashes.put(key, value);}// 批量保存connection.hMSet(hashName, hashes);}}return null;}});}}

五、创建接口测试

TestRestController增加接口/testredis
    @RequestMapping(value = "/testredis", method = RequestMethod.GET)public WebResult testRedis() {RedisUtils.set("test","aaaaa");return WebResult.ok(RedisUtils.get("test"));}


http://www.mrgr.cn/news/65170.html

相关文章:

  • Vue:模板 MVVM
  • HarmonyOS 私仓搭建
  • 解决Redis缓存穿透(缓存空对象、布隆过滤器)
  • 微信支付接口变心了:暂不支持该类型商户号绑定本AppID,请点击“拒绝”结束该流程,点此查看详细说明。如有疑问,可咨询客服
  • 数据库_SQLite3
  • vue3常用指令
  • 【C++动态规划】2435. 矩阵中和能被 K 整除的路径|1951
  • 【天线&空中农业】草莓果实检测系统源码&数据集全套:改进yolo11-HSFPN
  • 不得不会的 VSCode 快捷键!让你的工作效率快速提升
  • 【数据结构】Java 集合 Set 接口及其实现类的定义简介
  • ZFC in LEAN 之 等价关系(Equivalence Relation)及前集的等价关系(Equivalence in Pre-Set)
  • 2024 AFS-47 电子数据功能性鉴定(移动终端 APP)(2024能力验证)
  • 深入理解 Java 中的 TreeSet 集合
  • HTML5和CSS3 介绍
  • 在线预览 Word 文档
  • 【基于轻量型架构的WEB开发】课程 作业2 mybatis关联查询、缓存、注解
  • R语言贝叶斯:INLA下的贝叶斯回归、生存分析、随机游走、广义可加模型、极端数据的贝叶斯分析
  • 华为OD机试 - 连续天数的最高利润额 - 动态规划(Python/JS/C/C++ 2024 C卷 100分)
  • R 语言科研配色 --- 第 9 期
  • 循环
  • 三大细分领域入选,九州未来再登2024边缘计算产业图谱
  • ​​​​​​​PHP类型比较
  • SAP ABAP开发学习——接口中间件(PI)
  • 初步学习【因果推断】
  • 【C++】如何让C++字符串更快、C++的小字符串优化
  • 「Mac畅玩鸿蒙与硬件23」鸿蒙UI组件篇13 - 自定义组件的创建与使用