Use JCS to implement object cache in web portal applications (3)

zhaozj2021-02-16  40

The main principle of the framework

Cache properties

We configure all the cache parameters in the properties file called cache.ccf. These parameters include cache information such as the maximum number of objects stored in memory, cache time (after passing time, data nine automatically released from memory), interrupt time Since last accept time, memory cache name (for example : Cache algorithm such as LRU or MRU), etc. In the current version of JCS, the cache properties file is plain text format. Spiritcache Framework, a JCache API business implementation from Spiritsoft, supports a cache configuration in XML format.

Confirm that this property file is stored in the class path. Note: If you need to use other files to store buffer properties, JCS also provides a method to specify a name of a configuration file. Please refer to JFADOCS learning

what

Read the cache configuration information from other non-default properties files.

The following is some Java classes that the web application needs to know using the cache function. These classes are stored in the common.caching package of the sample code of this article. These classes of Javadocs are also included in the source code compression package. (The class diagram in Figure 2 shows the relationship between these Java classes)

Icachemanager

This is a primary interface (contract) that the customer app implements all caches (such as: storage, access, and release cache). The client can be a JSP, a Struts Action class, or a Pojo object. Create this interface to hide all the cache implementation details for the client, so we do not need to do anything when we need to switch another third-party cache API.

what

Adjustment.

Basecachemanager

This is the main class of the web portal cache frame. It is the most basic implementation of the ICACHEMANAGER interface. Create a BaseCachemanager to set all the cache related methods in a class. It is designed to be a single case mode to ensure that there is an instance of only one ICACHEManager in the JVM of the servlet container. In a cluster environment for multi-web server / servlet container instances, each JVM will create an independent ICachemanager instance. If you want to convert to a different cache API in the future, this is the only class that needs to be modified for the new cache API. If you switch to JCache-compatible cache implementation, the modification of the cache manager will be small.

IcacheLoader

This interface is used to implement real data access logic at the web client. All client applications that need to use a cache mechanism must implement the interface. It includes only one method called loadCacheObject (), there are two input parameters: a string parameter makes a cache area name, an object parameter makes the cache key value. In this way, the cache manager will know which client program (running the loadCacheObject method) to overwrite the objects in the cache when the cached object exceeds the specified "living time".

Iconkey

The purpose of the ICacheKey interface is to hide the details of a specific creating buffer key value. Sometimes the key value of the cache is not a simple string. It may be as complex as multiple objects, getting these values ​​from the data source requires multiple lookup methods instead of a single way. In this case, the ICACHEKEY interface can be used to define all complex logic of creating buffer keys. In this way, the cache key value creation logic will be defined as an independent class. I have written a simple class TestCachekey to implement the interface and implement the getCacheKey () method to demonstrate the use of the interface.

Cacngens

A cache area is defined as an organization to accommodate a set of cache objects. You need to define a cache area in the configuration file to store data in a separate memory space to manage the validity of the cache data. If desired, objects with similar features (eg, survival time and service purposes) should be cached in the same cache area, allowing them to fail in the same time. I define the separated cache area to store static data and small changes data. In order to avoid the efficiency of synchronous operations, I use a separate Cache (JCS) instance for each cache area. CacheElementInfo

This class is used to encapsulate all cache statistics (eg, hit numbers, non-moderate, hit ratios, etc.) to monitor all cache objects in the web application.

Compile, build, and unit test

I created a build script with ANT to compile all the code in my object cache frame. The build script build.xml is placed in a web-inf / class directory. I also wrote the JUnit test client to test different cache scenes using the web portal cache frame. Test script CachingTestCase, placed in a web-inf / class / common / caching / test directory. Unzip the sample code to a new web application directory, if you want to verify the JUnit test script, run the following command from the command line:

Switch the current directory to% tomcat_home / webAPPS / Web-App-Name / Web-INF / CLASSES (in the UNIX test environment, the directory should be $ TOMCAT_HOME / WebApps / Web-App-Name / Web-INF / CLASSES.

Run the following command:

Ant Common.Compile compiles all Java classes in the cache frame.

Ant Common.Runjunit is used to run the JUnit test script. Test scripts use the Log4j API to display all output information.

consider

what

Guidelines for using object cache

When you decide, please refer to these guidelines when you want to cache some specific categories of data in your web app. The cached application should be careful, only when other methods such as data, such as data access, etc., can no longer be further improved. The cache will bring complexity, making maintenance work more complicated. Therefore, it is necessary to oversee the balance between the complexity of performance and cache.

When considering the use of cache, you need to consider the predetermined execution time and refresh rate of the object or the time of survival called objects. Cache cannot accommodate all we want to store data, so the memory used by the cache is released in time, that is, the cached object can be explicitly released by defining reasonable living time, or explicitly releases the cached object when the data is no longer needed. You can specify a cache algorithm such as the recent access algorithm (LRU) or at least the algorithm (LFU) to cache the access frequency to release the object. Jack Shirazi's book Java performance adjustment provides a very interesting discussion about the cache topic, discussed what type of data should be cached, and

what

Use the cache recommendation.

Note that the cache framework does not process creation of objects that need to be cached in a web application (for example, data access logic from data source retrieving data is not written in the cache class). This depends on the customer program to define the real data access logic. Technologies such as Java data objects are often used to encapsat data access logic in an enterprise web application. Refer to O'Reilly's Java data object to learn more about

what

Know the data access layer to the business logic layer.

in conclusion

This article provides a summary of the Java Cache System (JCS) using Jakarta to develop object cache frameworks for Web Portal. This framework is very stable and can be reused for other

what

Web applications can even be used for Java applications for client / server mode. This article details the working principle of the web portal cache framework, and provides the JUnit test script to test the cache framework of different scenes. JCS WAS BUILT AS A SYSTEM CLOSE TO JCACHE JAVA TEMPORARY CACHING API (JSR-107), A Description of the Caching System Used in Oracle 9i and Other Popular Frameworks. This specification may be as a Java in the future JDK release. Extended framework. One of my purposes is to keep the Web portal cache framework and JCS loosely coupled. In this case, if I need to switch to another frame (for example, JCache) in the future, I can complete the switching without making a big adjustment to the web portal.

I now measure the efficiency of the cache by recording the log of cache monitoring information such as: hit number, no moderation, hit rate. There may be other parameters to be monitored to measure the efficiency of the cache. Similarly, it is used to measure or do not use a feedback time for data access to data access, and some load test tools should be used to test their scalability and performance effects.

Keeping a cache synchronization in a chart environment will be a challenge because each servlet container will have a cache manager instance in its JVM. The method of solving this problem is to create a message-driven bean (MDB) notify all cache managers when you need to refresh the data.

Usually object lookup methods, such as: Simple HashTable, JNDI, even EJB, provides methods for storing objects in memory and find objects through key value. But

what

One method does not provide a mechanism that is removed from memory when the object is not required, or automatically creates an object when the access object is late for the object storage period. The httpsession object (in the servlet package) also allows objects to be cached, but it does not have shared, failed, single object storage period, automatic loading, or spooling these cache frames requires the basic mechanism.

Although additional design and development work is required to integrate cache functions into web applications, I think the benefits of cache have greater than the extra work. I have seen that after I have achieved the cache frame, my web application has great improvements, especially in terms of accessing static data and finding results. The web application module is currently in the test phase. In the near future, I will provide some performance test data (including the use of the case where the cache is used) is compared.

what

Help design faster, more scalability web applications.

Sample code

CachingCode.zip

Reference resource

Java Performance Adjustment, Jack Shirazi, O'Reilly

JCS homepage

JCache API

Oscache

Spiritcache

Commons Collectes

Coherence

Object Caching Service for Java

Srini Penchikala is a software consultant, currently working for Computer Consultants of America, Inc..

转载请注明原文地址:https://www.9cbs.com/read-27633.html

New Post(0)