redisUtils
依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
package com.icil.elsa.collection.common.utils; import java.io.Serializable;
import java.util.List;
import java.util.Map;
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.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component; /**
* *************************************************************************
* <PRE>
* @ClassName: : RedisUtils
*
* @Description: : redis common utils
*
* @Creation Date : 12 Mar 2019 3:55:12 PM
*
* @Author : Sea
*
*
* </PRE>
**************************************************************************
*/
@SuppressWarnings("all")
@Component
public class RedisUtils {
@Autowired
private RedisTemplate redisTemplate;
/**
* 解决乱码问题
*/
@Autowired(required = false)
public void setRedisTemplate(RedisTemplate redisTemplate) {
RedisSerializer stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);
GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
// redisTemplate.setValueSerializer(stringSerializer);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(stringSerializer);
this.redisTemplate = redisTemplate;
} /**
* 写入缓存
* @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
* @param expireTime days
* @return
*/
public boolean setwithExpireDay(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.DAYS);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
} /**
* * 写入缓存设置时效时间
* @param key
* @param value
* @param expireTime sec
* @return
*/
public boolean setwithExpireSec(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;
}
/**
* 批量删除对应的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() > )
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 hashKey
* @param value
*/
public void hmSet(String key, Object hashKey, Object value){
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
hash.put(key,hashKey,value);
}
/**
* 哈希 添加
* @param key
* @param hashKey
* @param value
*/
public void hmSetwithExpireDay(String key, Object hashKey, Object value,Long expireTime){
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
redisTemplate.expire(key,expireTime, TimeUnit.DAYS);
// redisTemplate.expire(key,expireTime, TimeUnit.MINUTES);
hash.put(key,hashKey,value);
} /**
* 哈希获取数据
* @param key
* @param hashKey
* @return
*/
public Object hmGet(String key, Object hashKey){
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
return hash.get(key,hashKey);
} /**
* delete hashkey
* @param key
* @param hashKey
* @return
*/
public void hmDel(String key, Object hashKey){
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
hash.delete(key,hashKey);
} public Map<Object, Object> hmGetentries(String key){
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
Map<Object, Object> entries = hash.entries(key);
return entries;
} /**
* 列表添加
* @param k
* @param v
*/
public void lPush(String k,Object v){
ListOperations<String, Object> list = redisTemplate.opsForList();
list.rightPush(k,v);
} /**
* 列表获取
* @param k
* @param l
* @param l1
* @return
*/
public List<Object> lRange(String k, long l, long l1){
ListOperations<String, Object> list = redisTemplate.opsForList();
return list.range(k,l,l1);
} /**
* 集合添加
* @param key
* @param value
*/
public void add(String key,Object value){
SetOperations<String, Object> set = redisTemplate.opsForSet();
set.add(key,value);
} /**
* 集合获取
* @param key
* @return
*/
public Set<Object> setMembers(String key){
SetOperations<String, Object> set = redisTemplate.opsForSet();
return set.members(key);
} /**
* 有序集合添加
* @param key
* @param value
* @param scoure
*/
public void zAdd(String key,Object value,double scoure){
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
zset.add(key,value,scoure);
} /**
* 有序集合获取
* @param key
* @param scoure
* @param scoure1
* @return
*/
public Set<Object> rangeByScore(String key,double scoure,double scoure1){
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
return zset.rangeByScore(key, scoure, scoure1);
}
}
redisUtils的更多相关文章
- 单机版 RedisUtils({基本操作封装工具类})【三】
<!--集成的RedisJAR--> <!--引入jedis需的jar包--> <dependency> <groupId>redis.clients& ...
- java redisUtils工具类很全
GitHub地址:https://github.com/whvcse/RedisUtil redisUtils工具类: package com.citydo.utils; import org.spr ...
- Java-Class-C:com.ylbtech.api.platfrom.util.RedisUtils.class
ylbtech-Java-Class-C:com.ylbtech.api.platfrom.util.RedisUtils.class 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶 ...
- SpringBoot整合Redis、mybatis实战,封装RedisUtils工具类,redis缓存mybatis数据 附源码
创建SpringBoot项目 在线创建方式 网址:https://start.spring.io/ 然后创建Controller.Mapper.Service包 SpringBoot整合Redis 引 ...
- RedisUtils工具类
package test.util; import org.springframework.beans.factory.annotation.Autowired; import org.springf ...
- (Java) RedisUtils
package com.vcgeek.hephaestus.utils; import org.springframework.beans.factory.annotation.Autowired; ...
- Redis和SpringBoot整合RedisUtils类
一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- Redis在JAVA中的运用(工具类)
最近项目需要用redis在中间做缓存所以写了一个工具类作为练习用 redis版本:redis_version:3.0.504 用到阿里的解析JSON的库:fastjson import org.apa ...
- Redis命令
redis的常用命令主要分为两个方面.一个是键值相关命令.一个是服务器相关命令(redis-cli进入终端) 1.键值相关命令 keys * 取出当前所有的key exists name 查看n是否有 ...
随机推荐
- 黄聪:HBuilder左侧项目管理器如何不与标签页一起自动切换
把这个按钮取消就好了
- 日志框架学习(log4j2+slf4j)
现在比较吊的就是这个log4j2这个日志框架了,功能强悍.slf4j是个日志框架的统一接口,方便扩展,切换框架啥的. 配置SSM+log4J2+SL4J https://blog.csdn.net/c ...
- 引用 自动化测试基础篇--Selenium简介
原文链接:http://www.cnblogs.com/sanzangTst/p/7452636.html 鸣谢参藏法师 一.软件开发的一般流程 二.什么叫软件测试? 软件测试(英语:Software ...
- JIRA 资源
https://www.**.com/article/2015/8/7/22532.html e_v_g_e_t JIRA使用教程:在Windows上安装JIRAJIRA使用教程:在Linux上安装J ...
- 使用googletest进行C++单元测试(Netbeans为例)
googletest设置步骤(Netbeans为例) 下载googletest [https://github.com/google/googletest],解压到<gtest_dir> ...
- STL基础--算法(不修改数据的算法)
不修改数据的算法 count, min and max, compare, linear search, attribute // 算法中Lambda函数很常用: num = count_if(vec ...
- Office 2016 Pro Plus \ Project 专业版 \ Visio 专业版 \ 64 位vol版本方便KMS小马oem
在使用上,零售版和批量授权版并没有区别,只是授权方式方面的区别,相对而言,VOL 版的更容易激活一些,其他并没有什么区别了. 有需要的可以在下面下载:(以下均是 64位VL 版) 版本:Office ...
- 图像处理PILLOW的使用
1.安装 pip install Pillow 2.使用 1)图片缩放 from PIL import Imageim = Image.open('dog.jpg') w,h = im.size #获 ...
- http_range说明
100-200 // 第100到第200字节 500- // 第500字节到文件末尾 -1000 // 最后的1000个字节
- Hbase设置多个hmaster
Hbase设置多个hmaster https://www.cnblogs.com/prayer21/p/4866673.html