国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

利用緩存機(jī)制快速讀取XML文件數(shù)據(jù)

2019-11-18 12:47:07
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

  接到一個(gè)任務(wù),讓我做一個(gè)公司網(wǎng)站的后臺(tái)治理系統(tǒng)。要求很簡(jiǎn)單,就一個(gè)新聞發(fā)布模塊和一個(gè)招聘信息發(fā)布模塊。但不能用DB,只能用文件存取的形式實(shí)現(xiàn)。
  
  不用考慮肯定是用xml文件進(jìn)行數(shù)據(jù)的存取了,以前做畢設(shè)的時(shí)候也曾經(jīng)實(shí)現(xiàn)過(guò)類似的功能,所以關(guān)于XML的讀取并沒(méi)有問(wèn)題。要害是假如考慮到性能的問(wèn)題就值得推敲一下了,我是新人,以前也沒(méi)做過(guò)什么設(shè)計(jì),所以做出的東西在一些人眼中可能會(huì)有些稚嫩,那也沒(méi)關(guān)系,走自己的路讓別人去說(shuō)吧:)
  
  假如頻繁解析文件,速度肯定受到影響,在文件非常大的情況下,甚至是無(wú)法忍受的。假如在服務(wù)器啟動(dòng)的時(shí)候先把文件中的數(shù)據(jù)封裝成對(duì)象數(shù)組讀入到緩存中,每次訪問(wèn)的時(shí)候先判定一下要訪問(wèn)的文件實(shí)體有沒(méi)有被更新,假如沒(méi)有被更新,就直接從緩存中將想要的數(shù)據(jù)讀出來(lái),當(dāng)然假如文件被更新了,那只好老老實(shí)實(shí)的解析文件讀出想要的數(shù)據(jù)。治理者對(duì)文件的修改次數(shù)究竟是少數(shù),更多的是訪問(wèn)者的訪問(wèn)次數(shù)。這樣就能很大的提高了訪問(wèn)速度,代價(jià)是要占用一定的內(nèi)存空間,但相比之下應(yīng)該算小巫吧。
  
  下面把簡(jiǎn)單實(shí)現(xiàn)的幾個(gè)Class說(shuō)一說(shuō)。
  
  一 首先實(shí)現(xiàn)一個(gè)Cache類,里面有讀取對(duì)象的方法get(),假如文件沒(méi)有被修改則直接從HashMap里面將對(duì)象取出,假如文件被修改則調(diào)用readObject()方法實(shí)現(xiàn)從文件中讀出數(shù)據(jù),并同時(shí)將讀出的數(shù)據(jù)放入HashMap里,將原來(lái)的對(duì)象覆蓋。這樣下次再讀數(shù)據(jù)的時(shí)候就可以從緩存中直接讀到,并且保證是最新的數(shù)據(jù)。還有一個(gè)判定文件是否被修改的方法getModified();
  

  代碼實(shí)現(xiàn)如下:
  
  import java.io.File;
  import java.util.HashMap;
  
  public class Cache {
  
  HashMap mapLastModified = new HashMap();
  HashMap mapValues = new HashMap();
  public Cache() {
  super();
  }
  public Object get(String name, String path, Class clsParser, Class clsInstantiator, Class clsObj) {
  Object obj = null;
  String absPath = getClass().getResource(path).getPath();
  Long modified = getModified(name, absPath);
  if (modified != null) {
  obj = readObject(absPath, clsParser, clsInstantiator, clsObj);
  
  mapLastModified.put(name, modified);
  mapValues.put(name, obj);
  System.out.  } else {
  obj = mapValues.get(name);
  System.out.println("get object from cache");
  }
  return obj;
  }
  
  private Long getModified(String name, String path) {
  Long modified = new Long(new File(path).lastModified());
  Long saveModified = (Long) mapLastModified.get(name);
  if ((saveModified != null) && (saveModified.longValue() >= modified.longValue())) {
  modified = null;
  }
  return modified;
  }
  
  private Object readObject(String path, Class clsParser, Class clsInstantiator, Class clsObj) {
  try {
  FileParser parser = (FileParser) clsParser.newInstance();
  Instantiator instantiator = (Instantiator) clsInstantiator.newInstance();
  
  Object config = parser.parse(path);
  return instantiator.instantiate(clsObj, config);
  
  } catch (InstantiationException e) {
  e.printStackTrace();
  } catch (Illegal
accessException e) {
  e.printStackTrace();
  }
  return null;
  }
  }
  
  二 解析XML文件的類XmlFileParser,
  

  為了方便處理不同文件的解析,在這里先定義一個(gè)接口FileParser,XmlFileParser實(shí)現(xiàn)了它,假如還有諸如對(duì)其他種類文件的解析也可以實(shí)現(xiàn)它。
  
  //FileParser.java
  public interface FileParser {
  Object parse(String path);
  
  }
  
  //XmlFileParser.java
  //采用Jdom的解析方式
  
  import java.io.FileInputStream;
  import java.io.IOException;
  import org.jdom.Document;
  import org.jdom.Element;
  import org.jdom.input.SAXBuilder;
  
  public class XmlFileParser implements FileParser {
  
  public XmlFileParser() {
  super();
  }
  
  public Object parse(String path) {
  
  FileInputStream fi = null;
  try {
  fi = new FileInputStream(path);
  SAXBuilder sb = new SAXBuilder();
  Document doc = sb.build(fi);
  Element root = doc.getRootElement();
  return root.getChildren();
  } catch (Exception e) {
  e.printStackTrace();
  } finally {
  try {
  fi.close();
  } catch (IOException e1) {
  }
  }
  }
  }
  
  三 接下來(lái)是一個(gè)實(shí)例化處理的類ListTypeInstantiator,同樣為了方便處理不同文件的實(shí)例化,在這里先定義一個(gè)接口Instantiator,ListTypeInstantiator實(shí)現(xiàn)了它。
  

  //Instantiator.java
  public interface Instantiator {
  Object instantiate(Class clazz, Object configuration);
  }
  
  //ListTypeInstantiator.java
  import java.util.ArrayList;
  import java.util.List;
  
  import org.apache.commons.beanutils.BeanUtils;
  import org.jdom.Element;
  
  public class ListTypeInstantiator implements Instantiator {
  
  public ListTypeInstantiator() {
  super();
  }
  
  public Object instantiate(Class clazz, Object configuration) {
  List arr = new ArrayList();
  Object bean = null;
  
  List children = (List) configuration;
  Element child = null;
  
  List attributes = null;
  Element attribute = null;
  
  try {
  for(int i=0; i<children.size(); i++) {
  child = (Element) children.get(i);
  bean = clazz.newInstance();
  attributes = child.getChildren();
  for(int j=0; j<attributes.size(); j++) {
  attribute = (Element) attributes.get(j);
  BeanUtils.setProperty(bean, attribute.getName(), attribute.getText());
  }
  arr.add(bean);
  }
  } catch(Exception e) {
  e.printStackTrace();
  }
  return arr;
  }
  }
  
  四 另外還需要一個(gè)封裝我想要數(shù)據(jù)形式的JavaBean,這里設(shè)為NewsBean{}.
  

  //NewsBean.java
  public class NewsBean {
  
  private Long id;
  private String newsTitle;
  private String newsContent;
  private String newsType;
  private String deployDate;
  private String cancelDate;
  
  public Long getId() {
  return id;
  }
  public void setId(Long id) {
  this.id = id;
  }
  public String getNewsTitle() {
  return newsTitle;
  }
  public void setNewsTitle(String newsTitle) {
  this.newsTitle = newsTitle;
  }
  public String getNewsContent() {
  return newsContent;
  }
  public void setNewsContent(String newsContent) {
  this.newsContent = newsContent;
  }
  public String getNewsType() {
  return newsType;
  }
  public void setNewsType(String newsType) {
  this.newsType = newsType;
  }
  public String getDeployDate() {
  return deployDate;
  }
  public void setDeployDate(String deployDate) {
  this.deployDate = deployDate;
  }
  public String getCancelDate() {
  return cancelDate;
  }
  public void setCancelDate(String cancelDate) {
  this.cancelDate = cancelDate;
  }
  }
  
  五 最后一步測(cè)試結(jié)果,將news.xml文件放到classes目錄下。
  

  //MainClass.java
  
  import java.util.List;
  public class MainClass{
  
  public static void main(String[] args) throws Exception {
  
  List news1 = null;
  List news2 = null;
  NewsBean bean = null;
  
  news1 = (List)Cache.get(
  "news", "/news.xml",
  XmlFileParser.class, ListTypeInstantiator.class, NewsBean.class);
  for (int i = 0; i < news1.size(); i++) {
  bean = (NewsBean) news1.get(i);
  System.out.println(bean.getId());
  System.out.println(bean.getNewsTitle());
  System.out.println(bean.getNewsContent());
  System.out.println(bean.getNewsType());
  System.out.println(bean.getDeployDate());
  System.out.println(bean.getCancelDate());
  }
  news2 = (List)Cache.get(
  "news", "/news.xml",
  XmlFileParser.class, ListTypeInstantiator.class, NewsBean.class);
  for (int i = 0; i < news2.size(); i++) {
  bean = (NewsBean) news2.get(i);
  System.out.println(bean.getId());
  System.out.println(bean.getNewsTitle());
  System.out.println(bean.getNewsContent());
  System.out.println(bean.getNewsType());
  System.out.println(bean.getDeployDate());
  System.out.println(bean.getCancelDate());
  }
  }
  
  第一次會(huì)從文件中讀出數(shù)據(jù),第二次就會(huì)從緩存中讀取了,試著多讀幾次速度明顯快很多。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 佛学| 吉隆县| 石渠县| 华宁县| 临猗县| 斗六市| 湟源县| 渝中区| 嘉鱼县| 祥云县| 大兴区| 广平县| 怀远县| 咸丰县| 交城县| 泽库县| 山丹县| 香格里拉县| 平山县| 阿拉善左旗| 平乡县| 深圳市| 左权县| 天峨县| 日土县| 灵寿县| 景东| 辉县市| 呈贡县| 香河县| 农安县| 柳州市| 稻城县| 太湖县| 东宁县| 绥德县| 芜湖市| 广德县| 全州县| 平山县| 关岭|