SpringBoot_Redis
第一步
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.71</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
第二步
spring:
redis:
host: 122.112.166.49
port: 6379
database: 1
password: Galaxyeye01
lettuce:
pool:
max-active: 50
max-wait: 3000
max-idle: 20
min-idle: 1000
spring:
redis:
cluster:
nodes: 172.16.3.231:6379,172.16.3.232:6379,172.16.3.233:6379
database: 0
password: galaxyeye
jedis:
pool:
max-wait: 3000
max-idle: 20
min-idle: 2
timeout: 5000
第三步
@Component
public class FastJsonRedisSerializer<T> implements RedisSerializer<T> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private Class<T> clazz;
public FastJsonRedisSerializer(Class<T> clazz) {
super();
this.clazz = clazz;
}
@Override
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length <= 0) {
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
return (T) JSON.parseObject(str, clazz);
}
}
第四步
@Configuration
@EnableCaching
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory connectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<Object>(Object.class);
redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
redisTemplate.setValueSerializer(fastJsonRedisSerializer);
redisTemplate.setConnectionFactory(connectionFactory);
ParserConfig.getGlobalInstance().addAccept("com.example");
return redisTemplate;
}
}
第五步
@Component
public interface IRedisService {
<T> ValueOperations<String, T> append(String key, String appendValue);
<T> ValueOperations<String, T> setCacheObject(String key, T value);
<T> ValueOperations<String, T> setCacheObject(String key, T value, Integer timeout, TimeUnit timeUnit);
<T> ValueOperations<String, T> setCacheObject(String key, T value, Duration timeout);
<T> T getCacheObject(String key);
Boolean setExpire(String key, Long timeout, TimeUnit timeUnit);
void deleteObject(String key);
void deleteObject(Collection collection);
<T> ListOperations<String, T> setCacheList(String key, List<T> dataList);
<T> List<T> getCacheList(String key);
<T> BoundSetOperations<String, T> setCacheSet(String key, Set<T> dataSet);
<T> Set<T> getCacheSet(String key);
void hashPutAll(String key, Map<String, Object> dataMap);
void hashPut(String key, String field, Object value);
void hashPutIfAbsent(String key, String field, Object value);
Map<String, Object> hashGetAll(String key);
Object hashGet(String key, String field);
Set<String> hashKeys(String key);
List<Object> hashValues(String key);
Long hashIncrement(String key, String field, long increment);
Double hashIncrement(String key, String field, double increment);
Boolean hasKey(String key, String hashKey);
void hashDelete(String key, String... fields);
Collection<String> keys(String pattern);
Long increment(String key);
Long increment(String key, long num);
RedisTemplate getRedisTemplate();
Object execute(DefaultRedisScript redisScript, String key, Object value);
}
第六步
@Service
public class RedisServiceImpl implements IRedisService {
@Resource
private RedisTemplate redisTemplate;
public <T> ValueOperations<String, T> append(String key, String appendValue) {
ValueOperations<String, T> operations = this.redisTemplate.opsForValue();
operations.append(key, appendValue);
return operations;
}
public <T> ValueOperations<String, T> setCacheObject(String key, T value) {
ValueOperations<String, T> operations = this.redisTemplate.opsForValue();
operations.set(key, value);
return operations;
}
public <T> ValueOperations<String, T> setCacheObject(String key, T value, Integer timeout, TimeUnit timeUnit) {
ValueOperations<String, T> operations = this.redisTemplate.opsForValue();
operations.set(key, value, timeout, timeUnit);
return operations;
}
public <T> ValueOperations<String, T> setCacheObject(String key, T value, Duration timeout) {
ValueOperations<String, T> operations = this.redisTemplate.opsForValue();
operations.set(key, value, timeout);
return operations;
}
public <T> T getCacheObject(String key) {
ValueOperations<String, T> operation = this.redisTemplate.opsForValue();
return operation.get(key);
}
public Boolean setExpire(String key, Long timeout, TimeUnit timeUnit) {
return this.redisTemplate.expire(key, timeout, timeUnit);
}
public void deleteObject(String key) {
this.redisTemplate.delete(key);
}
public void deleteObject(Collection collection) {
this.redisTemplate.delete(collection);
}
public <T> ListOperations<String, T> setCacheList(String key, List<T> dataList) {
ListOperations listOperations = this.redisTemplate.opsForList();
if (null != listOperations) {
int size = dataList.size();
for (int i = 0; i < size; i++) {
listOperations.leftPush(key, dataList.get(i));
}
}
return listOperations;
}
public <T> List<T> getCacheList(String key) {
List<T> dataList = new ArrayList<>();
ListOperations<String, T> listOperations = this.redisTemplate.opsForList();
Long size = listOperations.size(key);
for (long i = 0; i < size; i++) {
dataList.add(listOperations.index(key, i));
}
return dataList;
}
public <T> BoundSetOperations<String, T> setCacheSet(String key, Set<T> dataSet) {
BoundSetOperations<String, T> setOperations = this.redisTemplate.boundSetOps(key);
Iterator<T> iterable = dataSet.iterator();
while (iterable.hasNext()) {
setOperations.add(iterable.next());
}
return setOperations;
}
public <T> Set<T> getCacheSet(String key) {
Set<T> dataSet = new HashSet<>();
BoundSetOperations<String, T> setOperations = this.redisTemplate.boundSetOps(key);
Long size = setOperations.size();
for (long i = 0; i < size; i++) {
dataSet.add(setOperations.pop());
}
return dataSet;
}
public void hashPutAll(String key, Map<String, Object> dataMap) {
this.redisTemplate.opsForHash().putAll(key, dataMap);
}
public void hashPut(String key, String field, Object value) {
this.redisTemplate.opsForHash().put(key, field, value);
}
public void hashPutIfAbsent(String key, String field, Object value) {
this.redisTemplate.opsForHash().putIfAbsent(key, field, value);
}
public Map<String, Object> hashGetAll(String key) {
return this.redisTemplate.opsForHash().entries(key);
}
public Object hashGet(String key, String field) {
return this.redisTemplate.opsForHash().get(key, field);
}
public Set<String> hashKeys(String key) {
return this.redisTemplate.opsForHash().keys(key);
}
public List<Object> hashValues(String key) {
return this.redisTemplate.opsForHash().values(key);
}
public Long hashIncrement(String key, String field, long increment) {
return this.redisTemplate.opsForHash().increment(key, field, increment);
}
public Double hashIncrement(String key, String field, double increment) {
return this.redisTemplate.opsForHash().increment(key, field, increment);
}
public Boolean hasKey(String key, String hashKey) {
return this.redisTemplate.opsForHash().hasKey(key, hashKey);
}
public void hashDelete(String key, String... fields) {
this.redisTemplate.opsForHash().delete(key, fields);
}
public Collection<String> keys(String pattern) {
return this.redisTemplate.keys(pattern);
}
public Long increment(String key) {
return this.redisTemplate.opsForValue().increment(key);
}
public Long increment(String key, long num) {
return this.redisTemplate.opsForValue().increment(key, num);
}
public RedisTemplate getRedisTemplate() {
return this.redisTemplate;
}
public Object execute(DefaultRedisScript redisScript, String key, Object value) {
return this.redisTemplate.execute(redisScript, Arrays.asList(key), value);
}
}
第七步
@RestController
public class RedisController {
@Resource
private IRedisService redisService;
@PostMapping("getRedis/01")
public String getRedis_01(String message) {
Object cacheObject = redisService.getCacheObject(message);
if (ObjectUtils.isNotEmpty(cacheObject)) {
} else {
}
redisService.setCacheObject(message, message);
return "success";
}
}