2商品分类信息实现redis操作
2.1业务分析
2.1.1什么样的数据添加缓存
条件:
1.不经常变化的数据
2.用户频繁查询的数据.
实际业务:
1.单个商品可以添加缓存.
2.省市县乡
3.营业厅信息.
4.商品分类信息….
2.2SpringBoot整合redis
2.2.1编辑Properties文件
#单台redis配置
redis.host=192.168.226.128
redis.port=6379
2.2.2编辑redis的配置类
//标识配置类信息
@Configuration
@PropertySource("classpath:/properties/redis.properties")
public class RedisConfig {
@Value("${redis.host}")
private String host;
@Value("${redis.port}")
private Integer port;
/**Springboot整合redis jedis*/
@Bean
@Scope("prototype")
public Jedis jedis() {
return new Jedis(host, port);
}
}
2.2.3对象与JSON转化问题
public class ObjectToJSON {
private static final ObjectMapper MAPPER = new ObjectMapper();
//对象转化为json
@Test
public void toJSON() throws JsonProcessingException {
ItemDesc desc = new ItemDesc();
desc.setItemDesc("测试JSON")
.setItemId(100L)
.setCreated(new Date())
.setUpdated(desc.getCreated());
String json = MAPPER.writeValueAsString(desc);
System.out.println(json);
//2.将json串转化为对象
ItemDesc itemDesc2 = MAPPER.readValue(json, ItemDesc.class);
System.out.println(itemDesc2.getItemDesc());
}
}
2.2.4编辑ObjectMapperUtil工具API
说明:为了简化调用者的代码的结构,可以使用API方式进行封装.
public class ObjectMapperUtil {
/**
* 1.对象转化为JSON
* 2.JSON转化为对象
*/
private static final ObjectMapper MAPPER
= new ObjectMapper();
public static String toJSON(Object obj) {
String result = null;
try {
result = MAPPER.writeValueAsString(obj);
} catch (JsonProcessingException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return result;
}
/**
* 需求:添加class类型,直接返回该类型对象
* <T> 定义泛型对象
* T 对象的引用. 前后一致即可.
* @param json
* @param target
* @return
*/
public <T> T toObj(String json,Class<T> target){
T t = null;
try {
t = MAPPER.readValue(json, target);
} catch (JsonProcessingException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return t;
}
}
2.2.5商品分类缓存实现业务流程
1.用户发起请求 http://manage.jt.com/item/cat/list
2.ItemCatController接收用户请求.
@RequestMapping("/list")
public List<EasyUITree> findItemCatByParentId(@RequestParam(defaultValue="0",name = "id") Long parentId){
return itemCatService.findItemCatByParentId(parentId);
}
3.根据业务需求查询数据库,.之后返回数据.
4.引入redis缓存实质减少了用户查询数据库的操作步骤.
1Redis
1.1商品分类缓存实现
1.1.1编辑ItemCatController
/**
* url:/item/cat/list
* 参数: 暂时忽略
* 返回值: List<EasyUITree>
*
* @RequestParam
* defaultValue: 默认值 如果不传数据则使用默认值
* name/value: 表示接收的数据.
* required: 是否为必传数据. 默认值true
*/
@RequestMapping("/list")
public List<EasyUITree> findItemCatByParentId(@RequestParam(defaultValue="0",name = "id") Long parentId){
//return itemCatService.findItemCatByParentId(parentId);
//使用redis实现数据查询 注意是临时方法!!!!!!
return itemCatService.findItemCatCache(parentId);
}
1.1.2编辑ItemCatService
/**
* 思路:
* 1.先查询缓存 key: com.jt.service.ItemCatServiceImpl.findItemCatCache::parentId
* 2.判断结果信息
*/
@SuppressWarnings("unchecked") //压制警告
@Override
public List<EasyUITree> findItemCatCache(Long parentId) {
Long startTime = System.currentTimeMillis();
String key = "com.jt.service.ItemCatServiceImpl.findItemCatCache::"+parentId;
String value = jedis.get(key);
List<EasyUITree> treeList = new ArrayList<>();
if(StringUtils.isEmpty(value)) {
//说明用户第一次查询.
treeList = findItemCatByParentId(parentId);
String json = ObjectMapperUtil.toJSON(treeList);
jedis.set(key, json);
Long endTime = System.currentTimeMillis();
Long time = endTime - startTime;
System.out.println("第一次查询,耗时:"+time+"毫秒");
}else {
//不是第一次查询. 获取json数据,转化为对象
treeList = ObjectMapperUtil.toObj(value, treeList.getClass());
Long endTime = System.currentTimeMillis();
Long time = endTime - startTime;
System.out.println("查询缓存,耗时:"+time+"毫秒");
}
return treeList;
}
1.1.3Redis性能提升
没有使用redis之前.
2.使用redis之后.
1.1.4全局异常处理机制
@RestControllerAdvice //返回值结果都是JSON串
public class SysResultExceptionAOP {
/**
* 统一返回数据 SysResult对象 status=201
*/
@ExceptionHandler(RuntimeException.class)
public SysResult fail(Exception exception) {
exception.printStackTrace();
return SysResult.fail();
}}