|  | @@ -1,291 +0,0 @@
 | 
	
		
			
				|  |  | -package com.tzld.piaoquan.ad.engine.commons.redis;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -import org.slf4j.Logger;
 | 
	
		
			
				|  |  | -import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | -import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | -import org.springframework.beans.factory.annotation.Qualifier;
 | 
	
		
			
				|  |  | -import org.springframework.data.redis.connection.RedisStringCommands;
 | 
	
		
			
				|  |  | -import org.springframework.data.redis.core.RedisCallback;
 | 
	
		
			
				|  |  | -import org.springframework.data.redis.core.RedisTemplate;
 | 
	
		
			
				|  |  | -import org.springframework.data.redis.core.types.Expiration;
 | 
	
		
			
				|  |  | -import org.springframework.data.redis.serializer.RedisSerializer;
 | 
	
		
			
				|  |  | -import org.springframework.stereotype.Component;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -import java.util.List;
 | 
	
		
			
				|  |  | -import java.util.Map;
 | 
	
		
			
				|  |  | -import java.util.concurrent.TimeUnit;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -/**
 | 
	
		
			
				|  |  | - * @author ehlxr
 | 
	
		
			
				|  |  | - */
 | 
	
		
			
				|  |  | -@Component
 | 
	
		
			
				|  |  | -public class RedisHelper {
 | 
	
		
			
				|  |  | -    private static final Logger log = LoggerFactory.getLogger(RedisHelper.class);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 解锁 lua 脚本
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    // public static final String UNLOCK = "if (redis.call('get', KEYS[1]) == ARGV[1]) then" +
 | 
	
		
			
				|  |  | -    //         " return redis.call('del', KEYS[1]);" +
 | 
	
		
			
				|  |  | -    //         "else" +
 | 
	
		
			
				|  |  | -    //         " return 0;" +
 | 
	
		
			
				|  |  | -    //         "end";
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 锁前缀
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    private static final String LOCK_PREFIX = "LOCK_";
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 每次重试间隔时间 (毫秒)
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    // private static final int DEFAULT_RETRY_INTERVAL = 100;
 | 
	
		
			
				|  |  | -    private static RedisTemplate<String, String> redisTemplate;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 加锁
 | 
	
		
			
				|  |  | -     *
 | 
	
		
			
				|  |  | -     * @param timeout 毫秒
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    // public static Boolean lock(String key, String lockId, long timeout) {
 | 
	
		
			
				|  |  | -    //     return redisTemplate.opsForValue().setIfAbsent(LOCK_PREFIX + key, lockId, timeout, TimeUnit.MILLISECONDS);
 | 
	
		
			
				|  |  | -    // }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 重试加锁
 | 
	
		
			
				|  |  | -     *
 | 
	
		
			
				|  |  | -     * @param interval 重试间隔时间
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    // public static Boolean tryLock(String key, String lockId, long timeout, int interval) {
 | 
	
		
			
				|  |  | -    //     if (interval <= 0) {
 | 
	
		
			
				|  |  | -    //         interval = DEFAULT_RETRY_INTERVAL;
 | 
	
		
			
				|  |  | -    //     }
 | 
	
		
			
				|  |  | -    //     while (timeout >= 0) {
 | 
	
		
			
				|  |  | -    //         if (lock(key, lockId, timeout)) {
 | 
	
		
			
				|  |  | -    //             return true;
 | 
	
		
			
				|  |  | -    //         }
 | 
	
		
			
				|  |  | -    //         try {
 | 
	
		
			
				|  |  | -    //             TimeUnit.MILLISECONDS.sleep(interval);
 | 
	
		
			
				|  |  | -    //         } catch (Exception e) {
 | 
	
		
			
				|  |  | -    //             log.error("tryLock error key {} lockId {}", key, lockId, e);
 | 
	
		
			
				|  |  | -    //         }
 | 
	
		
			
				|  |  | -    //         timeout -= interval;
 | 
	
		
			
				|  |  | -    //     }
 | 
	
		
			
				|  |  | -    //     return false;
 | 
	
		
			
				|  |  | -    // }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    // public static Boolean tryLock(String key, String lockId, long timeout) {
 | 
	
		
			
				|  |  | -    //     return tryLock(key, lockId, timeout, 0);
 | 
	
		
			
				|  |  | -    // }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 解锁
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    // public static Boolean unlock(String key, String lockId) {
 | 
	
		
			
				|  |  | -    //     String lockKey = LOCK_PREFIX + key;
 | 
	
		
			
				|  |  | -    //     DefaultRedisScript<Long> script = new DefaultRedisScript<>();
 | 
	
		
			
				|  |  | -    //     script.setResultType(Long.class);
 | 
	
		
			
				|  |  | -    //     script.setScriptSource(new StaticScriptSource(UNLOCK));
 | 
	
		
			
				|  |  | -    //
 | 
	
		
			
				|  |  | -    //     Object result = redisTemplate.execute(script, Collections.singletonList(lockKey), lockId);
 | 
	
		
			
				|  |  | -    //     if (Objects.isNull(result)) {
 | 
	
		
			
				|  |  | -    //         return null;
 | 
	
		
			
				|  |  | -    //     }
 | 
	
		
			
				|  |  | -    //     return Objects.equals(1L, Long.valueOf(result.toString()));
 | 
	
		
			
				|  |  | -    // }
 | 
	
		
			
				|  |  | -    @Autowired
 | 
	
		
			
				|  |  | -    public void setRedisTemplate(@Qualifier("redisTemplate") RedisTemplate<String, String> redisTemplate) {
 | 
	
		
			
				|  |  | -        RedisHelper.redisTemplate = redisTemplate;
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    public Object get(String key) {
 | 
	
		
			
				|  |  | -        return key == null ? null : redisTemplate.opsForValue().get(key);
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    public boolean set(String key, String value, long time) {
 | 
	
		
			
				|  |  | -        try {
 | 
	
		
			
				|  |  | -            if (time > 0) {
 | 
	
		
			
				|  |  | -                redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
 | 
	
		
			
				|  |  | -            } else {
 | 
	
		
			
				|  |  | -                set(key, value);
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -            return true;
 | 
	
		
			
				|  |  | -        } catch (Exception e) {
 | 
	
		
			
				|  |  | -            e.printStackTrace();
 | 
	
		
			
				|  |  | -            return false;
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    public boolean set(String key, String value) {
 | 
	
		
			
				|  |  | -        try {
 | 
	
		
			
				|  |  | -            redisTemplate.opsForValue().set(key, value);
 | 
	
		
			
				|  |  | -            return true;
 | 
	
		
			
				|  |  | -        } catch (Exception e) {
 | 
	
		
			
				|  |  | -            e.printStackTrace();
 | 
	
		
			
				|  |  | -            return false;
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    // public Long decr(String key, long delta) {
 | 
	
		
			
				|  |  | -    //     if (delta < 0) {
 | 
	
		
			
				|  |  | -    //         throw new RuntimeException("递减因子必须大于0");
 | 
	
		
			
				|  |  | -    //     }
 | 
	
		
			
				|  |  | -    //     return redisTemplate.opsForValue().increment(key, -delta);
 | 
	
		
			
				|  |  | -    // }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    public Long incr(String key, long delta) {
 | 
	
		
			
				|  |  | -        if (delta < 0) {
 | 
	
		
			
				|  |  | -            throw new RuntimeException("递增因子必须大于0");
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        return redisTemplate.opsForValue().increment(key, delta);
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    // public void expire(String key, long time) {
 | 
	
		
			
				|  |  | -    //     try {
 | 
	
		
			
				|  |  | -    //         if (time > 0) {
 | 
	
		
			
				|  |  | -    //             redisTemplate.expire(key, time, TimeUnit.SECONDS);
 | 
	
		
			
				|  |  | -    //         }
 | 
	
		
			
				|  |  | -    //     } catch (Exception e) {
 | 
	
		
			
				|  |  | -    //         e.printStackTrace();
 | 
	
		
			
				|  |  | -    //     }
 | 
	
		
			
				|  |  | -    // }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 根据value从一个set中查询,是否存在
 | 
	
		
			
				|  |  | -     *
 | 
	
		
			
				|  |  | -     * @param key   键
 | 
	
		
			
				|  |  | -     * @param value 值
 | 
	
		
			
				|  |  | -     * @return true 存在 false不存在
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    public Boolean sIsMember(String key, Object value) {
 | 
	
		
			
				|  |  | -        try {
 | 
	
		
			
				|  |  | -            return redisTemplate.opsForSet().isMember(key, value);
 | 
	
		
			
				|  |  | -        } catch (Exception e) {
 | 
	
		
			
				|  |  | -            log.error("set has key error", e);
 | 
	
		
			
				|  |  | -            return false;
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 将数据放入set缓存
 | 
	
		
			
				|  |  | -     *
 | 
	
		
			
				|  |  | -     * @param key    键
 | 
	
		
			
				|  |  | -     * @param values 值 可以是多个
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    public void sSet(String key, String... values) {
 | 
	
		
			
				|  |  | -        try {
 | 
	
		
			
				|  |  | -            redisTemplate.opsForSet().add(key, values);
 | 
	
		
			
				|  |  | -        } catch (Exception e) {
 | 
	
		
			
				|  |  | -            log.error("set add is error", e);
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    // public void sRemove(String key, Object... values) {
 | 
	
		
			
				|  |  | -    //     try {
 | 
	
		
			
				|  |  | -    //         redisTemplate.opsForSet().remove(key, values);
 | 
	
		
			
				|  |  | -    //     } catch (Exception e) {
 | 
	
		
			
				|  |  | -    //         log.error("set remove is error", e);
 | 
	
		
			
				|  |  | -    //     }
 | 
	
		
			
				|  |  | -    // }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    public void leftPushAll(String key, List<String> value) {
 | 
	
		
			
				|  |  | -        try {
 | 
	
		
			
				|  |  | -            redisTemplate.opsForList().leftPushAll(key, value);
 | 
	
		
			
				|  |  | -        } catch (Exception e) {
 | 
	
		
			
				|  |  | -            log.error("list left push all error", e);
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 获取list缓存的内容
 | 
	
		
			
				|  |  | -     *
 | 
	
		
			
				|  |  | -     * @param key   键
 | 
	
		
			
				|  |  | -     * @param start 开始
 | 
	
		
			
				|  |  | -     * @param end   结束 0 到 -1代表所有值
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    public List<String> lGet(String key, long start, long end) {
 | 
	
		
			
				|  |  | -        try {
 | 
	
		
			
				|  |  | -            return redisTemplate.opsForList().range(key, start, end);
 | 
	
		
			
				|  |  | -        } catch (Exception e) {
 | 
	
		
			
				|  |  | -            log.error("RedisUtils.lGet.is.error.key={},start={},end={}", key, start, end, e);
 | 
	
		
			
				|  |  | -            return null;
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 获取list缓存的长度
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    public Long lGetListSize(String key) {
 | 
	
		
			
				|  |  | -        try {
 | 
	
		
			
				|  |  | -            return redisTemplate.opsForList().size(key);
 | 
	
		
			
				|  |  | -        } catch (Exception e) {
 | 
	
		
			
				|  |  | -            log.error("get list size error", e);
 | 
	
		
			
				|  |  | -            return 0L;
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    // public Map<String, String> getHashEntries(String key) {
 | 
	
		
			
				|  |  | -    //     try {
 | 
	
		
			
				|  |  | -    //         return redisTemplate.<String, String>opsForHash().entries(key);
 | 
	
		
			
				|  |  | -    //     } catch (Exception e) {
 | 
	
		
			
				|  |  | -    //         log.error("get hash entry error", e);
 | 
	
		
			
				|  |  | -    //         return null;
 | 
	
		
			
				|  |  | -    //     }
 | 
	
		
			
				|  |  | -    // }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 获取某个元素的分数
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    public Double zScore(String key, String value) {
 | 
	
		
			
				|  |  | -        return redisTemplate.opsForZSet().score(key, value);
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    // public Long zRemove(String value, String key) {
 | 
	
		
			
				|  |  | -    //     return redisTemplate.opsForZSet().remove(key, value);
 | 
	
		
			
				|  |  | -    // }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 批量添加
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    // public static void batchSet(Map<String, String> map) {
 | 
	
		
			
				|  |  | -    //     redisTemplate.opsForValue().multiSet(map);
 | 
	
		
			
				|  |  | -    // }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 批量添加 并且设置失效时间
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    public static int batchSetOrExpire(Map<String, String> map, Long seconds) {
 | 
	
		
			
				|  |  | -        RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
 | 
	
		
			
				|  |  | -        return redisTemplate.executePipelined((RedisCallback<String>) connection -> {
 | 
	
		
			
				|  |  | -            map.forEach((key, value) -> {
 | 
	
		
			
				|  |  | -                byte[] keySeria = serializer.serialize(key);
 | 
	
		
			
				|  |  | -                byte[] valueSeria = serializer.serialize(value);
 | 
	
		
			
				|  |  | -                if (keySeria == null || valueSeria == null) {
 | 
	
		
			
				|  |  | -                    return;
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | -                connection.set(keySeria, valueSeria, Expiration.seconds(seconds), RedisStringCommands.SetOption.UPSERT);
 | 
	
		
			
				|  |  | -            });
 | 
	
		
			
				|  |  | -            return null;
 | 
	
		
			
				|  |  | -        }, serializer).size();
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    public void put(String key, String value) {
 | 
	
		
			
				|  |  | -        redisTemplate.opsForValue().set(key, value);
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    public void expire(String key, long day) {
 | 
	
		
			
				|  |  | -        redisTemplate.expire(key, day, TimeUnit.DAYS);
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    public boolean SetContain(String key, String value) {
 | 
	
		
			
				|  |  | -        return redisTemplate.opsForSet().isMember(key, value);
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    // public static List<String> batchGet(List<String> list) {
 | 
	
		
			
				|  |  | -    //     List<String> objectList = redisTemplate.opsForValue().multiGet(list);
 | 
	
		
			
				|  |  | -    //     return objectList;
 | 
	
		
			
				|  |  | -    // }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    // public static void batchDelete(List<String> list) {
 | 
	
		
			
				|  |  | -    //     redisTemplate.delete(list);
 | 
	
		
			
				|  |  | -    // }
 | 
	
		
			
				|  |  | -}
 |