You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
udi-spms-java/src/main/java/com/glxp/api/util/CacheUtils.java

130 lines
3.3 KiB
Java

2 years ago
package com.glxp.api.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
*
*/
//@Component
public class CacheUtils {
@Autowired
private RedisTemplate<String, String> redisTemplate;
// 维护一个本类的静态变量
private static CacheUtils cacheUtils;
// @PostConstruct
public void init() {
cacheUtils = this;
cacheUtils.redisTemplate = this.redisTemplate;
}
/**
*
* @param key
* @param value Serializable
*/
public static void set(String key, String value) {
cacheUtils.redisTemplate.opsForValue().set(key, value);
}
/**
*
* @param key
* @param value Serializable
* @param timeout
*/
public static void set(String key, String value, Long timeout) {
cacheUtils.redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
}
/**
*
* @param key
* @return
*/
public static Object get(String key) {
return cacheUtils.redisTemplate.opsForValue().get(key);
}
/**
*
* @param key
* @param ttl
*/
public static boolean expire(String key, Long ttl) {
return cacheUtils.redisTemplate.expire(key, ttl, TimeUnit.SECONDS);
}
/**
*
* @param key
*/
public static boolean hasKey(String key) {
return cacheUtils.redisTemplate.hasKey(key);
}
/**
*
* @param key
* @param value
* @return value
*/
public static Long sAdd(String key, String... value) {
return cacheUtils.redisTemplate.opsForSet().add(key, value);
}
/**
*
* @param key
* @return rediskeyvalueSet
*/
public static Set<String> sGetMembers(String key) {
return cacheUtils.redisTemplate.opsForSet().members(key);
}
/**
*
* @param key
* @param value
* @param score
* @return
*/
public static Boolean zAdd(String key, String value, double score) {
return cacheUtils.redisTemplate.opsForZSet().add(key, value, score);
}
/**
*
* @param key
* @param value
* @return
*/
public static Double zScore(String key, String value) {
return cacheUtils.redisTemplate.opsForZSet().score(key, value);
}
/**
*
* @param key
* @return
*/
public static Boolean delete(String key) {
return cacheUtils.redisTemplate.delete(key);
}
/**
*
* @param keys
* @return
*/
public static Long delete(Collection<String> keys) {
return cacheUtils.redisTemplate.delete(keys);
}
}