• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

利用缓存机制快速读取XML文件中的数据_xml

xml 搞代码 7年前 (2018-06-15) 208次浏览 已收录 0个评论

接到一个任务,让我做一个公司网站的后台管理系统。要求很简单,就一个新闻发布模块和一个招聘信息发布模块。但不能用DB,只能用文件存取的形式实现。

       不用考虑肯定是用xml文件进行数据的存取了,以前做毕设的时候也曾经实现过类似的功能,所以关于XML的读取并没有问题。关键是如果考虑到性能的问题就值得推敲一下了,我是新人,以前也没做过什么设计,所以做出的东西在一些人眼中可能会有些稚嫩,那也没关系,走自己的路让别人去说吧:)

       如果频繁解析文件,速度肯定受到影响,在文件非常大的情况下,甚至是无法忍受的。如果在服务器启动的时候先把文件中的数据封装成对象数组读入到缓存中,每次访问的时候先判断一下要访问的文件实体有没有被更新,如果没有被更新,就直接从缓存中将想要的数据读出来,当然如果文件被更新了,那只好老老实实的解析文件读出想要的数据。管理者对文件的修改次数毕竟是少数,更多的是访问者的访问次数。这样就能很大的提高了访问速度,代价是要占用一定的内存空间,但相比之下应该算小巫吧。

http://www.gaodaima.com/33800.html利用缓存机制快速读取XML文件中的数据_xml

        下面把简单实现的几个Class说一说。

        一 首先实现一个Cache类,里面有读取对象的方法get(),如果文件没有被修改则直接从HashMap里面将对象取出,如果文件被修改则调用readObject()方法实现从文件中读出数据,并同时将读出的数据放入HashMap里,将原来的对象覆盖。这样下次再读数据的时候就可以从缓存中直接读到,并且保证是最新的数据。还有一个判断文件是否被修改的方法getModified();
代码实现如下:

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.println(“get object from file”);
        } 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 (IllegalaccessException e) {
            e.printStackTrace();
        }       
        return null;
    }
}

二 解析XML文件的类XmlFileParser,
为了方便处理不同文件的解析,在这里先定义一个接口FileParser,XmlFileParser实现了它,如果还有诸如对其他种类文件的解析也可以实现它。
//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) {
   }
  }  
 }
}

三 接下来是一个实例化处理的类ListTypeInstantiator,同样为了方便处理不同文件的实例化,在这里先定义一个接口Instantiator,ListTypeInstantiator实现了它。
//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;
 }
}

四 另外还需要一个封装我想要数据形式的JavaBean,这里设为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;
 } 
}

五 最后一步测试结果,将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());
  }
}
第一次会从文件中读出数据,第二次就会从缓存中读取了,试着多读几次速度明显快很多。
 

欢迎大家阅读《利用缓存机制快速读取XML文件中的数据_xml》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:利用缓存机制快速读取XML文件中的数据_xml

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址