导入依赖:
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.5.6.RELEASE</version>
</dependency>
存储String数据类型接口:
package com.test.redis;
/**
* 封装redisTemplate对字符串处理接口
* @description
* @author Liuyh
* @date 2018年4月19日 上午10:15:30
*/
public interface IStringCache {
/**
* 如果key已经存在且是一个字符串,APPEND命令将value追加到原来的值的末尾. 如果key不存在,APPEND就简单的将给定key设置为value
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:11:28
* @param key
* @param value
* @return 追加value后,字符串的长度
*/
Integer stringAppendString(String key, String value);
/**
* 获取指定键的值
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:12:35
* @param key
* @return
*/
String stringGetStringByKey(String key);
/**
* 获取存储在键上的字符串的子字符串
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:12:43
* @param key
* @param start
* @param end
* @return 截取后的字符串
*/
String stringGetSubStringFromString(String key, long start, long end);
/**
* 将键的整数值按给定的长整型数值增加
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:13:03
* @param key
* @param delta
* @return 增长后的结果集
*/
Long stringIncrementLongString(String key, Long delta);
/**
* 键的整数值按给定的浮点型数值增加
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:13:32
* @param key
* @param delta
* @return 增长后的结果集
*/
Double stringIncrementDoubleString(String key, Double delta);
/**
* 设置指定键的值
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:13:53
* @param key
* @param value
*/
void stringSetString(String key, String value);
/**
* 设置键的字符串值并返回其旧值
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:14:03
* @param key
* @param value
* @return
*/
String stringGetAndSet(String key, String value);
/**
* 使用键和到期时间来设置值,时间单位默认为毫秒
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:14:22
* @param key
* @param value
* @param timeout
*/
void stringSetValueAndExpireTime(String key, String value, long timeout);
}
存储Set数据类型接口:
package com.test.redis;
import java.util.Set;
/**
* 封装redisTemplate对set处理接口
* @description
* @author Liuyh
* @date 2018年4月19日 上午10:16:13
*/
public interface ISetCache {
/**
* 将一个或多个 value 元素加入到集合 key 当中,已经存在于集合的 value 元素将被忽略。
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:10:39
* @param key
* @param values
* @return 被添加到集合中的新元素的数量,不包括被忽略的元素
*/
Long setAddSetMap(String key, String... values);
/**
* 获取set集合的大小
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:11:00
* @param key
* @return
*/
Long setGetSizeForSetMap(String key);
/**
* 获取set集合中的元素
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:11:06
* @param key
* @return
*/
Set<String> setGetMemberOfSetMap(String key);
/**
* 检查元素是不是set集合中的
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:11:12
* @param key
* @param o
* @return
*/
boolean setCheckIsMemberOfSet(String key, Object o);
}
存储List数据类型接口:
package com.test.redis;
import java.util.List;
/**
* 封装redisTemplate对list处理接口
* @description
* @author Liuyh
* @date 2018年4月19日 上午10:16:52
*/
public interface IListCache {
/**
* 从右向左存压栈
* @description
* @author Liuyh
* @date 2018年4月19日上午10:09:06
* @param key
* @param value
*/
void listRightPushList(String key, String value);
/**
* 从右出栈
* @description
* @author Liuyh
* @date 2018年4月19日上午10:09:21
* @param key
* @return
*/
String listRightPopList(String key);
/**
* 从左向右存压栈
* @description
* @author Liuyh
* @date 2018年4月19日上午10:09:28
* @param key
* @param value
*/
void listLeftPushList(String key, String value);
/**
* 从左出栈
* @description
* @author Liuyh
* @date 2018年4月19日上午10:09:35
* @param key
* @return
*/
String listLeftPopList(String key);
/**
* 集合list的长度
* @description
* @author Liuyh
* @date 2018年4月19日上午10:09:43
* @param key
* @return
*/
Long listSize(String key);
/**
* 查询列表 key 中指定区间内的元素,区间以偏移量 start 和 stop 指定。
* @description
* @author Liuyh
* @date 2018年4月19日上午10:09:49
* @param key
* @param start
* @param end
* @return
*/
List<String> listRangeList(String key, Long start, Long end);
/**
* 移除key中值为value的i个,返回删除的个数;如果没有这个元素则返回0
* @description
* @author Liuyh
* @date 2018年4月19日上午10:09:59
* @param key
* @param i
* @param value
* @return
*/
Long listRemoveFromList(String key, long i, Object value);
/**
* 根据下标查询list中某个值
* @description
* @author Liuyh
* @date 2018年4月19日上午10:10:05
* @param key
* @param index
* @return
*/
String listIndexFromList(String key, long index);
/**
* 根据下标设置value
* @description
* @author Liuyh
* @date 2018年4月19日上午10:10:20
* @param key
* @param index
* @param value
*/
void listSetValueToList(String key, long index, String value);
/**
* 裁剪(删除), 删除 除了[start,end]以外的所有元素
* @description
* @author Liuyh
* @date 2018年4月19日上午10:10:30
* @param key
* @param start
* @param end
*/
void listTrimByRange(String key, Long start, Long end);
}
存储Hash类型数据接口:
package com.test.redis;
import java.util.List;
import java.util.Map;
import java.util.Set;
public interface IHashCache {
/**
* 查看哈希表hKey中,给定域hashKey是否存在
*
* @description
* @author Liuyh
* @date 2018年4月19日上午9:56:56
* @param hKey
* @param hashKey
* @return
*/
boolean hashCheckHxists(String hKey, String hashKey);
/**
* 查看哈希表hKey中,给定域hashKey的值
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:00:03
* @param hKey
* @param hashKey
* @return
*/
<T> T hashGet(String hKey, String hashKey);
/**
* 获取所有的散列值
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:00:32
* @param key
* @return
*/
Map<Object, Object> hashGetAll(String key);
/**
* 哈希表hKey中的域hashKey的值加上增量delta
*
* @description 增量可以为负数 如果hKey不存在,会创建一个新的hash并执行HINCRBY命令.
* 如果hashKey不存在,会在执行前初始化一个0 如果hashKey的值时字符串,会报错
* 返回返回hKey中hashKey的当前值
* @author Liuyh
* @date 2018年4月19日上午10:02:18
* @param hKey
* @param hashKey
* @param delta
* @return
*/
Long hashIncrementLongOfHashMap(String hKey, String hashKey, Long delta);
/**
* 哈希表hKey中的域hashKey的值加上浮点型增量delta
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:05:09
* @param hKey
* @param hashKey
* @param delta
* @return
*/
Double hashIncrementDoubleOfHashMap(String hKey, String hashKey, Double delta);
/**
* 添加键值对到哈希表key中
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:05:42
* @param key
* @param hashKey
* @param value
*/
void hashPushHashMap(String key, Object hashKey, Object value);
/**
* 获取哈希表key中的所有域
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:08:18
* @param key
* @return
*/
Set<Object> hashGetAllHashKey(String key);
/**
* 获取散列中的字段数量
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:08:30
* @param key
* @return
*/
Long hashGetHashMapSize(String key);
/**
* 获取哈希中的所有值
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:08:39
* @param key
* @return
*/
List<Object> hashGetHashAllValues(String key);
/**
* 删除一个或多个哈希字段
*
* @description
* @author Liuyh
* @date 2018年4月19日上午10:08:49
* @param key
* @param hashKeys
* @return 被成功删除的数量
*/
Long hashDeleteHashKey(String key, Object... hashKeys);
}
接口太凌乱,整理一下吧!
package com.test.redis;
import java.util.Collection;
/**
* 封装redisTemplate公用处理接口
* @description
* @author Liuyh
* @date 2018年4月19日 上午10:18:41
*/
public interface ICache extends IStringCache,ISetCache,IListCache,IHashCache{
/**
* 删除键为key的缓存(hash/set/list/string)
*
* @description
* @author Liuyh
* @date 2018年4月19日上午9:56:24
* @param key
*/
void deleteFromRedis(String key);
/**
* 删除键为key的缓存(hash/set/list/string)
* @description
* @author Liuyh
* @date 2018年4月19日上午10:22:09
* @param keys
*/
void deleteFromRedis(Collection<String> keys);
}
核心:
package com.test.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
*
* @description
* @author Liuyh
* @date 2018年4月19日 上午9:44:52
* @param <K>
* 键类型
* @param <V>
* 值类型
*/
@Component
public class Cache implements ICache {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Override
public Integer stringAppendString(String key, String value) {
return redisTemplate.opsForValue().append(key, value);
}
@Override
public String stringGetStringByKey(String key) {
return redisTemplate.opsForValue().get(key);
}
@Override
public String stringGetSubStringFromString(String key, long start, long end) {
return redisTemplate.opsForValue().get(key, start, end);
}
@Override
public Long stringIncrementLongString(String key, Long delta) {
return redisTemplate.opsForValue().increment(key, delta);
}
@Override
public Double stringIncrementDoubleString(String key, Double delta) {
return redisTemplate.opsForValue().increment(key, delta);
}
@Override
public void stringSetString(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
@Override
public String stringGetAndSet(String key, String value) {
return redisTemplate.opsForValue().getAndSet(key, value);
}
@Override
public void stringSetValueAndExpireTime(String key, String value, long timeout) {
redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.MILLISECONDS);
}
@Override
public Long setAddSetMap(String key, String... values) {
return redisTemplate.opsForSet().add(key, values);
}
@Override
public Long setGetSizeForSetMap(String key) {
return redisTemplate.opsForSet().size(key);
}
@Override
public Set<String> setGetMemberOfSetMap(String key) {
return redisTemplate.opsForSet().members(key);
}
@Override
public boolean setCheckIsMemberOfSet(String key, Object o) {
return redisTemplate.opsForSet().isMember(key, o);
}
@Override
public void listRightPushList(String key, String value) {
redisTemplate.opsForList().rightPush(key, value);
}
@Override
public String listRightPopList(String key) {
return redisTemplate.opsForList().rightPop(key);
}
@Override
public void listLeftPushList(String key, String value) {
redisTemplate.opsForList().leftPush(key, value);
}
@Override
public String listLeftPopList(String key) {
return redisTemplate.opsForList().leftPop(key);
}
@Override
public Long listSize(String key) {
return redisTemplate.opsForList().size(key);
}
@Override
public List<String> listRangeList(String key, Long start, Long end) {
return redisTemplate.opsForList().range(key, start, end);
}
@Override
public Long listRemoveFromList(String key, long i, Object value) {
return redisTemplate.opsForList().remove(key, i, value);
}
@Override
public String listIndexFromList(String key, long index) {
return redisTemplate.opsForList().index(key, index);
}
@Override
public void listSetValueToList(String key, long index, String value) {
redisTemplate.opsForList().set(key, index, value);
}
@Override
public void listTrimByRange(String key, Long start, Long end) {
redisTemplate.opsForList().trim(key, start, end);
}
@Override
public boolean hashCheckHxists(String hKey, String hashKey) {
return redisTemplate.opsForHash().hasKey(hKey, hashKey);
}
@SuppressWarnings("unchecked")
@Override
public <T> T hashGet(String hKey, String hashKey) {
return (T) redisTemplate.opsForHash().get(hKey, hashKey);
}
@Override
public Map<Object, Object> hashGetAll(String key) {
return redisTemplate.opsForHash().entries(key);
}
@Override
public Long hashIncrementLongOfHashMap(String hKey, String hashKey, Long delta) {
return redisTemplate.opsForHash().increment(hKey, hashKey, delta);
}
@Override
public Double hashIncrementDoubleOfHashMap(String hKey, String hashKey, Double delta) {
return redisTemplate.opsForHash().increment(hKey, hashKey, delta);
}
@Override
public void hashPushHashMap(String key, Object hashKey, Object value) {
redisTemplate.opsForHash().put(key, hashKey, value);
}
@Override
public Set<Object> hashGetAllHashKey(String key) {
return redisTemplate.opsForHash().keys(key);
}
@Override
public Long hashGetHashMapSize(String key) {
return redisTemplate.opsForHash().size(key);
}
@Override
public List<Object> hashGetHashAllValues(String key) {
return redisTemplate.opsForHash().values(key);
}
@Override
public Long hashDeleteHashKey(String key, Object... hashKeys) {
return redisTemplate.opsForHash().delete(key, hashKeys);
}
@Override
public void deleteFromRedis(String key) {
redisTemplate.delete(key);
}
@Override
public void deleteFromRedis(Collection<String> keys) {
redisTemplate.delete(keys);
}
}
测试类:
package com.test.controller;
import com.test.redis.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "web")
public class WebController {
@Autowired
private Cache cache;
@RequestMapping(value = "getName",method = RequestMethod.GET)
public String getName(String name){
System.out.println(name);
return name;
}
@RequestMapping(value = "redisAdd",method = RequestMethod.GET)
public void redisAddName(String name){
System.out.println(name);
cache.stringSetValueAndExpireTime("name",name,20*1000);
}
}
项目地址:https://github.com/ShiLinDang/spring-boot2.git
https://github.com/ShiLinDang/spring-boot2.git