功能设计源码以及思路-字典篇

本文介绍了一个项目中如何利用缓存来提高高频率使用字段的效率,通过XML配置和Servlet实现系统启动时加载字典数据到缓存,详细阐述了加载、更新和查询字典缓存的代码实现。
摘要由CSDN通过智能技术生成
功能说明:在项目中,将一些简单的需要高频率的使用的字段加载在缓存中,减少与数据库的连接!
具体设计:
  总体分为两块:
              第一块是数据的加载:
          1,首先是xml
  <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 =
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值