首页数据添加一个简单的缓存
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package org.linlinjava.litemall.wx.service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 简单缓存首页的数据
|
||||
*/
|
||||
public class HomeCacheManager {
|
||||
private static Map<String, Object> cacheData;
|
||||
|
||||
/**
|
||||
* 缓存首页数据
|
||||
*
|
||||
* @param data
|
||||
*/
|
||||
public static void loadData(Map<String, Object> data) {
|
||||
cacheData = data;
|
||||
}
|
||||
|
||||
public static Map<String, Object> getCacheData() {
|
||||
return cacheData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断缓存中是否有数据
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static boolean hasData() {
|
||||
return cacheData != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存数据
|
||||
*/
|
||||
public static void clear() {
|
||||
cacheData = null;
|
||||
}
|
||||
}
|
||||
@@ -4,12 +4,14 @@ import org.linlinjava.litemall.core.util.ResponseUtil;
|
||||
import org.linlinjava.litemall.db.domain.*;
|
||||
import org.linlinjava.litemall.db.service.*;
|
||||
import org.linlinjava.litemall.core.system.SystemConfig;
|
||||
import org.linlinjava.litemall.wx.service.HomeCacheManager;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -32,6 +34,22 @@ public class WxHomeController {
|
||||
@Autowired
|
||||
private LitemallGrouponRulesService grouponRulesService;
|
||||
|
||||
|
||||
@GetMapping("/cache")
|
||||
public Object cache(@NotNull Integer key) {
|
||||
if (!key.equals("litemall_cache")) {
|
||||
return ResponseUtil.fail();
|
||||
}
|
||||
|
||||
// 清除缓存
|
||||
HomeCacheManager.clear();
|
||||
if (!HomeCacheManager.hasData()) {
|
||||
return ResponseUtil.ok();
|
||||
} else {
|
||||
return ResponseUtil.fail();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* app首页
|
||||
*
|
||||
@@ -54,6 +72,12 @@ public class WxHomeController {
|
||||
*/
|
||||
@GetMapping("/index")
|
||||
public Object index() {
|
||||
//优先从缓存中读取
|
||||
if (HomeCacheManager.hasData()) {
|
||||
return ResponseUtil.ok(HomeCacheManager.getCacheData());
|
||||
}
|
||||
|
||||
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
|
||||
List<LitemallAd> banner = adService.queryIndex();
|
||||
@@ -109,6 +133,8 @@ public class WxHomeController {
|
||||
}
|
||||
data.put("floorGoodsList", categoryList);
|
||||
|
||||
//缓存数据
|
||||
HomeCacheManager.loadData(data);
|
||||
return ResponseUtil.ok(data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user