加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

将 Xml 文章通过对象的形式保存到 缓存中

发布时间:2020-12-16 06:37:39 所属栏目:百科 来源:网络整理
导读:源文档地址:http://www.360doc.com/content/05/1129/12/2343_37352.shtml 接到一个任务,让我做一个公司网站的后台管理系统。要求很简单,就一个新闻发布模块和一个招聘信息发布模块。但不能用DB,只能用文件存取的形式实现。 不用考虑肯定是用XML文件进行

源文档地址:http://www.360doc.com/content/05/1129/12/2343_37352.shtml


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

不用考虑肯定是用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 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",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()); } } 第一次会从文件中读出数据,第二次就会从缓存中读取了,试着多读几次速度明显快很多。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读