<div style="line-height:1.7;color:#000000;font-size:14px;font-family:Arial"><div>I'm sorry, these English words are translated by Google. I hope not to be too weird and everyone can read them.<br></div><div><br></div><div>During a session request, the same data should not be read multiple times from the database.</div><br>I used one of my website's pages to test it. I think all database connections need to use the getConnection method, so I added some logging in this method.<br><br>Testing process:<br>1. Start Opencms.<br>2, open the page to test. (At this point, many data will be requested from the database and cached using FlexCache)<br>3, refresh this page. (At this point, the cached data has already been used. If there is a part that reads data from the database, this must be unavoidable every time the user requests it, so test this part.)<br>Test Results:<br>1) getConnection method is called 306 times (mainly from readResourceByPath, readResourceByStructureId, readContent three methods)<br>2) The page request processing takes 632 ms.<br><br>After detailed monitoring of these three methods, it was found that the same PATH, StructureId has a large number of repeated reading.<br><br><div>Therefore, the following improvements have been made, mainly in a user's one session request, to cache the data that has been read from the database, to avoid repeated reading of the database.</div><div><span style="background-color: rgb(192, 192, 192);"> public CmsResource readResource(CmsDbContext dbc, CmsUUID projectId, CmsUUID structureId, boolean includeDeleted)<br> throws CmsDataAccessException {<br> //Try to get cached data.<br> final String cacheKey = String.valueOf(structureId);<br> CmsResource resource = (CmsResource)dbc.getRequestContext().getAttribute(cacheKey);<br> if(null != resource) {<br> return resource;<br> }<br><br> //.........<br><br> // Caching so that it can be reused during a page request<br> if(resource != null) {<br> //cache by structureId<br> dbc.getRequestContext().setAttribute(cacheKey, resource);<br> //cache by path<br> dbc.getRequestContext().setAttribute(resource.getRootPath(), resource);<br> }<br> return resource;<br> }<br><br> /**<br> * @see org.opencms.db.I_CmsVfsDriver#readResource(org.opencms.db.CmsDbContext, CmsUUID, java.lang.String, boolean)<br> */<br> public CmsResource readResource(CmsDbContext dbc, CmsUUID projectId, String path, boolean includeDeleted)<br> throws CmsDataAccessException {</span><br><span style="background-color: rgb(192, 192, 192);"><span style="background-color: rgb(192, 192, 192);"> //Try to get cached data.</span><br> CmsResource resource = (CmsResource)dbc.getRequestContext().getAttribute(path);<br> if(null != resource) {<br> return resource;<br> }<br><br> //.............<br><br> // Caching so that it can be reused during a page request<br> if(resource != null) {<br> //cache by PATH<br> dbc.getRequestContext().setAttribute(path, resource);<br> //cache by structureId<br> dbc.getRequestContext().setAttribute(String.valueOf(resource.getStructureId()), resource);<br> }<br> return resource;<br> }</span></div><div><span style="background-color: rgb(192, 192, 192);"><br></span></div><div><span style="background-color: rgb(192, 192, 192);"></span>If this code generates BUG, please tell me, thank you.</div><div><br></div>Test process: (The above process is repeated after the Java code is modified and compiled into a jar package)<br>Test Results:<br>1) The getConnection method was called 145 times.<br>2) The page request processing time is 247ms.<br><br>in conclusion:<br>This process simply shows that after a few code adjustments, the performance improvements have been made in the same operating environment.<br>The main promotion is to reduce the page response time; reduce the number of database connections; for a large number of user access, this increase appears more valuable.<br><br>This is just an optimization for a page request. In the request of different pages, there will also be a lot of data is repeated, coupled with different user access, these data should require a cache mechanism before the database, rather than all data directly read directly from the database at any time.<br><br><br>I hope this tip will help you a little bit.<br><br><div style="position:relative;zoom:1">--<br><div><br></div>
<div>缘起法实不可思议也。</div>
<div style="clear:both"></div></div><div id="divNeteaseMailCard"></div><br></div><br><br><span title="neteasefooter"><p> </p></span>