怎么在spring boot中使用Redis实现工具类

发布时间:2021-05-21 16:11:57 作者:Leah
来源:亿速云 阅读:156

本篇文章为大家展示了怎么在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

实现代码

这里用到了 静态类工具类中 如何使用 @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);
  }
}

springboot是什么

springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。

上述内容就是怎么在spring boot中使用Redis实现工具类,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. spring-boot之一:简单入门
  2. Spring boot快速的配置多个Redis数据源的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot boot redis

上一篇:如何在Spring Data中操作MongoDB 数据库

下一篇:怎么在Spring mvc中防止数据重复提交

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》