Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。
下面基于spring-data-redis1.8.1.RELEASE、jedis2.9.0版本为基础编写的redis工具类,欢迎大家指正和补充。
maven依赖包:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.1.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
工具类代码:
/**
* Value存储数据 对象存储
*
* @param index
* template模版index
* @param key
* key
* @param value
* 对象值
* @param timeout
* 超时时间
*/
@SuppressWarnings("unchecked")
public void setValue(int index, String key, String value, Long timeout) {
BoundValueOperations<String, String> ops = (BoundValueOperations<String, String>) this.getRedisTemplate(index)
.boundValueOps(key);
if (timeout != null) {
ops.set(value, timeout, TimeUnit.MILLISECONDS);
} else {
ops.set(Utils.GSON.toJson(value));
}
}
/**
*
* Value存储数据 对象存储
*
* @param index
* template模版index
* @param key
* key
* @param value
* 对象值
* @param timeout
* 超时时间
*/
@SuppressWarnings("unchecked")
public void setValue(int index, String key, Object value, Long timeout) {
BoundValueOperations<String, String> ops = (BoundValueOperations<String, String>) this.getRedisTemplate(index)
.boundValueOps(key);
if (timeout != null) {
ops.set(Utils.GSON.toJson(value), timeout, TimeUnit.MILLISECONDS);
} else {
ops.set(Utils.GSON.toJson(value));
}
}
/**
* Value提取数据 对象获取
*
* @param index
* template模版index
* @param key
* key
* @return
*/
public String getValue(int index, String key) {
return (String) this.getRedisTemplate(index).opsForValue().get(key);
}
/**
* value提取数据 list获取
*
* @param index
* template模版index
* @param keys
* keys
* @param clazz
* 对象
* @return
*/
@SuppressWarnings("unchecked")
public <T> List<T> getValue(int index, List<String> keys, Class<T> clazz) {
List<String> vList = (List<String>) this.getRedisTemplate(index).opsForValue().multiGet(keys);
List<T> rList = new ArrayList<T>();
vList.forEach((String value) -> {
rList.add(Utils.GSON.fromJson(value, clazz));
});
return rList;
}
/**
* Value提取数据 对象获取
*
* @param index
* template模版index
* @param key
* key
* @param clazz
* 对象
*/
public <T> T getValue(int index, String key, Class<T> clazz) {
String value = (String) this.getRedisTemplate(index).opsForValue().get(key);
return Utils.GSON.fromJson(value, clazz);
}
/**
* Value长度获取
*
* @param index
* template模版index
* @param key
* key
* @return
*/
public Long getValueLength(int index, String key) {
return this.getRedisTemplate(index).opsForValue().size(key);
}
/**
* hash存储数据 对象存储
*
* @param index
* template模版index
* @param key
* key
* @param hashKey
* hashKey
* @param value
* 对象值
* @param timeout
* 超时时间
*/
public void setHash(int index, String key, Object hashKey, Object value, Long timeout) {
BoundHashOperations<String, Object, Object> ops = this.getRedisTemplate(index).boundHashOps(key);
if (timeout != null) {
ops.expire(timeout, TimeUnit.MILLISECONDS);
}
if (value instanceof String) {
ops.put(hashKey, value);
} else {
ops.put(hashKey, Utils.GSON.toJson(value));
}
}
/**
* hash存储数据 map存储
*
* @param index
* template模版index
* @param key
* key
* @param map
* 对象值
* @param timeout
* 超时时间
*/
public void setHash(int index, String key, Map<Object, String> map, Long timeout) {
BoundHashOperations<String, Object, String> ops = this.getRedisTemplate(index).boundHashOps(key);
if (timeout != null) {
ops.expire(timeout, TimeUnit.MILLISECONDS);
}
ops.putAll(map);
}
/**
* hash提取数据 对象提取
*
* @param index
* template模版index
* @param key
* key
* @param hashKey
* hashKey
* @param clazz
* 对象类型
* @return
*/
public <T> T getHash(int index, String key, Object hashKey, Class<T> clazz) {
String value = (String) this.getRedisTemplate(index).boundHashOps(key).get(hashKey);
if (StringUtils.isNotBlank(value))
return Utils.GSON.fromJson(value, clazz);
return null;
}
/**
* hash提取数据 列表提取
*
* @param index
* template模版index
* @param key
* key
* @param hashKey
* hashKey
* @param clazz
* 对象类型
* @return
*/
public <T> List<T> getHashList(int index, String key, Object hashKey, Class<T> clazz) {
String value = (String) getRedisTemplate(index).opsForHash().get(key, hashKey);
if (StringUtils.isNotBlank(value)) {
JsonElement je = new JsonParser().parse(value);
List<T> list = new ArrayList<T>();
if (je.isJsonArray()) {
je.getAsJsonArray().forEach((JsonElement em) -> {
list.add(Utils.GSON.fromJson(em, clazz));
});
} else {
list.add(Utils.GSON.fromJson(je.getAsJsonObject(), clazz));
}
return list;
}
return null;
}
/**
* hash提取数据 列表提取
*
* @param index
* template模版index
* @param key
* key
* @param hashKeys
* hashKeys
* @return
*/
public List<Object> getHashList(int index, String key, List<Object> hashKeys) {
return this.getRedisTemplate(index).boundHashOps(key).multiGet(hashKeys);
}
/**
* hash提取数据 提取所有数据
*
* @param index
* template模版index
* @param key
* key
* @return
*/
public Map<Object, Object> getHashList(int index, String key) {
return this.getRedisTemplate(index).opsForHash().entries(key);
}
/**
* hash提取数据 提取所有key数据
*
* @param index
* template模版index
* @param key
* key
* @return
*/
public Set<Object> getHashAllKeys(int index, String key) {
return this.getRedisTemplate(index).opsForHash().keys(key);
}
/**
* hash提取数据 提取所有value数据
*
* @param index
* template模版index
* @param key
* key
* @return
*/
public List<Object> getHashAllValues(int index, String key) {
return this.getRedisTemplate(index).opsForHash().values(key);
}
/**
* hash提取数据 根据hashKeys删除数据
*
* @param index
* template模版index
* @param key
* key
* @param hashKeys
* hashkey数组
* @return
*/
public Long delHash(int index, String key, Object... hashKeys) {
return this.getRedisTemplate(index).opsForHash().delete(key, hashKeys);
}
/**
* list存储数据 对象存储
*
* @param index
* template模版index
* @param key
* key
* @param value
* 对象值
* @param timeout
* 超时时间
*/
@SuppressWarnings("unchecked")
public void setListLeftPush(int index, String key, Object value, Long timeout) {
BoundListOperations<String, Object> ops = (BoundListOperations<String, Object>) this.getRedisTemplate(index)
.boundListOps(key);
if (timeout != null) {
ops.expire(timeout, TimeUnit.MILLISECONDS);
}
if (value instanceof String) {
ops.leftPush(value);
} else {
ops.leftPush(Utils.GSON.toJson(value));
}
}
/**
* list存储数据 对象存储
*
* @param index
* template模版index
* @param key
* key
* @param value
* 对象值
* @param timeout
* 超时时间
*/
@SuppressWarnings("unchecked")
public void setListRightPush(int index, String key, Object value, Long timeout) {
BoundListOperations<String, Object> ops = (BoundListOperations<String, Object>) this.getRedisTemplate(index)
.boundListOps(key);
if (timeout != null) {
ops.expire(timeout, TimeUnit.MILLISECONDS);
}
if (value instanceof String) {
ops.rightPush(value);
} else {
ops.rightPush(Utils.GSON.toJson(value));
}
}
/**
* list存储数据 list存储
*
* @param index
* template模版index
* @param key
* key
* @param value
* 对象值
* @param timeout
* 超时时间
*/
@SuppressWarnings("unchecked")
public void setListLeftPushAll(int index, String key, List<Object> value, Long timeout) {
BoundListOperations<String, Object> ops = (BoundListOperations<String, Object>) this.getRedisTemplate(index)
.boundListOps(key);
if (timeout != null) {
ops.expire(timeout, TimeUnit.MILLISECONDS);
}
ops.leftPushAll(Utils.GSON.toJson(value));
}
/**
* list存储数据 list存储
*
* @param index
* template模版index
* @param key
* key
* @param value
* 对象值
* @param timeout
* 超时时间
*/
@SuppressWarnings("unchecked")
public void setListRightPushAll(int index, String key, List<Object> value, Long timeout) {
BoundListOperations<String, Object> ops = (BoundListOperations<String, Object>) this.getRedisTemplate(index)
.boundListOps(key);
if (timeout != null) {
ops.expire(timeout, TimeUnit.MILLISECONDS);
}
ops.rightPushAll(Utils.GSON.toJson(value));
}
/**
* list存储数据 下标存储
*
* @param index
* template模版index
* @param subscript
* 存储下标位置
* @param key
* key
* @param value
* 对象值
* @param timeout
* 超时时间
*/
@SuppressWarnings("unchecked")
public void setListSubscript(int index, Long subscript, String key, Object value, Long timeout) {
BoundListOperations<String, Object> ops = (BoundListOperations<String, Object>) this.getRedisTemplate(index)
.boundListOps(key);
if (timeout != null) {
ops.expire(timeout, TimeUnit.MILLISECONDS);
}
if (value instanceof String) {
ops.set(subscript, value);
} else {
ops.set(subscript, Utils.GSON.toJson(value));
}
}
/**
* list提取数据 提取范围内数据
*
* @param index
* template模版index
* @param key
* key
* @param start
* 开始下标
* @param end
* 结束下标
* @param clazz
* 对象类型
* @return
*/
@SuppressWarnings("unchecked")
public <T> List<T> getListRange(int index, String key, int start, int end, Class<T> clazz) {
BoundListOperations<String, String> ops = (BoundListOperations<String, String>) this.getRedisTemplate(index)
.boundListOps(key);
List<T> list = new ArrayList<T>();
ops.range(start, end).forEach((String value) -> {
list.add(Utils.GSON.fromJson(value, clazz));
});
return list;
}
/**
* list提取数据 提取下标数据
*
* @param index
* template模版index
* @param subscript
* 下标
* @param key
* key
* @param clazz
* 对象类型
* @return
*/
@SuppressWarnings("unchecked")
public <T> T getListSubscript(int index, long subscript, String key, Class<T> clazz) {
BoundListOperations<String, String> ops = (BoundListOperations<String, String>) this.getRedisTemplate(index)
.boundListOps(key);
String value = ops.index(subscript);
if (StringUtils.isNotBlank(value))
return Utils.GSON.fromJson(value, clazz);
return null;
}
/**
* list提取数据 提取数组长度
*
* @param index
* template模版index
* @param key
* key
* @return
*/
public Long getListSize(int index, String key) {
return this.getRedisTemplate(index).opsForList().size(key);
}
/**
* list删除数据 下标和对象删除
*
* @param index
* template模版index
* @param subscript
* 下标
* @param key
* key
* @param value
* value
*/
@SuppressWarnings("unchecked")
public void delListSubscript(int index, long subscript, String key, Object value) {
BoundListOperations<String, Object> ops = (BoundListOperations<String, Object>) this.getRedisTemplate(index)
.boundListOps(key);
ops.remove(subscript, value);
}
/**
* set存储数据 set存储
*
* @param index
* template模版index
* @param key
* key
* @param timeout
* 超时时间
* @param values
* 对象值
*/
@SuppressWarnings("unchecked")
public void setSet(int index, String key, Long timeout, Object... values) {
BoundSetOperations<String, Object> ops = (BoundSetOperations<String, Object>) this.getRedisTemplate(index)
.boundSetOps(key);
if (timeout != null) {
ops.expire(timeout, TimeUnit.MILLISECONDS);
}
List<String> list = new ArrayList<String>();
for (Object obj : values) {
if (obj instanceof String) {
list.add((String) obj);
} else {
list.add(Utils.GSON.toJson(obj));
}
}
ops.add(list.toArray());
}
/**
* set提取数据 set提取
*
* @param index
* template模版index
* @param key
* key
* @param clazz
* 对象类型
* @return
*/
@SuppressWarnings("unchecked")
public <T> List<T> getSet(int index, String key, Class<T> clazz) {
BoundSetOperations<String, String> ops = (BoundSetOperations<String, String>) this.getRedisTemplate(index)
.boundSetOps(key);
List<T> list = new ArrayList<T>();
ops.members().forEach((String value) -> {
if (clazz.isAssignableFrom(String.class)) {
list.add((T) value);
} else {
list.add(Utils.GSON.fromJson(value, clazz));
}
});
return list;
}
/**
* set长度获取
*
* @param index
* template模版index
* @param key
* key
* @return
*/
public Long getSetSize(int index, String key) {
return this.getRedisTemplate(index).opsForSet().size(key);
}
/**
* set删除数据 values删除
*
* @param index
* template模版index
* @param key
* key
* @return
*/
public void delSet(int index, String key, Object... values) {
this.getRedisTemplate(index).opsForSet().remove(key, values);
}
/**
* zset存储数据 对象存储
*
* @param index
* template模版index
* @param key
* key
* @param value
* 对象值
* @param score
* 排序值
* @param timeout
* 超时时间
*/
@SuppressWarnings("unchecked")
public void setZset(int index, String key, Object value, Double score, Long timeout) {
BoundZSetOperations<String, Object> ops = (BoundZSetOperations<String, Object>) this.getRedisTemplate(index)
.boundZSetOps(key);
if (timeout != null) {
ops.expire(timeout, TimeUnit.MILLISECONDS);
}
if (value instanceof String) {
ops.add(value, score);
} else {
ops.add(Utils.GSON.toJson(value), score);
}
}
/**
* zset提取数据 set提取
*
* @param index
* template模版index
* @param key
* key
* @param start
* 开始下标
* @param end
* 结束下标
* @param clazz
* 对象类型
* @return
*/
@SuppressWarnings("unchecked")
public <T> List<T> getZset(int index, String key, Long start, Long end, Class<T> clazz) {
BoundZSetOperations<String, String> ops = (BoundZSetOperations<String, String>) this.getRedisTemplate(index)
.boundZSetOps(key);
List<T> list = new ArrayList<T>();
ops.range(start, end).forEach((String value) -> {
list.add(Utils.GSON.fromJson(value, clazz));
});
return list;
}
/**
* zset提取数据 set提取
*
* @param index
* template模版index
* @param key
* key
* @param min
* 最小值
* @param max
* 最大值
* @param clazz
* 对象类型
* @return
*/
@SuppressWarnings("unchecked")
public <T> List<T> getZset(int index, String key, Double min, Double max, Class<T> clazz) {
BoundZSetOperations<String, String> ops = (BoundZSetOperations<String, String>) this.getRedisTemplate(index)
.boundZSetOps(key);
List<T> list = new ArrayList<T>();
ops.rangeByScore(min, max).forEach((String value) -> {
list.add(Utils.GSON.fromJson(value, clazz));
});
return list;
}
/**
* zset提取数据 zset长度获取
*
* @param index
* template模版index
* @param key
* key
* @param min
* 最小值
* @param max
* 最大值
* @return
*/
public Long getZsetSize(int index, String key, Double min, Double max) {
return this.getRedisTemplate(index).opsForZSet().count(key, min, max);
}
/**
* zset提取数据 zset长度获取
*
* @param index
* template模版index
* @param key
* key
* @return
*/
public Long getZsetSize(int index, String key) {
return this.getRedisTemplate(index).opsForZSet().size(key);
}
/**
* zset删除数据 范围删除
*
* @param index
* template模版index
* @param key
* key
* @param start
* 开始下标
* @param end
* 结束下标
* @return
*/
public Long delZset(int index, String key, Long start, Long end) {
return this.getRedisTemplate(index).opsForZSet().removeRange(key, start, end);
}
/**
* zset删除数据 数值删除
*
* @param index
* template模版index
* @param key
* key
* @param values
* 数值数组
*/
public void delZset(int index, String key, Object... values) {
this.getRedisTemplate(index).opsForZSet().remove(key, values);
}
/**
* 删除数据 key值删除
*
* @param index
* template模版index
* @param key
* key
*/
public void delByKey(int index, String key) {
this.getRedisTemplate(index).delete(key);
}
/**
* 删除数据 keys值删除
*
* @param index
* template模版index
* @param key
* key
*/
public void delByKeys(int index, List<String> key) {
this.getRedisTemplate(index).delete(key);
}