服务器之家

服务器之家 > 正文

spring boot结合Redis实现工具类的方法示例

时间:2021-06-17 11:02     来源/作者:兰茗翔

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

引入依赖

?
1
2
3
4
<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-data-redis</artifactid>
</dependency>

加入配置

?
1
2
3
4
5
6
# redis数据库索引(默认为0
spring.redis.database=0
# redis服务器地址
spring.redis.host=localhost
# redis服务器连接端口
spring.redis.port=6379

实现代码

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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);
  }
}

相关地址

github 地址:https://github.com/lmxdawn/vue-admin-java

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://segmentfault.com/a/1190000017131552

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部