java 优化 笔记

来源:互联网 发布:全景效果图软件 编辑:程序博客网 时间:2024/06/09 14:35

由于项目的数据是定时更新,为了页面的加载速度变快,使用了各种的优化:

1.首先是数据库的优化,由于数据是属于每分钟一条的,经过和项目经理的协商,可以计算为5分钟一天(取平均数),然后把一天分为288列(每天产生一天数据:ps 相当于java的去除冗余,而不是把全部的都简化到一条数据了,要看真正的需求);

2.后来由于上面的人数页面加载还是慢,而且由于每次访问数据库操作量大,有需要进一步的优化,利用ehcache缓存,把数据存到内存中,然后定时更新,具体就不写ehcache怎么使用,毕竟不是为了写这个

ps:2.1.中间还有一些优化就简单的说一下不仔细说了。尽可能优化sql语句,能一次查完的数据,不要分多次查询,能使用更有效率的sql语句就不用其他的等。合理利用多线程。

3.再后来,由于数据量的增加,服务器内存有限(小公司 ~ ~!),经过商量决定利用静态页面,然后由我来做,关于页面静态化请点击。由于第一次做页面静态化所以没有考虑更多就开始去做,后来发现页面内有页面加载的时候就会去做一个post请求,不知道怎么实现怎么加载到页面中。最后放弃页面静态化,至于为什么不把post请求改为直接传到页面,是因为改动太大而且有更好的选择。

4.更好的选择就是把数据写到xml中,用的时候从xml中读取。定时去更新xml中的数据。下面把 自己写的一个简单的创建和解析xml的代码贴一下,没怎么用过,写的不好的地方还请指出来,互相学习。

package com.intcache.hepburn.util;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.transform.OutputKeys;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerConfigurationException;import javax.xml.transform.TransformerException;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;import com.intcache.base.var.Constants;public class ParseXml {private static Document document;private static String ONE_ELEMENT_NAME = "oneData";private static String TWO_ELEMENT_NAME = "twoData";private static String THREE_ELEMENT_NAME = "threeData";private static String FOUR_ELEMENT_NAME = "fourData";private static String FIVE_ELEMENT_NAME = "fiveData";private static String SIX_ELEMENT_NAME = "sixData";private static String SEVEN_ELEMENT_NAME = "sevenMap";public static void init() {try {DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();document = builder.newDocument();} catch (ParserConfigurationException e) {System.out.println(e.getMessage());}}public static String getNodeName(String nodeName) {return Constants.XML_NODE_PREFIX + nodeName;}/** * 生成一个xml 解析的时候 参数map 和此方法一致 * @param fileName 文件名 * @param oneMap 第一个map Map<String,List<Long>> * @param twoMap 第二个map Map<String,List<Long>> * @param threeMap 第三个map Map<String, Long> * @param fourMap 第四个map Map<String, Long> * @param fiveMap 第五个map Map<String, List<Double>> * @param sixMap 第六个map Map<String, List<Double>> * @param sevenMap 第七个map Map<String, Map<String, List<Float>>> */public static void createXml(String fileName, Map<String, List<Long>> oneMap, Map<String, List<Long>> twoMap, Map<String, Long> threeMap, Map<String, Long> fourMap,Map<String, List<Double>> fiveMap, Map<String, List<Double>> sixMap, Map<String, Map<String, List<Float>>> sevenMap) {init();Element root = document.createElement(getNodeName("data")); //创建根节点  document.appendChild(root);if (oneMap != null) {Element oneItem = document.createElement(ONE_ELEMENT_NAME);root.appendChild(oneItem);for (String str : oneMap.keySet()) {addElement(oneMap.get(str).toString(), oneItem, str);}}if (twoMap != null) {Element twoItem = document.createElement(TWO_ELEMENT_NAME);root.appendChild(twoItem);for (String str : twoMap.keySet()) {addElement(twoMap.get(str).toString(), twoItem, str);}}if (threeMap != null) {Element threeItem = document.createElement(THREE_ELEMENT_NAME);root.appendChild(threeItem);for (String str : threeMap.keySet()) {addElement(threeMap.get(str).toString(), threeItem, str);}}if (fourMap != null) {Element fourItem = document.createElement(FOUR_ELEMENT_NAME);root.appendChild(fourItem);for (String str : fourMap.keySet()) {addElement(fourMap.get(str).toString(), fourItem, str);}}if (fiveMap != null) {Element fiveItem = document.createElement(FIVE_ELEMENT_NAME);root.appendChild(fiveItem);for (String str : fiveMap.keySet()) {addElement(fiveMap.get(str).toString(), fiveItem, str);}}if (sixMap != null) {Element sixItem = document.createElement(SIX_ELEMENT_NAME);root.appendChild(sixItem);for (String str : sixMap.keySet()) {addElement(sixMap.get(str).toString(), sixItem, str);}}if (sevenMap != null) {Element sevenItem = document.createElement(SEVEN_ELEMENT_NAME);root.appendChild(sevenItem);for (String str : sevenMap.keySet()) {Element strItem = document.createElement(str);sevenItem.appendChild(strItem);for (String str2 : sevenMap.get(str).keySet()) {addElement(sevenMap.get(str).get(str2).toString(), strItem, str2);}}}TransformerFactory tf = TransformerFactory.newInstance();try {Transformer transformer = tf.newTransformer();DOMSource source = new DOMSource(document);transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");transformer.setOutputProperty(OutputKeys.INDENT, "yes");PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));StreamResult result = new StreamResult(pw);transformer.transform(source, result); //关键转换  System.out.println("生成XML文件成功!");} catch (TransformerConfigurationException e) {System.out.println(e.getMessage());} catch (IllegalArgumentException e) {System.out.println(e.getMessage());} catch (FileNotFoundException e) {System.out.println(e.getMessage());} catch (TransformerException e) {System.out.println(e.getMessage());}}private static void addElement(String textContent, Element parentItem, String str) {Element item = document.createElement(getNodeName(str));item.setTextContent(textContent);parentItem.appendChild(item);}public static void createXml(String fileName) {init();Element root = document.createElement("data"); //创建根节点  document.appendChild(root);Element data = document.createElement("wk");data.setNodeValue("NodeValue");data.setTextContent("TextContent");root.appendChild(data);TransformerFactory tf = TransformerFactory.newInstance();try {Transformer transformer = tf.newTransformer();DOMSource source = new DOMSource(document);transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");transformer.setOutputProperty(OutputKeys.INDENT, "yes");PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));StreamResult result = new StreamResult(pw);transformer.transform(source, result); //关键转换  System.out.println("生成XML文件成功!");} catch (TransformerConfigurationException e) {System.out.println(e.getMessage());} catch (IllegalArgumentException e) {System.out.println(e.getMessage());} catch (FileNotFoundException e) {System.out.println(e.getMessage());} catch (TransformerException e) {System.out.println(e.getMessage());}}/** * 解析xml文件  解析的时候 参数map 和生成xml(createXml())一致 * @param fileName 文件名 * @param oneMap 第一个map Map<String,List<Long>> * @param twoMap 第二个map Map<String,List<Long>> * @param threeMap 第三个map Map<String, Long> * @param fourMap 第四个map Map<String, Long> * @param fiveMap 第五个map Map<String, List<Double>> * @param sixMap 第六个map Map<String, List<Double>> * @param sevenMap 第七个map Map<String, Map<String, List<Float>>> */public static void parserXml(String fileName, Map<String, List<Long>> oneMap, Map<String, List<Long>> twoMap, Map<String, Long> threeMap, Map<String, Long> fourMap,Map<String, List<Double>> fiveMap, Map<String, List<Double>> sixMap, Map<String, Map<String, List<Float>>> sevenMap) {try {DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();DocumentBuilder db = dbf.newDocumentBuilder();Document document = db.parse(fileName);NodeList employees = document.getChildNodes();String[] strArr;List<Long> dataList;for (int i = 0; i < employees.getLength(); i++) {Node employee = employees.item(i);NodeList employeeInfo = employee.getChildNodes();for (int j = 0; j < employeeInfo.getLength(); j++) {Node node = employeeInfo.item(j);NodeList employeeMeta = node.getChildNodes();for (int k = 0; k < employeeMeta.getLength(); k++) {if (employeeInfo.item(j).getNodeName().equals(ONE_ELEMENT_NAME)) {strArr = employeeMeta.item(k).getTextContent().replace("[", "").replace("]", "").split(",");dataList = new ArrayList<Long>();if (employeeMeta.item(k).getNodeName().contains("#text")) {continue;}for (String string : strArr) {if (string.trim().equals("")) {dataList.add(Long.valueOf(0));} else {dataList.add(Long.valueOf(string.trim()));}}oneMap.put(employeeMeta.item(k).getNodeName().replace(Constants.XML_NODE_PREFIX, ""), dataList);} else if (employeeInfo.item(j).getNodeName().equals(TWO_ELEMENT_NAME)) {strArr = employeeMeta.item(k).getTextContent().replace("[", "").replace("]", "").split(",");dataList = new ArrayList<Long>();if (employeeMeta.item(k).getNodeName().contains("#text")) {continue;}for (String string : strArr) {if (string.trim().equals("")) {dataList.add(Long.valueOf(0));} else {dataList.add(Long.valueOf(string.trim()));}}twoMap.put(employeeMeta.item(k).getNodeName().replace(Constants.XML_NODE_PREFIX, ""), dataList);} else if (employeeInfo.item(j).getNodeName().equals(THREE_ELEMENT_NAME)) {threeMap.put(employeeMeta.item(k).getNodeName().replace(Constants.XML_NODE_PREFIX, ""),Long.valueOf((employeeMeta.item(k).getTextContent().trim().equals("") ? String.valueOf(0) : employeeMeta.item(k).getTextContent().toString())));} else if (employeeInfo.item(j).getNodeName().equals(FOUR_ELEMENT_NAME)) {fourMap.put(employeeMeta.item(k).getNodeName().replace(Constants.XML_NODE_PREFIX, ""),Long.valueOf((String) (employeeMeta.item(k).getTextContent().trim().equals("") ? String.valueOf(0) : employeeMeta.item(k).getTextContent().toString())));} else if (employeeInfo.item(j).getNodeName().equals(FIVE_ELEMENT_NAME)) {strArr = employeeMeta.item(k).getTextContent().replace("[", "").replace("]", "").split(",");List<Double> doubleList = new ArrayList<Double>();if (employeeMeta.item(k).getNodeName().contains("#text")) {continue;}for (String string : strArr) {if (string.trim().equals("")) {doubleList.add(Double.valueOf(0));} else {doubleList.add(Double.valueOf(string.trim()));}}fiveMap.put(employeeMeta.item(k).getNodeName().replace(Constants.XML_NODE_PREFIX, ""), doubleList);} else if (employeeInfo.item(j).getNodeName().equals(SIX_ELEMENT_NAME)) {strArr = employeeMeta.item(k).getTextContent().replace("[", "").replace("]", "").split(",");List<Double> doubleList = new ArrayList<Double>();if (employeeMeta.item(k).getNodeName().contains("#text")) {continue;}for (String string : strArr) {if (string.trim().equals("")) {doubleList.add(Double.valueOf(0));} else {doubleList.add(Double.valueOf(string.trim()));}}sixMap.put(employeeMeta.item(k).getNodeName().replace(Constants.XML_NODE_PREFIX, ""), doubleList);} else if (employeeInfo.item(j).getNodeName().equals(SEVEN_ELEMENT_NAME)) {Map<String, List<Float>> map = new HashMap<String, List<Float>>();Node floatData = employeeMeta.item(k);NodeList floatDatas = floatData.getChildNodes();for (int l = 0; l < floatDatas.getLength(); l++) {strArr = floatDatas.item(l).getTextContent().replace("[", "").replace("]", "").split(",");List<Float> floatList = new ArrayList<Float>();if (floatDatas.item(l).getNodeName().contains("#text")) {continue;}for (String string : strArr) {if (string.trim().equals("")) {floatList.add(Float.valueOf(0));} else {floatList.add(Float.valueOf(string.trim()));}}map.put(floatDatas.item(l).getNodeName().replace(Constants.XML_NODE_PREFIX, ""), floatList);}sevenMap.put(employeeMeta.item(k).getNodeName(), map);}}}}System.out.println("解析完毕");} catch (FileNotFoundException e) {System.out.println(e.getMessage());} catch (ParserConfigurationException e) {System.out.println(e.getMessage());} catch (SAXException e) {System.out.println(e.getMessage());} catch (IOException e) {System.out.println(e.getMessage());}}public static void main(String[] args) {createXml("D:\\wk.txt");}}

代码是死的,人是活的,根据需求自己改。

好久没有写过原创的了,好激动撒大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑

还有什么方法可以优化的还请指教,(当然有很多啦,本人刚入行 勿喷撒)

0 0