Document Number: PT-TR-Cache
Prient Ecache Using Documents
PRIENT ECACHE Architect Document
V3.0
Initial release time: 2004/05/24
Current Edition Release time: 2004/06/01
Peng Tian Information System (Beijing) Co., Ltd.
Technology research and development
table of Contents
Basic information ... 3
Features ... 4
Technology background ... 4
Interface range ... 4
API description ... 4
Precautions ... 5
Development and test instance ... 5
Local method test ... 5
Distributed mode test ... 5
Notification extension implementation ... 9
Basic Information
Document Description Information:
file name:
Prient Ecache Using Documents
The latest version of:
V3.0
Document creator:
Peng Tian Company Technology R & D Department
Document maintenance:
Organization: Peng Tian Company Technology Research and Development Department
Member: Era, Chen Yonghui, take people
Document reviewer:
Peng Tian Company Technology Research Department, Nanjing Local Tax Project Group, Xi'an Local Tax Project Group
Date of order:
2004-06-01
Document goal:
This document describes the technical details of Prient Ecache as well as the interface and use of the interface and use when describing the theoretical based on, concept, feature, and instance.
Document modification summary:
version
date
Modify description
Author
V3.0
2004-06-01
Prient Ecache Using Documents
Original force
Prient Ecache has high-speed retrieval and distributed, and it is a THREADSAFE thread secure Cache product.
Features
1, support local way access
2, support distributed synchronization mechanism
3, divided into cache independent management
4, can perform external configuration
5. Comply with thread safety specifications, no in-memory leakage, support stage Cache and permanent Cache
6, easy to expand
7, the document is complete
technical background
Ecache uses the following techniques to improve the Cache mechanism
1. Use lrumap as the element storage method of cache, use the LRU algorithm
2, use Concurrent's high-efficiency LinkedHashMap to store the index column of the cache element
3. Use the DiskStore extension to ensure that the Cache data is complete in the memory cache technology.
4. Use JGroup technology to perform Cache synchronization between Cluster
5, define the NOTIFICATION interface to extend the personality requirements of Cache on the business layer
6. Provide local cacheManager for direct access to local cache
7. Provide MulticacheManager for Multi mode to encapsulate Local Cache, producing multicache for distributed processing
8. Provide ObjectCache interface for developers to develop cache to encapsulate local cache
Interface range
Core Programs
Cache.java Local Cache implements the interface for local cache access, with local CacheManager management
Cacheentity.java cache stores elements package that requires Cache objects, cacheentity provides properties of the lifecycle, version, access rate, etc. of the elements.
Interface
1, ObjectCache.java Provides custom encapsulation interfaces for Local Cache
2, Notification.java provides application interfaces when distributed Cache in synchronization
Factory
CacheManager.java Local Cache Manager, managing local cache, can be packaged again, and there is a method of encapsulating muticachemanager.java
MulticacheManager.java encapsulates CacheManager and provides distributed access
ExtensibilityMuticache.java extends local cache to provide distributed access
API description
For details, please refer to Ecache.chm (javadoc is Chinese)
Precautions
1
For these Collection
List, you can use the vector instead, you can also use com.ptf.util.concurrent.copyonwriteaRaylist instead
Hashmap, you can use HashTable instead, you can also use com.ptf.util.concurrent.linkedHashmap or com.ptf.util.concurrent.concurrenthashmap instead
Some non-serializabel's Collection can find alternative implementations in com.ptf.util.concurrent.
2, if you apply Cache in a JVM, you can use CacheManager and Cache directly.
3, if you use Cache in multiple JVM or distributed environments, you can use MulticacheManager and Multicache
4, the broadcast sender cannot receive the broadcast of its own, and the sender needs to manually perform the use of Notification () method.
5, broadcast mode has been defined and tested, please do not modify the broadcast mode freely
Development and test instance
Provides a test of various applications in Package Com.ptf.ECTEST;
contain
Local test
COM.PTF.ECTEST.CACHEMGRTEST Test for local cache
com.ptf.ectest.cachetest is a cache test in Junit
Distributed mode test
com.ptf.ECTEST.MULTICACHEMGRCONSOLETEST
com.ptf.ECTEST.MULTICACHEMGRRECIVEDTEST
com.ptf.ECTEST.MULTICACHEMGRREMOTEST
Two process tests in these three programs are accepted by broadcast conditions
REMOVE is responsible for sending REMOVE actions
Code example
1. Basic operations for Cache
Try
{
// getMultiManager (NULL, NULL); two parameters in the middle If you don't need to use other configurations, you can set it empty
// The first parameter represents the configuration file using cache, such as ECACHE.XML (not recommended)
// Second parameter represents the use of additional broadcast protocols, such as jgroup.xml (not recommended)
MulticacheManager Manager = MulticacheManager.getMultiManager (NULL, NULL);
/ / Get the specified cache from the Cache group
Multicache cache = manager.getMulticache ("SampleCache1");
System.out.println (Manager.getMulticachenames (). Length "");
For (int i = 0; i <500000; i )
{
// Add element operation to cache, but not notification
//cache.multistore (Nitofic); adding elements for broadcasting methods
Cache.addcacheentity ("key [" i "]", "value1 [" i "]");}
Long ST = system.currenttimemillis ();
For (int I = 100; i <200; i )
{
/ / Do not know the Cache
Cacheentity Entity = cache.getcacheentity ("Key [" i "]");
System.out.println (Entity);
}
Long et = system.currenttimemillis ();
Long ft = etc - st;
System.out.println (ft);
System.out.println (cache.getsize ());
}
Catch (CacheException EX)
{
EX.PrintStackTrace ();
}
Catch (IllegaLaRgumentException EX)
{
EX.PrintStackTrace ();
}
2. Operation of Remove for Cache and notify the distributed cache
Try
{
// getMultiManager (NULL, NULL); two parameters in the middle If you don't need to use other configurations, you can set it empty
// The first parameter represents the configuration file using cache, such as ECACHE.XML (not recommended)
// Second parameter represents the use of additional broadcast protocols, such as jgroup.xml (not recommended)
MulticacheManager Manager = MulticacheManager.getMultiManager (NULL, NULL);
/ / Get the specified cache from the Cache group
/ / For the distributed operation of Cache, you must operate on the same designated cache, other Cache is not affected.
Multicache cache = manager.getMulticache ("SampleCache1");
System.out.println (Manager.getMulticachenames (). Length "");
For (int i = 0; i <500000; i )
{
// Add element operation to cache, but not notification
//cache.multistore (Nitofic); adding elements for broadcasting methods
Cache.addcaches ("key [" i "]", "value1 [" i "]");
}
Long ST = system.currenttimemillis ();
For (int I = 100; i <200; i )
{
/ / Do not know the Cache
Cacheentity Entity = cache.getcacheentity ("Key [" i "]");
System.out.println (Entity);
}
// Clear distributed cache in // Clear Cache notification
CLEARALLNOTIFICATION N = New CLEARALLNOTIFICATION ("SampleCache1"); // Broadcast You can't receive it, you need to execute your Notification Execute () method
n.execute (cache);
// Call the MulticacheManager interface to send
/ / Other JVM or server Cache will accept the notification and execute the operation and the operation of this operation.
Manager.send (n);
Long et = system.currenttimemillis ();
Long ft = etc - st;
System.out.println (ft);
System.out.println (cache.getsize ());
}
Catch (CacheException EX)
{
EX.PrintStackTrace ();
}
Catch (IllegaLaRgumentException EX)
{
EX.PrintStackTrace ();
}
3. Access to local cache
Try
{
// Use the list to create a Cache initialization environment
CacheManager Manager = cacheManager.create ();
// Get cache that has been created
Cache cache = manager.getcache ("samplecache1");
System.out.println (Manager.getCachenames (). Length "");
For (int i = 0; i <500000; i )
{
/ / Add an element to cache
Cache.addcaches ("key [" i "]", "value1 [" i "]");
}
Long ST = system.currenttimemillis ();
For (int i = 100000; i <200000; i )
{
// Get the elements already in cache
Cache.getCacheentity ("Key [" i "]");
}
Long et = system.currenttimemillis ();
Long ft = etc - st;
System.out.println (ft);
System.out.println (cache.getsize ());
}
Catch (CacheException EX)
{
EX.PrintStackTrace ();
}
4, dynamically create a cache
Try
{
// Use the list to create a Cache initialization environment
CacheManager Manager = cacheManager.create ();
// Dynamically create cache and register into CacheManager
// Parameter 1: Cache Name
// Parameter 2: Maximum number of elements of Cache
// Parameter 3: Whether it is occupied in maximum, cache new to disk cache
// Parameter 4: This cache is permanent Cache
// Parameter 5: Element activity time
// Parameter 6: Element free time
Cache Cache = New Cache ("Testcache", 1000, True, False, 5, 2);
Manager.addcache (cache);
System.out.println (manager.getcachenames (). Length ""); for (int i = 0; i <500000; i )
{
/ / Add an element to cache
Cache.addcaches ("key [" i "]", "value1 [" i "]");
}
Long ST = system.currenttimemillis ();
For (int i = 100000; i <200000; i )
{
// Get the elements already in cache
Cache.getCacheentity ("Key [" i "]");
}
Long et = system.currenttimemillis ();
Long ft = etc - st;
System.out.println (ft);
System.out.println (cache.getsize ());
}
Catch (CacheException EX)
{
EX.PrintStackTrace ();
}
Notification Extension Implementation
Public string getcachename (); // get specific Cache name
Public string getcommand (); // Get notification type
Public serializable getKey (); // Get Entity Key
Public void setcachename (String Cachename); // Setting specific Cache name
Public void setKey; // Setting Entity Key
Abstract Void Execute (ObjectCache Cache); / / Actuator Interface
Public String Tostring (); // Tostring Interface
1. In this interface, the primary implementation of the execute () method is that the distributed cache performs the entry of the local Cache operation after obtaining the notification.
2, other parameters are for Execute () preparation parameters
3. When the Notification interface is implemented, the developer can customize the attribute to meet the execute () method needs.
4, the command attribute is the name of the identity of this Notification, this identity should be unique
The following is an example of a NOTIFICATION
Package com.ptf.ecache.cluster;
Import java.io. *;
Import com.ptf.ecache. *;
/ **
* Communication between distributed Cache container managers to perform a notification implementation of all Cache elements operations
* This notice is implemented for all EntityReload operations at the specified cache
* @Author com.prient.techresearch
* @Author
* @since May 18, 2004
* @version 1.1
* /
Public Class ReloadNotification
Implements notification
{
/ / -------------------------------------------------------------------------------------------- -------------------------
// Property item
/ / -------------------------------------------------------------------------------------------- ------------------------- / **
* Specific Cache Name
* /
Private string cachename;
/ **
* This notification command name
* /
Private final static string command = "(reloadall)";
/ **
* Specific Entity Key, not used in this example
* /
PRIVATE SERIALIZABLE KEY;
/ **
* Set up Cache name
* @Param CahcName String
* /
Public void setcachename (String Cahcname)
{
THIS.CAChename = cahcname;
}
/ **
* Get cache names
* @Return String name
* /
Public String getCachename ()
{
Return Cachename;
}
/ **
* Set Entity Key
* @Param Key Serializable
* /
Public Void SetKey (Serializable Key)
{
THIS.KEY = KEY;
}
/ **
* Get Key
* @Return Serializable
* /
Public serializable getKey ()
{
Return Key;
}
/ **
* Get the command name of this notification
* @Return the final value of static.
* /
Public String getcommand ()
{
Return Command;
}
/ / -------------------------------------------------------------------------------------------- -------------------------
// Structure method
/ / -------------------------------------------------------------------------------------------- -------------------------
/ **
* The default constructor requires Cachename to make this notification structure
* @Param Cachename String Cache Name
* /
Public reloadNotification (String CahcName)
{
THIS.CAChename = CACHENAME
}
/ **
* Command Actuator Implementation
* This Actuator uses the local cache for Reload operation
* It is divided into two parts, 1: Clear designated Cache, 2: CACHE loading according to the business
* @Param Cache ObjectCache Cache interface
* /
Public void Execute (Objectcache Cache)
{
Try
{
Cache.doclerall ();
//cache.doload () This method is related to the business, this is not implemented here
}
Catch (IOException EX)
{
EX.PrintStackTrace ();
}
Catch (IllegalStateException EX)
{
EX.PrintStackTrace ();
}
}
/ **
* TOSTRING implementation
* @Return String
* /
Public String Tostring ()
{
StringBuffer buffer = new stringbuffer ();
Buffer.Append (Command Cachename);
Return buffer.toString ();
}