功能说明:在项目中,将一些简单的需要高频率的使用的字段加载在缓存中,减少与数据库的连接!
具体设计:
总体分为两块:
第一块是数据的加载:
1,首先是xml
<servlet>
<servlet>
<servlet-name>dhservlet</servlet-name>
<servlet-class>***.***.cache.servlet.DictionaryHelperServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
2然后是该指定类:
public class DictionaryHelperServlet extends HttpServlet{
/**
* 系统启动初始化加载字典缓存
*/
@Override
public void init() throws ServletException {
DictionaryHelper dictionaryHelper = new DictionaryHelper();
dictionaryHelper.joinCache();
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
}
3然后在调用数据库中的数据存入缓存中
public class DictionaryHelper {
private int t = 0;
/**
* 加入缓存
*/
public void joinCache(){
Map map = getDictCacheMap();
LoginCache.put(LoginConfig.USER_DICTIONARY_HELPER, LoginConfig.DICTIONARY_HELPER, map, 3600);
}
public static String toJson(String dictId){
DictionaryHelper dh = new DictionaryHelper();
return StringUtil.getSelectJson(dh.getDictById(dictId), false);
}
/**
* 重新封装字典
* @return
*/
private Map getDictCacheMap(){
Map<String,List<DictDataInfo>> retMap = new HashMap<String, List<DictDataInfo>>();
List<DictManagerInfo> dmList = getDictionaryHelper();
List<DictDataInfo> ddList = getDictInfoList();
if(dmList != null){
List<DictDataInfo> retList = null;
for (int i = 0; i < dmList.size(); i++) {
DictManagerInfo managerInfo = dmList.get(i);
for(int j = 0; j < ddList.size(); j++){
DictDataInfo dataInfo = ddList.get(j);
if(managerInfo.getDict_code().equals(dataInfo.getDict_code())){
retList = retMap.get(managerInfo.getDict_code());
if(retList == null){
retList = new ArrayList<DictDataInfo>();
}
retList.add(dataInfo);
System.out.println("a"+managerInfo.getDict_code());
retMap.put(managerInfo.getDict_code(), retList);
}
}
}
}
return retMap;
}
/**
* 取所有父字典信息
* @return
*/
private List<DictManagerInfo> getDictionaryHelper(){
CompService com = BaseDAOFactory.getCompService();
Map dictmap = (Map)com.invoke("DictManagerData", "getDictManagerLists");
String rnt = "";
if(dictmap!=null){
rnt = StringUtil.toString(dictmap.get("rnt"));
}
List<DictManagerInfo> list = null;
if("1".equals(rnt)){
list = (List<DictManagerInfo>) dictmap.get("dmList");
}
return list;
}
/**
* 取所有子字典信息
* @return
*/
private List<DictDataInfo> getDictInfoList(){
CompService com = BaseDAOFactory.getCompService();
Map dictmap = (Map)com.invoke("DictManagerData", "getAllDictDataInfo");
String rnt = "";
if(dictmap!=null){
rnt =