1. 修正了 redis-starter 中 cache 反序列化的问题

2. 增加了 redis-cache 实例
This commit is contained in:
Administrator
2020-11-03 16:02:24 +08:00
parent 101e1a87ce
commit b8b47bf595
11 changed files with 279 additions and 76 deletions

View File

@ -0,0 +1,13 @@
package com.chinaunicom.mall.ebtp.cloud.redis.cache.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class RedisCacheExampleApplication {
public static void main(String[] args) {
SpringApplication.run(RedisCacheExampleApplication.class, args);
}
}

View File

@ -0,0 +1,19 @@
package com.chinaunicom.mall.ebtp.cloud.redis.cache.example.model;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String username;
private Integer age;
}

View File

@ -0,0 +1,22 @@
package com.chinaunicom.mall.ebtp.cloud.redis.cache.example.service;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import com.chinaunicom.mall.ebtp.cloud.redis.cache.example.model.User;
@CacheConfig(cacheNames = "user")
public interface ExampleService {
@CachePut(key = "#user.id")
User update(User user);
@CacheEvict(key = "#id", allEntries = true)
void deleteById(Integer id);
@Cacheable(key = "#id")
User queryById(Integer id);
}

View File

@ -0,0 +1,55 @@
package com.chinaunicom.mall.ebtp.cloud.redis.cache.example.service.impl;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.chinaunicom.mall.ebtp.cloud.redis.cache.example.model.User;
import com.chinaunicom.mall.ebtp.cloud.redis.cache.example.service.ExampleService;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
public class ExampleServiceImpl implements ExampleService {
private ConcurrentHashMap<Integer, User> userRepo = new ConcurrentHashMap<>();
ExampleServiceImpl() {
log.info("Example database initial");
for (int i = 1; i <= 10; i++) {
userRepo.put(i, new User(i, "test" + i, i + 20));
}
}
@Override
public User update(User user) {
log.info(String.format("Find user by id [%s]", user.getId()));
User target = userRepo.get(user.getId());
if (!Objects.isNull(target)) {
BeanUtils.copyProperties(user, target);
}
return target;
}
@Override
public void deleteById(Integer id) {
log.info(String.format("Delete user by id [%s]", id));
userRepo.remove(id);
}
@Override
public User queryById(Integer id) {
log.info(String.format("Query user by id [%s]", id));
// List<User> list = new ArrayList<User>();
//
// for(int i = 1; i < id; i++) {
// list.add(userRepo.get(i));
// }
// return list;
return userRepo.get(id);
}
}

View File

@ -0,0 +1,16 @@
spring:
redis:
database: 0
host: 125.32.114.204
password: redis@CC1234
port: 16379
timeout: 6000
ssl: false
lettuce:
pool:
max-wait: -1ms
max-active: 8
max-idle: 8
min-idle: 0
cache:
cacheNames: demo

View File

@ -0,0 +1,10 @@
spring:
profiles:
active: redis
application:
name: mall-ebtp-cloud-demo
logging:
level:
root: info