package com.ibeetl.admin.console.service;
|
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.dao.DataAccessException;
|
import org.springframework.data.geo.Point;
|
import org.springframework.data.redis.connection.RedisConnection;
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
import org.springframework.data.redis.core.*;
|
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
import org.springframework.stereotype.Service;
|
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.JedisPool;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.io.Serializable;
|
import java.util.*;
|
import java.util.concurrent.TimeUnit;
|
|
/**
|
* redis服务
|
*/
|
@Service
|
public class RedisService {
|
@Autowired
|
private RedisTemplate redisTemplate;
|
|
private JedisPool jedisPool;
|
@Bean
|
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory)
|
{
|
/*Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
|
ObjectMapper om = new ObjectMapper();
|
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
jackson2JsonRedisSerializer.setObjectMapper(om);
|
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
|
template.setConnectionFactory(redisConnectionFactory);
|
template.setKeySerializer(jackson2JsonRedisSerializer);
|
template.setValueSerializer(jackson2JsonRedisSerializer);
|
template.setHashKeySerializer(jackson2JsonRedisSerializer);
|
template.setHashValueSerializer(jackson2JsonRedisSerializer);
|
template.afterPropertiesSet();
|
this.redisTemplate = template;*/
|
RedisSerializer stringSerializer = new StringRedisSerializer();
|
redisTemplate.setKeySerializer(stringSerializer);
|
redisTemplate.setValueSerializer(stringSerializer);
|
redisTemplate.setHashKeySerializer(stringSerializer);
|
redisTemplate.setHashValueSerializer(stringSerializer);
|
this.redisTemplate = redisTemplate;
|
return redisTemplate;
|
}
|
|
/**
|
* 写入缓存
|
* @param key
|
* @param value
|
* @return
|
*/
|
public boolean set(final String key, Object value) {
|
boolean result = false;
|
try {
|
ValueOperations<String, Object> operations = redisTemplate.opsForValue();
|
operations.set(key, value);
|
result = true;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return result;
|
}
|
/**
|
* 写入缓存设置时效时间
|
* @param key
|
* @param value
|
* @return
|
*/
|
public boolean set(final String key, Object value, Long expireTime) {
|
boolean result = false;
|
try {
|
ValueOperations<String, Object> operations = redisTemplate.opsForValue();
|
operations.set(key, value);
|
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
|
result = true;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return result;
|
}
|
|
/**
|
* 防重复提交设置
|
* @param key
|
* @param value
|
* @return
|
*/
|
public boolean preventManyCommit(String key, String value, Long expireTime) {
|
if(get(key) == null){
|
try {
|
ValueOperations<String, Object> operations = redisTemplate.opsForValue();
|
operations.set(key, value);
|
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
|
return true;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return false;
|
}else{
|
return false;
|
}
|
}
|
|
/**
|
* redis 琐
|
* @param key
|
* @param time
|
* @return
|
*/
|
public boolean lock(String key, Long time){
|
String lock = key;
|
return (boolean)redisTemplate.execute(new RedisCallback<Boolean>() {
|
@Override
|
public Boolean doInRedis(RedisConnection redisConnection) throws DataAccessException {
|
long expireAt = System.currentTimeMillis() + time + 1;
|
Boolean acquire = redisConnection.setNX(lock.getBytes(), String.valueOf(expireAt).getBytes());
|
if(acquire){
|
return true;
|
}else {
|
byte[] value = redisConnection.get(lock.getBytes());
|
if (Objects.nonNull(value) && value.length > 0) {
|
long expireTime = Long.parseLong(new String(value));
|
if (expireTime < System.currentTimeMillis()) {
|
// 如果锁已经过期
|
byte[] oldValue = redisConnection.getSet(lock.getBytes(), String.valueOf(System.currentTimeMillis() + 1).getBytes());
|
// 防止死锁
|
return Long.parseLong(new String(oldValue)) < System.currentTimeMillis();
|
}
|
}
|
}
|
return false;
|
}
|
});
|
}
|
|
|
/**
|
* 批量删除对应的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<String> keys = redisTemplate.keys(pattern);
|
if (keys.size() > 0) {
|
redisTemplate.delete(keys);
|
}
|
}
|
/**
|
* 删除对应的value
|
* @param key
|
*/
|
public void remove(final String key) {
|
if (exists(key)) {
|
redisTemplate.delete(key);
|
}
|
}
|
|
public long hdel(String key, String... field) {
|
Jedis jedis = jedisPool.getResource();
|
Long result = jedis.hdel(key, field);
|
jedis.close();
|
return result;
|
}
|
|
/**
|
* 判断缓存中是否有对应的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<String, 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
|
* @return
|
*/
|
public Object hmGet(String key, Object hashKey){
|
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
|
return hash.get(key,hashKey);
|
}
|
|
/**
|
* 列表添加
|
* @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);
|
}
|
|
/**
|
* 获取数据Map
|
*
|
* @param mapName
|
* @return
|
*/
|
public Map<String, Object> getMapValue(String mapName) {
|
HashOperations<String, String, Object> hps = redisTemplate.opsForHash();
|
return hps.entries(mapName);
|
}
|
|
}
|