JCS (Java Cache System) basic structure analysis and use

Source: Internet
Author: User

JCS (Java Caching System) project: http://commons.apache.org/proper/commons-jcs/index.html

JCS is a distributed cache system written in Java. It is designed to speed up applications by providing a means to manage the various dynamic cache data , which is a composite buffer tool that is said to be beyond simple object caches, which can buffer objects into memory, hard disk (local disk, network address disk, database), With buffered object time expiration settings, you can also build a buffered distributed architecture with JCS for high-performance applications.

for some objects that require frequent access and are very resource-intensive each time they are accessed, far more read than write applications, JCS provides buffers, which can improve the performance of the service, and JCS provides a variety of helpers to match the buffer, making it more than just the object cache.


1. About the JCS version issue

JCS1.3 is the first official version of the Version:2.0-snapshot, which is currently released March 25, 2014 (November 22, 2014).

This article is based on the JCS1.3 documentation, which can be combined with the latest documentation, the basic usage and the overall structural changes are similar.

JCS 1.3 working in JDK1.3 and above, depending on Commons logging and Doug Lea ' s (one of the authors of Java concurrency programming) Util Concurrent

JCS 2.0 works in JDK1.6 and above, relying only on Commons Logging.

At present JCS1.3 is stable version, the basic composition of JCS not much change, just JCS1.3 support the lower version of the JDK, so there are some things to take into account, and JCS2.0 support JDK1.6 and above, is a new beginning. You can follow the next version and use JCS2.0 as much as possible.


Basic composition of 2.JCS


JCS by configuring property values to describe how to cache data, you can easily apply JCS to the system, and only adjust the values of the configuration properties to achieve different cache implementations and effects.


is the general composition of the JCS structure:


650) this.width=650; "style=" width:800px;height:435px; "src=" http://s3.51cto.com/wyfs02/M00/53/C9/ Wkiol1rwirnw2odaaagew-o4m_m323.jpg "title=" JCS structure "alt=" wkiol1rwirnw2odaaagew-o4m_m323.jpg "height=" 435 "hspace=" 0 "vspace=" 0 "width=" "border=" 0 "/>


JCS manages the cache through a combined cache manager. By configuring different buffers in a configuration file similar to log4j, a buffer named default is provided in JCS. For each cache, there is only one memory auxiliary (helper), and other such as disk aids, lateral distribution aids can be combined to enhance each buffer.


JCS obtains instance objects, such as Cacheaccess and Groupcacheacess, based on the different buffers in the configuration, to perform operations such as caching and obtaining data. In addition, the JCS is highly flexible and can be coded to define new buffers.


Description of the auxiliary device for 3.JCS

is a simple description of the JCS helper, due to the outdated documentation of JCS1.3, now JCS2.0 documents are updated ( http://commons.apache.org/proper/commons-jcs/index.html there are discrepancies in the description, and some of the features mentioned in the JCS1.3 documentation are not implemented specifically in the release version, but the JCS2.0 documentation has not been mentioned. Since JCS2.0 will support JDK1.6 and above, the change is larger. Document update is slow, this time recommended to view the source code is more reliable.


650) this.width=650; "style=" WIDTH:833PX;HEIGHT:627PX; "src=" http://s3.51cto.com/wyfs02/M00/53/CB/ Wkiom1rwiortb1qbaai0qipe1d4509.jpg "title=" JCS Helper description "alt=" wkiom1rwiortb1qbaai0qipe1d4509.jpg "height=" 627 "hspace= "0" vspace= "0" width= "833" border= "0"/>


Although JCS provides a richer complement, the indexedcacahe of memory assist and disk assist can be used in a personal perspective. Conversely, some of the features of scale-out and build distributed architectures can often be replaced by other alternatives. (PS: If not full display, you can click to view the full picture)


3.JCS Cache Configuration


The following is a simple configuration:

# default cache regionjcs.default=jcs.default.cacheattributes= org.apache.jcs.engine.compositecacheattributesjcs.default.cacheattributes.maxobjects= 1000jcs.default.cacheattributes.memorycachename= Org.apache.jcs.engine.memory.lru.lrumemorycachejcs.default.cacheattributes.usememoryshrinker= falsejcs.default.cacheattributes.maxmemoryidletimeseconds= 3600jcs.default.cacheattributes.shrinkerintervalseconds=60jcs.default.elementattributes= Org.apache.jcs.engine.elementattributesjcs.default.elementattributes.iseternal= Truejcs.default.elementattributes.maxlifeseconds=21600jcs.default.elementattributes.idletime= 1800jcs.default.elementattributes.isspool=truejcs.default.elementattributes.isremote= Truejcs.default.elementattributes.islateral=true# pre-defined cache regionsjcs.region.icache= dcjcs.region.icache.cacheattributes= org.apache.jcs.engine.compositecacheattributesjcs.region.icache.cacheattributes.maxobjects= 1000jcs.region.icache.cacheattributes.memorycachename=org.aPache.jcs.engine.memory.lru.lrumemorycachejcs.region.icache.cacheattributes.usememoryshrinker= falsejcs.region.icache.cacheattributes.maxmemoryidletimeseconds= 3600jcs.region.icache.cacheattributes.shrinkerintervalseconds= 60jcs.region.icache.cacheattributes.maxspoolperrun=500jcs.region.icache.elementattributes= Org.apache.jcs.engine.elementattributesjcs.region.icache.elementattributes.iseternal=falsejcs.auxiliary.dc= org.apache.jcs.auxiliary.disk.indexed.indexeddiskcachefactoryjcs.auxiliary.dc.attributes= org.apache.jcs.auxiliary.disk.indexed.indexeddiskcacheattributesjcs.auxiliary.dc.attributes.diskpath=${ User.dir}/jcs_swapjcs.auxiliary.dc.attributes.maxpurgatorysize=10000000jcs.auxiliary.dc.attributes.maxkeysize= 1000000jcs.auxiliary.dc.attributes.maxrecyclebinsize=5000jcs.auxiliary.dc.attributes.optimizeatremovecount= 300000jcs.auxiliary.dc.attributes.shutdownspooltimelimit=60


   The above is configured with two buffers Dafult (Jcs.default) and Icache (Jcs.region.icache), an auxiliaryConverter DC(Jcs.auxiliary.DC), where the buffer Icache uses a DC aux (JCS.REGION.ICACHE=DC).

This shows that Icache buffers can cache data by disk index.

In addition to the contents of the configuration How to write, you can refer to the document:

http://commons.apache.org/proper/commons-jcs/LocalCacheConfig.html

http://commons.apache.org/proper/commons-jcs/JCSPlugins.html

PS: Another approach is to configure the value of its properties (reading the source code) by using the API to view the corresponding Cache Property object (cacheattributes) and element attribute (elementattributes) object classes.


  use the sample code according to the buffer configuration above:

    public static void main (String[] args)  throws  cacheexception {        cacheaccess cacheaccess =  Jcs.getaccess ("Icache");        for  (int i = 0; &NBSP;I&NBSP;&LT;&NBSP;10000;&NBSP;I&NBSP;=&NBSP;I&NBSP;+&NBSP;2)  {             cacheaccess.put ("id"  + i, i);         }        int count = 0;         for  (int i = 0; i < 10000; i++)  {            Object o =  Cacheaccess.get ("id"  + i);             if   (Null == o)   {                 Cacheaccess.put ("id"  + i, i);                 count++;             }        }         System.out.println ("Cache count:" +count);        count = 0;         for  (int i = 0; i < 10000;  i++)  {            object o =  cacheaccess.get ("id"  + i);             if  (Null == o)  {                 cacheaccess.put("id"  + i, i);                 count++;            }         }        system.out.println ( "Cache count:" +count);     }


after running the above test code, a cache file is generated under the current working directory.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/53/C9/wKioL1RwIomBlnRqAAAgWJqo24w328.jpg "title=" JCS (3) . png "alt=" wkiol1rwiomblnrqaaagwjqo24w328.jpg "/>


4. Finally

Because this article is written according to JCS1.3, the inevitable and future JCS2.0 stable version has a large discrepancy, but the basic structure and usage of the JCS basically similar, the idea of the way to expand the support device still supports JCS, can continue to follow the development of JCS, this morning using the latest JCS2.0 version (this version supports JDK1.6 +).


Expand your profile:

JCS Configuration Item Memo: http://favccxx.blog.51cto.com/2890523/1204907

JCS precautions during use: http://99307064.blog.51cto.com/718663/1432996

five steps to using JCS to quickly build your cache environment: http://favccxx.blog.51cto.com/2890523/1204907

Doug Lea ' s Util Concurrent: http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html


This article from "Mustang Red" blog, declined reprint!

JCS (Java Cache System) basic structure analysis and use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.