parent
917fb69bc9
commit
be0bce1f09
@ -0,0 +1,89 @@
|
||||
package com.yeqifu.sys.cache;
|
||||
|
||||
import com.yeqifu.bus.entity.Customer;
|
||||
import com.yeqifu.bus.entity.Goods;
|
||||
import com.yeqifu.bus.entity.Provider;
|
||||
import com.yeqifu.bus.mapper.CustomerMapper;
|
||||
import com.yeqifu.bus.mapper.GoodsMapper;
|
||||
import com.yeqifu.bus.mapper.ProviderMapper;
|
||||
import com.yeqifu.sys.common.SpringUtil;
|
||||
import com.yeqifu.sys.entity.Dept;
|
||||
import com.yeqifu.sys.entity.User;
|
||||
import com.yeqifu.sys.mapper.DeptMapper;
|
||||
import com.yeqifu.sys.mapper.UserMapper;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author: 落亦-
|
||||
* @Date: 2019/12/20 18:05
|
||||
*/
|
||||
public class CachePool {
|
||||
|
||||
/**
|
||||
* 所有的缓存数据放到这个CACHE_CONTAINER类似于redis
|
||||
*/
|
||||
public static volatile Map<String,Object> CACHE_CONTAINER = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 根据KEY删除缓存
|
||||
* @param key
|
||||
*/
|
||||
public static void removeCacheByKey(String key){
|
||||
if (CACHE_CONTAINER.containsKey(key)){
|
||||
CACHE_CONTAINER.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空所有缓存
|
||||
*/
|
||||
public static void removeAll(){
|
||||
CACHE_CONTAINER.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步缓存
|
||||
*/
|
||||
public static void syncData(){
|
||||
//同步部门数据
|
||||
DeptMapper deptMapper = SpringUtil.getBean(DeptMapper.class);
|
||||
List<Dept> deptList = deptMapper.selectList(null);
|
||||
for (Dept dept : deptList) {
|
||||
CACHE_CONTAINER.put("dept:"+dept.getId(),dept);
|
||||
}
|
||||
//同步用户数据
|
||||
UserMapper userMapper = SpringUtil.getBean(UserMapper.class);
|
||||
List<User> userList = userMapper.selectList(null);
|
||||
for (User user : userList) {
|
||||
CACHE_CONTAINER.put("user:"+user.getId(),user);
|
||||
}
|
||||
|
||||
//同步客户数据
|
||||
CustomerMapper customerMapper = SpringUtil.getBean(CustomerMapper.class);
|
||||
List<Customer> customerList = customerMapper.selectList(null);
|
||||
for (Customer customer : customerList) {
|
||||
CACHE_CONTAINER.put("customer:"+customer.getId(),customer);
|
||||
}
|
||||
|
||||
//同步供应商数据
|
||||
ProviderMapper providerMapper = SpringUtil.getBean(ProviderMapper.class);
|
||||
List<Provider> providerList = providerMapper.selectList(null);
|
||||
for (Provider provider : providerList) {
|
||||
CACHE_CONTAINER.put("provider:"+provider.getId(),provider);
|
||||
}
|
||||
|
||||
//同步商品数据
|
||||
GoodsMapper goodsMapper = SpringUtil.getBean(GoodsMapper.class);
|
||||
List<Goods> goodsList = goodsMapper.selectList(null);
|
||||
for (Goods goods : goodsList) {
|
||||
CACHE_CONTAINER.put("goods:"+goods.getId(),goods);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.yeqifu.sys.common;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
||||
/**
|
||||
* @Author: 落亦-
|
||||
* @Date: 2019/12/20 18:40
|
||||
*/
|
||||
public class CacheBean {
|
||||
|
||||
private String key;
|
||||
|
||||
private Object value;
|
||||
|
||||
public CacheBean() {
|
||||
}
|
||||
|
||||
public CacheBean(String key, Object value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return JSON.toJSON(value).toString();
|
||||
}
|
||||
|
||||
public void setValue(Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.yeqifu.sys.common;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @Author: 落亦-
|
||||
* @Date: 2019/12/20 18:23
|
||||
*/
|
||||
@Component
|
||||
public class SpringUtil implements ApplicationContextAware {
|
||||
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext act) throws BeansException {
|
||||
applicationContext = act;
|
||||
}
|
||||
|
||||
public static ApplicationContext getApplicationContext(){
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
public static <T> T getBean(Class<T> cls){
|
||||
return applicationContext.getBean(cls);
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package com.yeqifu.sys.controller;
|
||||
|
||||
import com.yeqifu.sys.cache.CachePool;
|
||||
import com.yeqifu.sys.common.CacheBean;
|
||||
import com.yeqifu.sys.common.DataGridView;
|
||||
import com.yeqifu.sys.common.ResultObj;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 缓存管理控制器
|
||||
* @Author: 落亦-
|
||||
* @Date: 2019/12/20 18:36
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("cache")
|
||||
public class CacheController {
|
||||
|
||||
private static volatile Map<String,Object> CACHE_CONTAINER= CachePool.CACHE_CONTAINER;
|
||||
|
||||
/**
|
||||
* 查询所有缓存
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("loadAllCache")
|
||||
public DataGridView loadAllCache(){
|
||||
List<CacheBean> list = new ArrayList<>();
|
||||
|
||||
Set<Map.Entry<String, Object>> entrySet = CACHE_CONTAINER.entrySet();
|
||||
for (Map.Entry<String, Object> entry : entrySet) {
|
||||
list.add(new CacheBean(entry.getKey(),entry.getValue()));
|
||||
}
|
||||
return new DataGridView(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("deleteCache")
|
||||
public ResultObj deleteCache(String key){
|
||||
CachePool.removeCacheByKey(key);
|
||||
return ResultObj.DELETE_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空所有缓存
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("removeAllCache")
|
||||
public ResultObj removeAllCache(){
|
||||
CachePool.removeAll();
|
||||
return ResultObj.DELETE_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步缓存
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("syncCache")
|
||||
public ResultObj syncCache(){
|
||||
CachePool.syncData();
|
||||
return ResultObj.SYNCCACHE_SUCCESS;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue