Fast reading of data in XML files using caching mechanism

Source: Internet
Author: User
Tags object config implement interface return string access
xml| Cache | Data received a task, let me do a company website background management system. The requirement is simple, a press release module and a recruitment information release module. But can not be used in db, can only be implemented in the form of file access.

Do not have to consider the data must be used to access the XML file, before the completion of the time has been implemented similar functions, so the reading of XML is not a problem. The key is that if you take into account the performance of the problem is worth the scrutiny, I was new, did not do before what design, so make things in some people's eyes may be a little bit immature, that also does not matter, go their own way to let others say:

If the file is frequently parsed, the speed will certainly be affected, in the case of very large files, even unbearable. If the data in the file is encapsulated into an array of objects to be read into the cache when the server is started, each visit to determine whether the file entity to be accessed has been updated, if not updated, directly from the cache will want to read the data, of course, if the file is updated, That had to be honest to parse the file to read out the desired data. The number of times a manager modifies a file is, after all, a small number of visits. This can greatly improve the speed of access, the price is to occupy a certain amount of memory space, but in comparison should be a small witch bar.

Here's a simple implementation of a few classes to say.

First implementation of a cache class, which has read the object's method get (), if the file has not been modified directly from the HashMap inside the object out, if the file is modified to call the ReadObject () method to read the data from the file, And at the same time will read out of the data into the HashMap, the original object coverage. The next time you read the data, you can read it directly from the cache and make sure it's the most recent data. There is also a way to determine whether the file has been modified getmodified ();
The code implementation is as follows:

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;
}
}

The class xmlfileparser of parsing xml file,
In order to facilitate the processing of different file parsing, here first define an interface Fileparser,xmlfileparser implemented it, if there are other kinds of file parsing can also implement it.
Fileparser.java
Public interface Fileparser {
Object Parse (String path);

}

Xmlfileparser.java
Using Jdom method of parsing

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) {
}
}
}
}

Three Next is an instantiated class Listtypeinstantiator, also in order to facilitate the processing of different file instantiation, here first defines an interface instantiator,listtypeinstantiator implement it.
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;
}
}

IV also need a package I want the data form of the JavaBean, which is set to 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;
}
}

Five final step test results, put the News.xml file in the classes directory.
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 ());
}
}
The first time the data is read from the file, the second is read from the cache, try to read more than a few times significantly faster.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.