JCS Getting Started
1. Overview
JCS is a sub-project of the Jakarta Project Turbine, which is a composite buffer tool with flexible configuration features. JCS provides memory, hard disk, distributed architecture, build cache server four ways to implement object cache, it is easy to realize the difference of cache customization. The buffer tool is significantly more useful for read operations than write operations.
2. JCS architecture Diagram
There are three core concepts involved: elements, regions, auxiliaries. A JCS can define multiple region, and each region can have its own definition of its survival time, storage mode, and number of objects. Region is a unit of object caching in JCS. Corresponding to region is the auxiliary. The way each region is stored and some attributes, such as survival time, are defined by auxiliary. Default is used to define defaults, and default values are used when a property is not defined in auxiliary. The region has a Hashtable component, which stores the objects that need to be cached and the corresponding key. Elements corresponds to hashtables.
3. function
JCS now supports both the LRU and MRU of the memory buffer algorithm. Usually, the LRU algorithm is used.
Using a memory buffer requires a buffer size to be defined, and when the buffer limit is exceeded, the buffer content is discarded. If there is a drive buffer, the squeezed buffer is written to the hard disk buffer.
JCS for buffered objects, you can set the buffer expiration time, an object that stays in the buffer longer than this time, will be considered "not fresh" and is discarded.
Indexed hard disk buffering
On the one hand, in order to avoid the buffer too large, to burst the memory of the virtual machine, on the other hand, but also want to be able to buffer more objects, JCS can buffer the size of the object to cache to the hard disk. Configuration is also more convenient, only need to specify the buffer temporary file storage directory location. Hard disk Buffering writes the contents of the buffered object to a file, but keeps the access index in memory, thus achieving the highest possible access efficiency.
Parallel-distributed buffer (lateral)
In general, objects are buffered in memory, improving the performance of the application on the one hand, while allowing applications to be distributed without distribution. Because it is assumed that an application configuration runs in parallel on both servers, and that the two servers are buffered separately, it is easy to cause inconsistencies in the version of the two buffer contents to fail. The data is modified on one machine, which affects the local memory buffer and the database server, but does not notify the other server, causing the data to be buffered on the other side to be effectively invalid.
Parallel distributed buffering is the solution to this problem. Can be configured, a few servers into a buffer group, each server in the group has data updates, the content will be updated horizontally through the TCP/IP protocol to the buffer layer of other servers, so that the above situation can be guaranteed. The disadvantage of this is that if the number of parallel servers in the group increases, the amount of data transfer within the group will increase rapidly. This scenario is suitable for a smaller number of parallel servers.
Client/server-type buffer (Remote)
Customer/service-side buffered clusters. This approach supports a primary server and up to 256 clients. The client's buffer layer attempts to connect to the primary server, and if the connection succeeds, it is registered on the primary server. Each client has data updates that are notified to the primary server, and the master notifies all clients except the message source of the update.
Each client can be configured with more than one server, the first server is the primary server, if the connection to the first server fails, the client tries to connect with the standby server, if the connection succeeds, the standby server will be able to talk to other clients, and periodically continue to attempt to connect with the primary server. If the standby server also fails to connect, it will attempt to connect to the next standby server in the configured order.
In this way, the update notification is a lightweight, data update on a machine, does not transfer the entire data, but only notifies an ID, when the remote other machine receives the update notification, the corresponding ID of the buffer object will be removed from the local memory buffer, to ensure that there is no error data in the buffer.
This kind of construction needs to configure the client and server separately, the configuration is more troublesome.
4. Configuration
One of the benefits of JCS is that applications can be developed without the need to conceive the underlying buffer configuration architecture. The same application, only need to modify the configuration, you can change the buffer structure, do not need to modify the application's source code. The configuration method is also relatively simple, which is to modify the configuration file CACHE.CCF. This file is placed under the Web-inf/classes directory. Configure a configuration file format similar to log4j. Here's a look at the configuration method using various buffer structures.
Memory buffering
[Plain]View Plaincopy
- #WEB-INF/CLASSES/CACHE.CCF (do not break the following line)
- jcs.default=
- Jcs.default.cacheattributes=org.apache.jcs.engine.compositecacheattributes
- jcs.default.cacheattributes.maxobjects=1000
- Jcs.default.cacheattributes.memorycachename=org.apache.jcs.engine.memory.lru.lrumemorycache
The default buffering properties are configured above. In an application, because of the different object types, multiple buffers may be used, each buffer will have a name, and if there are no properties in the configuration file indicating a particular buffer, all buffers will be built according to the default properties. The above contents indicate that the buffer size is 1000 objects, and the memory buffer uses the Lrumemorycache object. Optionally, Mrumemorycache, you should be able to customize the new memory buffer. 1000 buffered objects This capacity means that each buffer is buffered by 1000, not the total buffer capacity. The above configuration, you can let the application run up.
Time Expires
If you need to introduce a time expiration mechanism, you need to add
Jcs.default.cacheattributes.cacheattributes.usememoryshrinker=true
jcs.default.cacheattributes.cacheattributes.maxmemoryidletimeseconds=3600
Jcs.default.cacheattributes.cacheattributes.shrinkerintervalseconds=60
This indicates that the object expires in excess of 3,600 seconds and is checked every 60 seconds.
indexed hard disk buffering
Indexed hard disk buffering is a kind of auxiliary buffering, the use of the following things need to do
[Plain]View Plaincopy
- #定义一个硬盘缓冲区产生器 (Factory), named DC
- Jcs.auxiliary.dc=org.apache.stratum.jcs.auxiliary.disk.indexed.indexeddiskcachefactory
- Jcs.auxiliary.dc.attributes=org.apache.stratum.jcs.auxiliary.disk.indexed.indexeddiskcacheattributes
- Jcs.auxiliary.dc.attributes.diskpath=g:/dev/jakarta-turbine-stratum/raf
- #这里其实就是指明了缓冲文件存放到那里去.
- #然后, make the following changes
- Jcs.default=dc
- #这样, all buffers that do not specifically specify a property will use a hard disk buffer themselves, and the buffer file will be named in the name of the buffer. stored in the specified directory.
- #横向式的并行缓冲
- #并行式的配置如下
- Jcs.auxiliary.ltcp=org.apache.jcs.auxiliary.lateral.lateralcachefactory
- Jcs.auxiliary.ltcp.attributes=org.apache.jcs.auxiliary.lateral.lateralcacheattributes
- Jcs.auxiliary.ltcp.attributes.transmissiontypename=tcp
- jcs.auxiliary.ltcp.attributes.tcpservers=192.168.10.129:1121,192.168.10.222:1121
- jcs.auxiliary.ltcp.attributes.tcplistenerport=1121
- Jcs.auxiliary.ltcp.attributes.putonlymode=false
The configuration here is to implement parallel buffering on 41,129,221 of three machines.
Everyone listens on port 1121 and connects to two other machines. If the connection fails, it waits for one time before connecting again until the connection is successful. Any buffer in each of the three machines is updated, such as the put and remove actions, and the update is passed to the other two.
Specify the properties of a buffer individually
If, for a buffer, such as TestCache1, you need to configure the properties separately, you can configure the following.
[Plain]View Plaincopy
- Jcs.region.testcache1=dc,ltcp
- Jcs.region.testcache1.cacheattributes=org.apache.stratum.jcs.engine.compositecacheattributes
- jcs.region.testcache1.cacheattributes.maxobjects=1000
- Jcs.region.testcache1.cacheattributes.memorycachename=org.apache.stratum.jcs.engine.memory.lru.lrumemorycache
- Jcs.region.testcache1.cacheattributes.usememoryshrinker=true
- jcs.region.testcache1.cacheattributes.maxmemoryidletimeseconds=3600
- Jcs.region.testcache1.cacheattributes.shrinkerintervalseconds=60
- System. Groupidcache
- #这个概念我也不是很清楚. However, the JCS documentation indicates that it is better to configure the following content.
- Jcs.system.groupidcache=dc
- Jcs.system.groupidcache.cacheattributes=org.apache.stratum.jcs.engine.compositecacheattributes
- jcs.system.groupidcache.cacheattributes.maxobjects=10000
- Jcs.system.groupidcache.cacheattributes.memorycachename= Org.apache.stratum.jcs.engine.memory.lru.LRUMemoryCache
- #这可能是JCS自己的组管理体系上的缓冲区.
Client/server-type buffer (Remote)
This architecture requires a separate configuration of the client and server, and if you want to study, you can view the http://jakarta.apache.org/turbine/jcs/RemoteAuxCache.html
Here is an example of a previous system configuration: CACHE.CFF Configuration
[Plain]View Plaincopy
- # DEFAULT CACHE Region
- # Sets the default AUX value for any non configured caches
- Jcs.default=dc
- Jcs.default.cacheattributes=org.apache.jcs.engine.compositecacheattributes
- Jcs.default.cacheattributes.maxobjects=1
- Jcs.default.cacheattributes.memorycachename=org.apache.jcs.engine.memory.lru.lrumemorycache
- Jcs.default.elementattributes.iseternal=true
- jcs.default.elementattributes.maxlifeseconds=360000
- jcs.default.elementattributes.idletime=1800
- Jcs.default.elementattributes.isspool=true
- Jcs.default.elementattributes.isremote=true
- Jcs.default.elementattributes.islateral=true
- # CACHE Regions AVAILABLE
- # auxiliary CACHES AVAILABLE
- # Primary Disk Cache--faster than the rest because of memory key storage
- Jcs.auxiliary.dc=org.apache.jcs.auxiliary.disk.indexed.indexeddiskcachefactory
- Jcs.auxiliary.dc.attributes=org.apache.jcs.auxiliary.disk.indexed.indexeddiskcacheattributes
- Jcs.auxiliary.dc.attributes.diskpath=./kpicache
- jcs.auxiliary.dc.attributes.maxpurgatorysize=100000000
- jcs.auxiliary.dc.attributes.maxkeysize=10000000
- jcs.auxiliary.dc.attributes.optimizeatremovecount=300000
- jcs.auxiliary.dc.attributes.maxrecyclebinsize=7500
Some methods of operation:
[Java]View Plaincopy
- Package com.zyujie.util;
- Import Org.apache.jcs.JCS;
- Public class Jcsmanagerdto {
- private static jcsmanagerdto instance;
- private static int checkedout = 0;
- public static JCS Objcache;
- Private Jcsmanagerdto () {
- try {
- Objcache = Jcs.getinstance ("Objcache");
- } catch (Exception e) {
- E.printstacktrace ();
- }
- }
- public static Jcsmanagerdto getinstance () {
- synchronized (jcsmanagerdto. Class) {
- if (instance = = null) {
- Instance = new Jcsmanagerdto ();
- }
- }
- synchronized (instance) {
- instance.checkedout++;
- }
- return instance;
- }
- Public Object Getobj (Object key) {
- Object obj = null;
- obj = (Object) objcache.get (key);
- return obj;
- }
- public void Storeobject (Object key, Object obj) {
- try {
- if (!key.equals ("")) {
- //Objcache.remove (key);
- }
- Objcache.put (key, obj);
- } catch (Exception e) {
- }
- }
- public void Clearobject (Object key) {
- try {
- Objcache.remove (key);
- } catch (Exception e) {
- }
- }
- public void Clear () {
- try {
- Objcache.clear ();
- } catch (Exception e) {
- }
- }
- public void Clearmatchobject (String key) {
- //Cacheaccess access1=cacheaccess.getaccess ("DC");
- //Java.util.Map map=access1.get.getmatchingcacheelements (key);
- }
- public static void Main (string[] args) {
- Jcsmanagerdto dto = Jcsmanagerdto.getinstance ();
- Dto.storeobject ("Test1", "111");
- Dto.storeobject ("Test2", "222");
- Dto.storeobject ("test3", "333");
- Dto.storeobject ("test4", "444");
- System.out.println ("test1 is" + dto.getobj ("test1"));
- System.out.println ("test2 is" + dto.getobj ("test2"));
- System.out.println ("test3 is" + dto.getobj ("test3"));
- System.out.println ("test4 is" + dto.getobj ("test4"));
- }
- }
5, involving the package
Dependent jar Packages
Jcs-1.3.jar
Commons-lang-2.3.jar
Commons-collections-2.1.1.jar
Concurrent-1.3.4.jar
001--jcs Getting Started