package com.alatus.service.impl;
import com.alatus.service.RedisService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Component
public class RedisServiceImpl implements RedisService {
// 数据源
@Resource
private RedisTemplate<String,Object> redisTemplate;
// 放入数据
@Override
public void setValue(String key, Object value) {
redisTemplate.opsForValue().set(key,value);
}
//取出数据
@Override
public Object getValue(String key) {
return redisTemplate.opsForValue().get(key);
}
//删除数据
@Override
public Boolean removeValue(String key) {
return redisTemplate.delete(key);
}
@Override
public Boolean expire(String key, Long timeOut, TimeUnit timeUnit) {
return redisTemplate.expire(key,timeOut,timeUnit);
}
@Override
public List<Object> rangeAll(String key) {
return redisTemplate.opsForList().range(key, 0, -1);
}
@Override
public <V> Long listPush(String key, Collection<V> collection) {
Object[] t = new Object[collection.size()];
collection.toArray(t);
return redisTemplate.opsForList().leftPushAll(key,t);
}
}
package com.alatus.service.impl;
import com.alatus.service.RedisService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Component
public class RedisServiceImpl implements RedisService {
// 数据源
@Resource
private RedisTemplate<String,Object> redisTemplate;
// 放入数据
@Override
public void setValue(String key, Object value) {
redisTemplate.opsForValue().set(key,value);
}
//取出数据
@Override
public Object getValue(String key) {
return redisTemplate.opsForValue().get(key);
}
//删除数据
@Override
public Boolean removeValue(String key) {
return redisTemplate.delete(key);
}
@Override
public Boolean expire(String key, Long timeOut, TimeUnit timeUnit) {
return redisTemplate.expire(key,timeOut,timeUnit);
}
@Override
public List<Object> rangeAll(String key) {
return redisTemplate.opsForList().range(key, 0, -1);
}
@Override
public <V> Long listPush(String key, Collection<V> collection) {
Object[] t = new Object[collection.size()];
collection.toArray(t);
return redisTemplate.opsForList().leftPushAll(key,t);
}
}
package com.alatus.service;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
public interface RedisService {
void setValue(String key,Object value);
Object getValue(String key);
Boolean removeValue(String key);
Boolean expire(String key, Long timeOut, TimeUnit timeUnit);
List<Object> rangeAll(String key);
<V> Long listPush(String key, Collection<V> values);
}
package com.alatus.service;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
public interface RedisService {
void setValue(String key,Object value);
Object getValue(String key);
Boolean removeValue(String key);
Boolean expire(String key, Long timeOut, TimeUnit timeUnit);
List<Object> rangeAll(String key);
<V> Long listPush(String key, Collection<V> values);
}
package com.alatus.manager;
import com.alatus.constant.Constants;
import com.alatus.service.RedisService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
@Component
public class RedisManager {
@Resource
private RedisTemplate<String,Object> redisTemplate;
@Resource
private RedisService redisService;
public Object getValue(String key){
Object object = redisService.rangeAll(key);
redisService.expire(key, Constants.DEFAULT_KEY_EXPIRE_TIME, TimeUnit.SECONDS);
return object;
}
public <T> Object setValue(String key, Collection<T> data){
Long result = redisService.listPush(key, data);
redisService.expire(key, Constants.DEFAULT_KEY_EXPIRE_TIME, TimeUnit.SECONDS);
return result;
}
}
package com.alatus.manager;
import com.alatus.constant.Constants;
import com.alatus.service.RedisService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
@Component
public class RedisManager {
@Resource
private RedisTemplate<String,Object> redisTemplate;
@Resource
private RedisService redisService;
public Object getValue(String key){
Object object = redisService.rangeAll(key);
redisService.expire(key, Constants.DEFAULT_KEY_EXPIRE_TIME, TimeUnit.SECONDS);
return object;
}
public <T> Object setValue(String key, Collection<T> data){
Long result = redisService.listPush(key, data);
redisService.expire(key, Constants.DEFAULT_KEY_EXPIRE_TIME, TimeUnit.SECONDS);
return result;
}
}