Using the cache mechanism to quickly read data in XML files

Source: Internet
Author: User
After receiving a task, let me create a background management system for my company's website. The requirement is very simple: a news publishing module and a recruitment information publishing module. However, it cannot be implemented using databases, but only in the form of file access.

You don't have to worry about using XML files for data access. Similar functions have been implemented during the previous design, so there is no problem with reading XML. The key is that it is worthwhile to think about performance issues. I am a newbie and have never done any design before. Therefore, some people may be somewhat immature in their eyes, it doesn't matter. Let others talk about it in their own way :)

If files are frequently parsed, the speed will certainly be affected. In the case of a very large file, it is even intolerable. If the data in the file is first encapsulated into an object array and read into the cache when the server is started, the system first checks whether the object to be accessed has been updated during each access, if the file is not updated, you can directly read the desired data from the cache. Of course, if the file is updated, You have to honestly read the desired data from the parsing file. After all, the number of file modifications made by the Administrator is a minority, and more is the number of visits made by the visitor. In this way, the access speed can be greatly improved, and the cost is to occupy a certain amount of memory space, but it should be considered a waste.

The following describes some simple implementation classes.

First, implement a cache class with the get () method for reading objects. if the object is not modified, retrieve the object directly from hashmap. if the object is modified, call readobject () to read data from the file, and put the read data into the hashmap to overwrite the original object. In this way, you can directly read the data from the cache next time, and ensure that the data is the latest. Another method is getmodified () to determine whether the object is modified ();
The code is implemented 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) 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 file parsing class xmlfileparser,
To facilitate parsing of different files, define an interface fileparser first, and xmlfileparser implements it. If there are other types of files that can be parsed, it can also be implemented.
// Fileparser. Java
Public interface fileparser {
Object parse (string path );

}

// Xmlfileparser. Java
// Parse using 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 ){
}
}
}
}

The third step is a class of listtypeinstantiator for instantiation. To facilitate the instantiation of different files, we define an interface instantiator, which is implemented by 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;
}
}

4. You also need a JavaBean that encapsulates the desired data format. Here it 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;
}
}

5. 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 time it is read from the cache, trying to read several times faster.
 

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.