springboot+redis集成和redis工具类

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

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.stereotype.Component;

/**

  • redicache 工具类

*/
@SuppressWarnings(“unchecked”)
@Component
public class RedisUtil {
@SuppressWarnings(“rawtypes”)
@Autowired
private RedisTemplate redisTemplate;

/**
 * 批量删除对应的value
 * 
 * @param keys
 */
public void remove(final String... keys) {
	for (String key : keys) {
		remove(key);
	}
}

/**
 * 批量删除key
 * 
 * @param pattern
 */
public void removePattern(final String pattern) {
	Set<Serializable> keys = redisTemplate.keys(pattern);
	if (keys.size() > 0)
		redisTemplate.delete(keys);
}

public void removeByPrex(final String pattern) {
	Set<Serializable> keys = redisTemplate.keys(pattern + "*");
	if (keys.size() > 0)
		redisTemplate.delete(keys);
}

public void removeBySuffix(final String pattern) {
	Set<Serializable> keys = redisTemplate.keys("*" + pattern);
	if (keys.size() > 0)
		redisTemplate.delete(keys);
}

/**
 * 删除对应的value
 * 
 * @param key
 */
public void remove(final String key) {
	if (exists(key)) {
		redisTemplate.delete(key);
	}
}

/**
 * 判断缓存中是否有对应的value
 * 
 * @param key
 * @return
 */
public boolean exists(final String key) {
	return redisTemplate.hasKey(key);
}

/**
 * 读取缓存
 * 
 * @param key
 * @return
 */
public Object get(final String key) {
	Object result = null;
	ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
	result = operations.get(key);
	return result;
}

/**
 * 写入缓存
 * 
 * @param key
 * @param value
 * @return
 */
public boolean set(final String key, Object value) {
	boolean result = false;
	try {
		ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
		operations.set(key, value);
		result = true;
	} catch (Exception e) {
		e.printStackTrace();
	}
	return result;
}

/**
 * 写入缓存
 * 
 * @param key
 * @param value
 * @return
 */
public boolean set(final String key, Object value, Long expireTime) {
	boolean result = false;
	try {
		ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
		operations.set(key, value);
		redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
		result = true;
	} catch (Exception e) {
		e.printStackTrace();
	}
	return result;
}

    public boolean setHash(final String key, String hashKey,String value){
    	boolean result = false;
    	try {
    		HashOperations<String, String, String> operations = redisTemplate.opsForHash();
	        operations.put(key, hashKey, value);
	        result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return result;
        
    }
    public String getHash(final String key, String hashKey){
    	String value = null;
    	try {
    		HashOperations<String, String, String> operations = redisTemplate.opsForHash();
	        value = operations.get(key, hashKey);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return value;
    }
    
    public List<String> getHash(final String key){
    	List<String> list = null;
    	try {
    		HashOperations<String, String, String> operations = redisTemplate.opsForHash();
    		list = operations.values(key);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return list;
    }

}

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
List类型(list类型存入链表结构 取数据时可以按下标取值 redis中保存json类型)

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
@SuppressWarnings(“unchecked”)
@Repository
public class ListRedisTemplate {
@SuppressWarnings(“rawtypes”)
@Autowired
private RedisTemplate redisTemplate;

/** 
 *  
 * @param key 
 * @param value 
 * @return 
 */  
public <T> Long lpush(String key, T t) {  
    return redisTemplate.opsForList().leftPush(key, t);  
    
}  

/** 
 *  
 * @param key 
 * @return 
 */  
public <T> T lpop(String key) {  
    return (T) redisTemplate.opsForList().leftPop(key);  
}  

/** 
 *  
 * @param key 
 * @param value 
 * @return 
 */  
public <T> Long rpush(String key, T t) {  
    return redisTemplate.opsForList().rightPush(key, t);  
}  

/** 
 *  
 * @param key 
 * @return 
 */  
public <T> T rpush(String key) {  
    return (T) redisTemplate.opsForList().leftPop(key);  
}  

/** 
 *  
 * @param key 
 * @return 
 */  
public Long length(String key) {  
    return redisTemplate.opsForList().size(key);  
}  

/** 
 *  
 * @param key 
 * @param start 
 * @param end 
 * @return 
 */  
public <T> List<T> range(String key, int start, int end) {  
    return redisTemplate.opsForList().range(key, start, end);  
}  

/** 
 *  移除key中值为value的i个,返回删除的个数
 *  
 * @param key 
 * @param i 
 * @param value 
 */  
public Long remove(String key, long i, String value) {  
    return redisTemplate.opsForList().remove(key, i, value);  
}  

/** 
 * 检索 
 *  
 * @param key 
 * @param index 
 * @return 
 */  
public <T> T index(String key, long index) {  
    return (T) redisTemplate.opsForList().index(key, index);  
}  

/** 
 * 赋值 
 *  
 * @param key 
 * @param index 
 * @param value 
 */  
public <T> void set(String key, long index, T t) {  
    redisTemplate.opsForList().set(key, index, t);  
}  

/** 
 * 裁剪,删除除了[start,end]以外的所有元素
 *  
 * @param key 
 * @param start 
 * @param end 
 */  
public void trim(String key, long start, int end) {  
    redisTemplate.opsForList().trim(key, start, end);  
}  

}

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
set类型

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

@SuppressWarnings(“unchecked”)
@Repository
public class SetRedisTemplate {
@SuppressWarnings(“rawtypes”)
@Autowired
private RedisTemplate redisTemplate;

/**
 * 新增
 * @param key
 * @param t
 * @return
 */

public <T> Long add(String key, T... t) {  
    return redisTemplate.opsForSet().add(key, t);
}  


/**
 * 获取set
 * 
 * @param key
 * @return
 */
public <T> Set<T>  members(String key){
    return redisTemplate.opsForSet().members(key);
}

/**
 * 从redis的某个key下面的set集合中删除元素。
 * @param key
 * @return
 */
public <T>  Long remove(String key, T... t) {
	return redisTemplate.opsForSet().remove(key, t);
}

/**
 * 查看是否存在指定数据
 * @param key
 * @param t
 * @return
 */
public <T>  Boolean isMember(String key,T t){
    return redisTemplate.opsForSet().isMember(key, t);
}

public  Long size(String key){
    return redisTemplate.opsForSet().size(key);
}

/**
 * 移除随机
 * @param key
 * @return
 */
public <T> T pop(String key){
    return (T) redisTemplate.opsForSet().pop(key);
}

}

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
hash类型

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

@SuppressWarnings(“unchecked”)
@Repository
public class HashRedisTemplate {
@SuppressWarnings(“rawtypes”)
@Autowired
private RedisTemplate redisTemplate;

/**
 * 添加单个
 *
 * @param key    key
 * @param filed  filed
 * @param domain 对象
 */

public <T> void put(String key,String filed,T domain){
    redisTemplate.opsForHash().put(key, filed, domain);
}


/**
 * 添加HashMap
 *
 * @param key    key
 * @param hm    要存入的hash表
 */

public <T> void mput(String key, HashMap<String,T> hm){
    redisTemplate.opsForHash().putAll(key,hm);
}

/**
 * 查询key和field所确定的值
 *
 * @param key 查询的key
 * @param field 查询的field
 * @return HV
 */

public <T> T get(String key,String field) {
    return (T) redisTemplate.opsForHash().get(key, field);
}

/**
 * 查询该key下所有值
 *
 * @param key 查询的key
 * @return Map<HK, HV>
 */

public <T> Map<Object, T> mget(String key) {
    return  (Map<Object, T>) redisTemplate.opsForHash().entries(key);
}

/**
 * 删除key下所有值
 *
 * @param key 查询的key
 */

public void delete(String key) {
    redisTemplate.opsForHash().getOperations().delete(key);
}

/**
 * 判断key和field下是否有值
 *
 * @param key 判断的key
 * @param field 判断的field
 */

public Boolean hasKey(String key,String field) {
    return redisTemplate.opsForHash().hasKey(key,field);
}

/**
 * 判断key下是否有值
 *
 * @param key 判断的key
 */

public Boolean hasKey(String key) {
    return redisTemplate.opsForHash().getOperations().hasKey(key);
}

}

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

import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

@SuppressWarnings(“unchecked”)
@Repository
public class ValueRedisTemplate {
@SuppressWarnings(“rawtypes”)
@Autowired
private RedisTemplate redisTemplate;
// StringRedisTemplate stringRedisTemplate;

/**
 * 查询key,支持模糊查询
 *
 * @param key
 *            传过来时key的前后端已经加入了*,或者根据具体处理
 */

public <T> Set<T> keys(String key) {
	return redisTemplate.keys(key);
}

/**
 * 重命名key
 */

public <T> void renameKey(String key, T t) {
	redisTemplate.rename(key, t);
}

/**
 * 字符串添加信息
 * 
 * @param key
 * @param t
 *            可以是单个的值,也可以是任意类型的对象
 */

public <T> void set(String key, T t) {
	redisTemplate.opsForValue().set(key, t);
}

/**
 * 字符串添加信息
 * 
 * @param key
 * @param t
 *            可以是单个的值,也可以是任意类型的对象
 * @param expire
 *            设置失效时间
 */

public <T> void set(String key, T t, long expire) {
	redisTemplate.opsForValue().set(key, t, expire, TimeUnit.SECONDS);
}

/**
 * 字符串添加信息
 * 
 * @param key
 * @param t
 *            可以是单个的值,也可以是任意类型的对象
 * @return
 */

public <T> Boolean setIfAbsent(String key, T t) {
	return redisTemplate.opsForValue().setIfAbsent(key, t);
}

/**
 * 字符串获取值
 * 
 * @param key
 */

public <T> T get(String key) {
	return (T) redisTemplate.opsForValue().get(key);
}

public <T> T getAndSet(String key, T value) {
	return (T) redisTemplate.opsForValue().getAndSet(key, value);
}

public void delete(String key) {
	redisTemplate.opsForValue().getOperations().delete(key);
}

public Boolean expire(String key, long sec) {
	return redisTemplate.expire(key, sec, TimeUnit.SECONDS);
}

public Boolean expireAt(String key, Date date) {
	return redisTemplate.expireAt(key, date);
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜空繁星vv

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值