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是否有 ...
随机推荐
- 黄聪:pjax使用心得总结
初次结识pjax是在使用tower时钟发现的.当时使用时发现网站可以局部刷新,当然我们知道使用ajax也是可以实现局部刷新的. 然而我们知道,使用ajax进行局部刷新时网站的title是不会变化的,并 ...
- Reg2Bat_By Slore(生成同名bat文件,支持XP WIN7 WIN7X64).vbs
原文http://slore.blogbus.com/logs/52627038.htmlSlore编写的这个reg文件转换为bat文件,是逐句转换的,不是通过批处理生成临时reg文件然后导入的方法, ...
- 前端安全之CSRF
一.跨站点请求伪造(CSRF) 什么是csrf呢? 借助用户的身份去做有损用户利益(一些事情)的事情. 怎么实现跨站点请求伪造呢? 1.伪造者通过创造一个带有<a href=&qu ...
- http_range说明
100-200 // 第100到第200字节 500- // 第500字节到文件末尾 -1000 // 最后的1000个字节
- BIO & NIO & NIO常见框架
BIO & NIO BIO - Blocking IO - 同步式阻塞式IO --- UDP/TCP NIO - New IO - 同步式非阻塞式IO AIO - Asynchronous ...
- K近邻(K Nearest Neighbor-KNN)原理讲解及实现
算法原理 K最近邻(k-Nearest Neighbor)算法是比较简单的机器学习算法.它采用测量不同特征值之间的距离方法进行分类.它的思想很简单:如果一个样本在特征空间中的k个最近邻(最相似)的样本 ...
- docker安装testlink
testlink 镜像 https://hub.docker.com/r/bitnami/testlink ```#shell 下载镜像 docker pull bitnami/testlink
- 用JavaScript来生成HTML
用JavaScript来生成HTML <style> table{ border-top: 1px #ff0000 solid; border-left: 1px #ff0000 soli ...
- Git revert及其他一些回退操作
放弃本地的代码和本地提交,希望会退到远程分支的某次提交时,使用git reset --hard fa042ce. 本地已提交,但是发现有问题,想要撤销本地的提交,使用git checkout /trs ...
- mysql查询中取差集的问题
有个场景 现在有个打卡的记录表(daka),记录了用户每天的打卡信息,同时还有个运动打卡表(sport_daka),如果用户有运动打卡则在运动打卡表里面记录. 现在要统计用户的每天的打开信息,包括运动 ...