package com.wang.util;
import com.wang.exception.CustomException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.Set;
/**
* JedisUtil(推荐存Byte数组,存Json字符串效率更慢)
* @author Wang926454
* @date 2018/9/4 15:45
*/
@Component
public class JedisUtil {
/**
* Logger
*/
private static Logger logger = LoggerFactory.getLogger(JedisUtil.class);
/**
* Status-OK
*/
public final static String OK = "OK";
/**
* 静态注入JedisPool连接池
* 本来是正常注入JedisUtil,可以在Controller和Service层使用,但是重写Shiro的CustomCache无法注入JedisUtil
* 现在改为静态注入JedisPool连接池,JedisUtil直接调用静态方法即可
* https://blog.csdn.net/W_Z_W_888/article/details/79979103
*/
private static JedisPool jedisPool;
@Autowired
public void setJedisPool(JedisPool jedisPool) {
JedisUtil.jedisPool = jedisPool;
}
/**
* 获取Jedis实例
* @param
* @return redis.clients.jedis.Jedis
* @author Wang926454
* @date 2018/9/4 15:47
*/
public static synchronized Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
} catch (Exception e) {
throw new CustomException("获取Jedis资源异常:" + e.getMessage());
}
}
/**
* 释放Jedis资源
* @param
* @return void
* @author Wang926454
* @date 2018/9/5 9:16
*/
public static void closePool() {
try {
jedisPool.close();
}catch (Exception e){
throw new CustomException("释放Jedis资源异常:" + e.getMessage());
}
}
/**
* 获取redis键值-object
* @param key
* @return java.lang.Object
* @author Wang926454
* @date 2018/9/4 15:47
*/
public static Object getObject(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
byte[] bytes = jedis.get(key.getBytes());
if(StringUtil.isNotNull(bytes)) {
return SerializableUtil.unserializable(bytes);
}
} catch (Exception e) {
throw new CustomException("获取Redis键值getObject方法异常:key=" + key + " cause=" + e.getMessage());
} finally {
if(jedis != null) {
jedis.close();
}
}
return null;
}
/**
* 设置redis键值-object
* @param key
* @param value
* @return java.lang.String
* @author Wang926454
* @date 2018/9/4 15:49
*/
public static String setObject(String key, Object value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.set(key.getBytes(), SerializableUtil.serializable(value));
} catch (Exception e) {
throw new CustomException("设置Redis键值setObject方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
} finally {
if(jedis != null) {
jedis.close();
}
}
}
/**
* 设置redis键值-object-expiretime
* @param key
* @param value
* @param expiretime
* @return java.lang.String
* @author Wang926454
* @date 2018/9/4 15:50
*/
public static String setObject(String key, Object value, int expiretime) {
String result = "";
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
result = jedis.set(key.getBytes(), SerializableUtil.serializable(value));
if(OK.equals(result)) {
jedis.expire(key.getBytes(), expiretime);
}
return result;
} catch (Exception e) {
throw new CustomException("设置Redis键值setObject方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
} finally {
if(jedis != null) {
jedis.close();
}
}
}
/**
* 获取redis键值-Json
* @param key
* @return java.lang.Object
* @author Wang926454
* @date 2018/9/4 15:47
*/
public static String getJson(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.get(key);
} catch (Exception e) {
throw new CustomException("获取Redis键值getJson方法异常:key=" + key + " cause=" + e.getMessage());
} finally {
if(jedis != null) {
jedis.close();
}
}
}
/**
* 设置redis键值-Json
* @param key
* @param value
* @return java.lang.String
* @author Wang926454
* @date 2018/9/4 15:49
*/
public static String setJson(String key, String value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.set(key, value);
} catch (Exception e) {
throw new CustomException("设置Redis键值setJson方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
} finally {
if(jedis != null) {
jedis.close();
}
}
}
/**
* 设置redis键值-Json-expiretime
* @param key
* @param value
* @param expiretime
* @return java.lang.String
* @author Wang926454
* @date 2018/9/4 15:50
*/
public static String setJson(String key, String value, int expiretime) {
String result = "";
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
result = jedis.set(key, value);
if(OK.equals(result)) {
jedis.expire(key, expiretime);
}
return result;
} catch (Exception e) {
throw new CustomException("设置Redis键值setJson方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage());
} finally {
if(jedis != null) {
jedis.close();
}
}
}
/**
* 删除key
* @param key
* @return java.lang.Long
* @author Wang926454
* @date 2018/9/4 15:50
*/
public static Long delKey(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.del(key.getBytes());
}catch(Exception e) {
throw new CustomException("删除Redis的键delKey方法异常:key=" + key + " cause=" + e.getMessage());
}finally{
if(jedis != null) {
jedis.close();
}
}
}
/**
* key是否存在
* @param key
* @return java.lang.Boolean
* @author Wang926454
* @date 2018/9/4 15:51
*/
public static Boolean exists(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.exists(key.getBytes());
}catch(Exception e) {
throw new CustomException("查询Redis的键是否存在exists方法异常:key=" + key + " cause=" + e.getMessage());
}finally{
if(jedis != null) {
jedis.close();
}
}
}
/**
* 模糊查询获取key集合
* @param key
* @return java.
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ShiroJwt.rar (49个子文件)
ShiroJwt
pom.xml 6KB
src
main
resources
mybatis-config.xml 3KB
application.yml 1KB
generator
generatorConfig.xml 3KB
config.properties 978B
mapper
PermissionMapper.xml 1017B
RoleMapper.xml 992B
UserRoleMapper.xml 532B
RolePermissionMapper.xml 556B
UserMapper.xml 673B
sql
MySQL.sql 2KB
java
com
wang
exception
CustomUnauthorizedException.java 326B
CustomException.java 317B
config
shiro
UserRealm.java 4KB
JWTFilter.java 5KB
ShiroConfig.java 4KB
CustomCache.java 3KB
JedisConfig.java 3KB
CustomCacheManager.java 453B
JWTToken.java 500B
model
RoleDto.java 345B
UserDto.java 347B
PermissionDto.java 369B
UserRoleDto.java 362B
common
ResponseBean.java 847B
RolePermissionDto.java 387B
entity
User.java 2KB
UserRole.java 1KB
Role.java 870B
Permission.java 1KB
RolePermission.java 1KB
Application.java 473B
mapper
UserRoleMapper.java 245B
UserMapper.java 229B
PermissionMapper.java 572B
RoleMapper.java 524B
RolePermissionMapper.java 269B
controller
UserController.java 6KB
ExceptionController.java 2KB
service
IBaseService.java 5KB
IUserService.java 187B
impl
UserServiceImpl.java 313B
BaseServiceImpl.java 3KB
util
StringUtil.java 3KB
JedisUtil.java 10KB
SerializableUtil.java 2KB
common
JsonConvertUtil.java 590B
JWTUtil.java 2KB
.gitignore 664B
共 49 条
- 1
资源评论
时间可以改变一切
- 粉丝: 36
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功