Redis 工具类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component

public class RedisUtil {

@Autowired
private StringRedisTemplate stringRedisTemplate;

@Autowired
private RedisTemplate<Object, Object> redisTemplate;

public void set(String key, String value) {
    stringRedisTemplate.opsForValue().set(key, value);
}

public void set(String key, String value, long time) {
    stringRedisTemplate.opsForValue().set(key, value, time);
}

public void set(String key, String value, long time, TimeUnit unit) {
    // 向redis里存入数据和设置缓存时间
    stringRedisTemplate.opsForValue().set(key, value, time, unit);
}

public String get(String key) {
    return stringRedisTemplate.opsForValue().get(key);
}

public Set<String> getKeys(String pattern) {
    return stringRedisTemplate.keys(pattern);
}

public void delete(String key) {
    stringRedisTemplate.delete(key);
}
上一篇:SpringBoot手写启动器


下一篇:2021-10-14