com.feingto.cloud.cache.provider.RedisProvider Maven / Gradle / Ivy
package com.feingto.cloud.cache.provider;
import com.feingto.cloud.cache.ICache;
import com.feingto.cloud.cache.RedisManager;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Redis 缓存操作
*
* @author longfei
*/
@SuppressWarnings("unchecked")
public class RedisProvider implements ICache {
private RedisManager redisManager;
public RedisProvider(RedisTemplate template) {
redisManager = RedisManager.builder().template(template).build();
}
@Override
public T get(String key) {
return (T) redisManager.getValueStore().get(key);
}
@Override
public void put(String key, Object obj) {
redisManager.getValueStore().set(key, obj);
}
@Override
public void put(String key, Object obj, long expireTime) {
redisManager.getValueStore().set(key, obj, expireTime, TimeUnit.MILLISECONDS);
}
@Override
public boolean has(String key) {
return redisManager.getValueStore().get(key) != null;
}
@Override
public void remove(String key) {
redisManager.getTemplate().delete(key);
}
@Override
public void removeByPrefix(String prefix) {
Optional.ofNullable(redisManager.getTemplate().keys(prefix + "*"))
.ifPresent(keys -> keys.stream()
.filter(key -> key.startsWith(prefix))
.forEach(this::remove));
}
@Override
public void clear() {
redisManager.getTemplate().execute((RedisCallback
© 2015 - 2025 Weber Informatics LLC | Privacy Policy