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