spring boot 结合Redis 实现工具类

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: 自己整理了 spring boot 结合 Redis 的工具类引入依赖    org.springframework.boot    spring-boot-starter-data-redis加入配置# Redis数据库索引(默认为0)spring.

自己整理了 spring boot 结合 Redis 的工具类

引入依赖

<dependency>

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

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

</dependency>

加入配置

# Redis数据库索引(默认为0)

spring.redis.database=0

# Redis服务器地址

spring.redis.host=localhost

# Redis服务器连接端口

spring.redis.port=6379

 推荐一个交流学习群:614478470 里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

点击:加入

实现代码

这里用到了 静态类工具类中 如何使用 @Autowired

package com.lmxdawn.api.common.utils;

import java.util.Collection;

import java.util.Set;

import java.util.concurrent.TimeUnit;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.stereotype.Component;

/**

* 缓存操作类

*/

@Component

public class CacheUtils {

    @Autowired

    private RedisTemplate<String, String> redisTemplate;

    // 维护一个本类的静态变量

    private static CacheUtils cacheUtils;

    @PostConstruct

    public void init() {

        cacheUtils = this;

        cacheUtils.redisTemplate = this.redisTemplate;

    }

    /**

    * 将参数中的字符串值设置为键的值,不设置过期时间

    * @param key

    * @param value 必须要实现 Serializable 接口

    */

    public static void set(String key, String value) {

        cacheUtils.redisTemplate.opsForValue().set(key, value);

    }

    /**

    * 将参数中的字符串值设置为键的值,设置过期时间

    * @param key

    * @param value 必须要实现 Serializable 接口

    * @param timeout

    */

    public static void set(String key, String value, Long timeout) {

        cacheUtils.redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);

    }

    /**

    * 获取与指定键相关的值

    * @param key

    * @return

    */

    public static Object get(String key) {

        return cacheUtils.redisTemplate.opsForValue().get(key);

    }

    /**

    * 设置某个键的过期时间

    * @param key 键值

    * @param ttl 过期秒数

    */

    public static boolean expire(String key, Long ttl) {

        return cacheUtils.redisTemplate.expire(key, ttl, TimeUnit.SECONDS);

    }

    /**

    * 判断某个键是否存在

    * @param key 键值

    */

    public static boolean hasKey(String key) {

        return cacheUtils.redisTemplate.hasKey(key);

    }

    /**

    * 向集合添加元素

    * @param key

    * @param value

    * @return 返回值为设置成功的value数

    */

    public static Long sAdd(String key, String... value) {

        return cacheUtils.redisTemplate.opsForSet().add(key, value);

    }

    /**

    * 获取集合中的某个元素

    * @param key

    * @return 返回值为redis中键值为key的value的Set集合

    */

    public static Set<String> sGetMembers(String key) {

        return cacheUtils.redisTemplate.opsForSet().members(key);

    }

    /**

    * 将给定分数的指定成员添加到键中存储的排序集合中

    * @param key

    * @param value

    * @param score

    * @return

    */

    public static Boolean zAdd(String key, String value, double score) {

        return cacheUtils.redisTemplate.opsForZSet().add(key, value, score);

    }

    /**

    * 返回指定排序集中给定成员的分数

    * @param key

    * @param value

    * @return

    */

    public static Double zScore(String key, String value) {

        return cacheUtils.redisTemplate.opsForZSet().score(key, value);

    }

    /**

    * 删除指定的键

    * @param key

    * @return

    */

    public static Boolean delete(String key) {

        return cacheUtils.redisTemplate.delete(key);

    }

    /**

    * 删除多个键

    * @param keys

    * @return

    */

    public static Long delete(Collection<String> keys) {

        return cacheUtils.redisTemplate.delete(keys);

    }

}

推荐一个交流学习群,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

点击:加入

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
1天前
|
JSON NoSQL Java
深入浅出Redis(十三):SpringBoot整合Redis客户端
深入浅出Redis(十三):SpringBoot整合Redis客户端
|
10天前
|
缓存 NoSQL Java
springboot业务开发--springboot集成redis解决缓存雪崩穿透问题
该文介绍了缓存使用中可能出现的三个问题及解决方案:缓存穿透、缓存击穿和缓存雪崩。为防止缓存穿透,可校验请求数据并缓存空值;缓存击穿可采用限流、热点数据预加载或加锁策略;缓存雪崩则需避免同一时间大量缓存失效,可设置随机过期时间。文章还提及了Spring Boot中Redis缓存的配置,包括缓存null值、使用前缀和自定义过期时间,并提供了改造代码以实现缓存到期时间的个性化设置。
|
11天前
|
存储 NoSQL Java
Spring Boot与Redis:整合与实战
【4月更文挑战第29天】Redis,作为一个高性能的键值存储数据库,广泛应用于缓存、消息队列、会话存储等多种场景中。在Spring Boot应用中整合Redis可以显著提高数据处理的效率和应用的响应速度。
26 0
QGS
|
17天前
|
NoSQL 关系型数据库 MySQL
手拉手Springboot+RocketMQ+Redis抢单实现10W级QPS
手拉手Springboot+RocketMQ+Redis抢单实现10W级QPS
QGS
32 3
|
22天前
|
NoSQL 数据可视化 Java
Springboot整合redis
Springboot整合redis
|
22天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
29 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
|
22天前
|
人工智能 前端开发 Java
Java语言开发的AI智慧导诊系统源码springboot+redis 3D互联网智导诊系统源码
智慧导诊解决盲目就诊问题,减轻分诊工作压力。降低挂错号比例,优化就诊流程,有效提高线上线下医疗机构接诊效率。可通过人体画像选择症状部位,了解对应病症信息和推荐就医科室。
182 10
|
存储 SQL 消息中间件
springboot整合redis
redis是一个支持key-value的数据库,数据全部在内存中处理,在在一定时间间隔中将数据固化到磁盘。因为是内存操作,所以速度特别快。(这里我们主要介绍redis作为缓存使用)
177 0
springboot整合redis
|
存储 缓存 NoSQL
SpringBoot整合Redis
SpringBoot整合Redis
335 0
SpringBoot整合Redis