[opencms-dev] During a session request, the same data should not be read multiple times from the database.

脸谱 afeinet at 163.com
Wed Jun 6 10:40:09 CEST 2018


I'm sorry, these English words are translated by Google. I hope not to be too weird and everyone can read them.



During a session request, the same data should not be read multiple times from the database.

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.

Testing process:
1. Start Opencms.
2, open the page to test. (At this point, many data will be requested from the database and cached using FlexCache)
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.)
Test Results:
1) getConnection method is called 306 times (mainly from readResourceByPath, readResourceByStructureId, readContent three methods)
2) The page request processing takes 632 ms.

After detailed monitoring of these three methods, it was found that the same PATH, StructureId has a large number of repeated reading.


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.
    public CmsResource readResource(CmsDbContext dbc, CmsUUID projectId, CmsUUID structureId, boolean includeDeleted)
    throws CmsDataAccessException {
         //Try to get cached data.
        final String cacheKey = String.valueOf(structureId);
        CmsResource resource = (CmsResource)dbc.getRequestContext().getAttribute(cacheKey);
        if(null != resource) {
            return resource;
        }

        //.........

        // Caching so that it can be reused during a page request
        if(resource != null) {
            //cache by structureId
            dbc.getRequestContext().setAttribute(cacheKey, resource);
            //cache by path
            dbc.getRequestContext().setAttribute(resource.getRootPath(), resource);
        }
        return resource;
    }

    /**
     * @see org.opencms.db.I_CmsVfsDriver#readResource(org.opencms.db.CmsDbContext, CmsUUID, java.lang.String, boolean)
     */
    public CmsResource readResource(CmsDbContext dbc, CmsUUID projectId, String path, boolean includeDeleted)
    throws CmsDataAccessException {
         //Try to get cached data.
        CmsResource resource = (CmsResource)dbc.getRequestContext().getAttribute(path);
        if(null != resource) {
            return resource;
        }

        //.............

        // Caching so that it can be reused during a page request
        if(resource != null) {
            //cache by PATH
            dbc.getRequestContext().setAttribute(path, resource);
            //cache by structureId
            dbc.getRequestContext().setAttribute(String.valueOf(resource.getStructureId()), resource);
        }
        return resource;
    }


If this code generates BUG, please tell me, thank you.


Test process: (The above process is repeated after the Java code is modified and compiled into a jar package)
Test Results:
1) The getConnection method was called 145 times.
2) The page request processing time is 247ms.

in conclusion:
This process simply shows that after a few code adjustments, the performance improvements have been made in the same operating environment.
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.

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.


I hope this tip will help you a little bit.


--



缘起法实不可思议也。

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20180606/0e36981f/attachment.htm>


More information about the opencms-dev mailing list