Use JCS to implement object cache in web portal applications
In my nearest Web portal application development, we need to store some find data in the Servlet container (Tomcat) memory (for example, ratio update data, status, product list) so that we don't need to access data every time. Perform a database lookup. At the same time, we also need to refresh the data stored in memory to ensure its fresh and accurate. We also need a mechanism to refresh different types of data stored in memory at different time intervals. For example, the ratio update data requires a daily refresh, and the data of the lookup type can be kept in memory for a long time. Object Cache is the most convenient perfect solution to all of the above purposes.
Object cache
The most appropriate object cache defines can be found in the functional specification document of Object Caching Service for Java (OCS4J), which is defined:
The server must divide information and executables into three basic category management: I will never change objects, each request will change objects, and objects between the two. Java provides good support for the processing of the first two objects, but supports the third category of support very limited. If the object will never change, we create a static object when we initialize the server. If each request object is different, we create new objects each time. For objects or information between the two, their status may change, but it is necessary to provide a sharing of requests, across the user, or across the process, and use "Object Cache Service".
Basically, the object cache is to be reused by reused in memory after using the object, but is stored in memory and is reused by the later client, avoiding the expensive cost of the object. When the data is retrieved from the data source for the first time, it is temporarily stored in a memory buffer named cache. When the same data is accessed again, the object is taken out from the cache, rather than re-acquired from the data source. Finally, the cached data is released from memory when it is no longer needed. From the viewpoint of web applications, when specified objects can be released from memory depends on defining an object to become invalid, a reasonable interval.
Cache benefits
The main benefit of object caching is the major improvement of application performance. In a multi-layer application, data access is a very high cost comparison with other work. By Keeping the Frequently Accessed Data Around, we can avoid re-acquire the overhead and time of resending objects. This will increase the performance of the web application because we don't have to access the database every time you get the data.
Scalability is another benefit that uses an object cache. Because the cached data can across multiple session and web applications, object cache can be an important part of the scalability web application design. With object caching, just like the object pool, you can help avoid overhead of getting the object.
Unfavorable factor in cache
The use of object caching in web applications can bring many benefits, but caching objects will also bring some unfavorable factors.
Synchronization complexity: The growth of complexity depends on the type of data, as it is necessary to ensure the consistency of the original data in the cached data to the data source. Otherwise, the cached data is inconsistent with the actual data, which will result in data errors in the application.
Durability: When the server crashes, the modified back of the cached data is lost. You can use synchronous cache to avoid this problem.
Memory size: If there is a lot of data that is not used in the cache, and these data is not released in the normal time interval, the JVM memory size will become unacceptable.
Cache instance
Either
what
If you don't change, it takes a long time to get the data obtained from the data source is the best choice for cache. This includes almost all static data, code, and description lists, as well as findings with paging features (search results can be obtained from the data source and store them in the cache, waiting for the user to click on the page number on the result page. ). Middleware technologies such as EJB and CORBA allow remote objects between clients and servers. Access to this type is also known as coarse granular data access, which is used to reduce expensive remote methods call overhead. If these data transfer objects (also known as values objects) are not very frequent, they can be stored in the cache. This reduces the number of times the servlet container accesses the application server when each client needs value objects.
So what type of data should not be cached? Here is some data categories that are not recommended to use cache:
Can be accessed by other users on the website.
User description information.
Private information, including social insurance numbers or credit card detailed information.
Regular changes, and inaccurate or not timely business information.
Session-specific data that should not be accessed by other users.