JCS for distributed cache sharing

Source: Internet
Author: User
Tags delete cache

To make a product and want the product to be distributed, you have to study the distributed cache.

There are many open-source projects for caching. Through testing, JCS is the easiest to configure and use, so it is used as the product cache manager.

 

JCS is an open source project of Apache, project URL: http://jakarta.apache.org/jcs/

Steps:

1. Download the project jar package and the dependent jar package

Jcs-1.3.jar

Commons-lang-2.3.jar

Commons-collections-2.1.1.jar

Concurrent-1.3.4.jar

2. JCs distributed configuration

Configure File Cache. CCF under your project WEB-INF/classes

The content is as follows:

JCs. Default = ltcp
JCs. Default. cache. Attributes = org. Apache. JCs. Engine. compositecacheattributes
JCs. Default. cacheattributes. memorycachename = org. Apache. JCs. Engine. Memory. LRU. lrumemorycache
JCs. Auxiliary. ltcp = org. Apache. JCs. Auxiliary. Lateral. Socket. tcp. lateraltcpcachefactory
JCs. Auxiliary. ltcp. Attributes = org. Apache. JCs. Auxiliary. Lateral. Socket. tcp. tcplateralcacheattributes
JCs. Auxiliary. ltcp. Attributes. putonlymode = true
JCs. Auxiliary. ltcp. Attributes. tcplistenerport = 1100
JCs. Auxiliary. ltcp. Attributes. udpdiscoveryaddr = 228.5.6.8
JCs. Auxiliary. ltcp. Attributes. udpdiscoveryport = 1101
JCs. Auxiliary. ltcp. Attributes. udpdiscoveryenabled = true

The preceding configurations enable multicast communication in the LAN. You can configure multiple servers. When one of the servers updates the cache, several other caches are notified to be updated at the same time to meet the distributed cache requirements.

3. Code Implementation

The implementation code is quite concise, with my Cache Management class attached.

When one machine implements the addcache. updatecache. removecache method, the cache synchronization event is triggered.

 

 

Appendix:

Package com. Framework. cache;

Import org. Apache. JCs. JCs;

/**
* Purpose: cache manager
*
* @ Author Gu Weimin
* @ Date 2009-11-12
*/
Public class cachemanager {
/**
* System cache body
*/
Public static JCs cache;
 
/**
* Initialize the cache manager.
* @ Autor Gu Weimin
*/
Public static void Init (){
Try {
Cache = JCs. getinstance ("systemcache ");
}
Catch (exception e ){
E. printstacktrace ();
}
}
 
/**
* Add Cache
* @ Autor Gu Weimin
*
* @ Param cachekey ID
* @ Param OBJ: cache object
* @ Return true or false
*/
Public static Boolean addcache (string cachekey, object OBJ ){
Try {
Cache. Put (cachekey, OBJ );
Return true;
}
Catch (exception e ){
E. printstacktrace ();
Return false;
}
}
 
/**
* Update Cache
* @ Autor Gu Weimin
*
* @ Param cachekey ID
* @ Param OBJ: cache object
* @ Return true or false
*/
Public static Boolean updatecache (string cachekey, object OBJ ){
Try {
If (Cache. Get (cachekey )! = NULL) removecache (cachekey );
Return addcache (cachekey, OBJ );
}
Catch (exception e ){
E. printstacktrace ();
Return false;
}
}
 
/**
* Delete Cache
* @ Autor Gu Weimin
*
* @ Param key ID
* @ Return true or false
*/
Public static Boolean removecache (string cachekey ){
Try {
Cache. Remove (cachekey );
Return true;
}
Catch (exception e ){
E. printstacktrace ();
Return false;
}
}
 
/**
* Get the specified cache object
* @ Autor Gu Weimin
*
* @ Param key ID
* @ Return object: cache object
*/
Public static object getcache (string cachekey ){
Try {
Return cache. Get (cachekey );
}
Catch (exception e ){
E. printstacktrace ();
Return NULL;
}
}
}

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.