如何Java的缓存系统 (JCS) 作品?

概观: 在现代应用程序开发的缓存是在应用程序设计要考虑的最重要的方面之一. 缓存中最常见的目的是为了改善性能. 缓存可以被定义为对象的集合. 它包含键作为唯一标识状态的对象. 任何缓存系统中最常见的存储在内存中. 但缓存系统也可以存储在磁盘项, 数据库或任何其他持久性存储器. 在Java缓存系统 (JCS) 是一个分布式的缓存框架用Java开发. 它提高了提供支持来管理各种动态缓存data.JCS应用程序的性能在内存和磁盘缓存支持. 参谋长联席会议框架是通过属性文件,完全可配置的,并提供了很大的灵活性缓存管理.

在本文中,我将讨论工作示例JCS框架.

介绍: 在大部分的Web应用程序的, 数据被从数据库中检索. 数据库的操作是昂贵和费时的. Now a day the web applications are data intensive and first response time is the basic criteria for its success. If the web application is frequently accessing database for each request then its performance will be slow. So the web applications are following different design techniques for reducing latency times and scale up.

JCS is a composite distributed caching system that works on JDK 1.4 and upper versions. The performance of JCS is very impressive for low put and high read applications.
Following are some of the major features of Java Caching System
· 内存管理
· 线程池控制
· 可扩展的框架
· 配置运行时参数
· 主要基于数据检索
· 分布式缓存复合
· 元素事件处理

JCS – The Composite Cache: The foundation of Java Caching System is based on composite cache. There are four types of caches available for each region. Any type of cache can be plugged into the JCS configuration to make it active. Following are the four different caches and their details.

  • Memory Cache (LRU): This is the widely used basic caching mechanism. The memory cache is extremely fast. It uses Least Recently Used (LRU) algorithm to manage the objects stored in the memory. The LRU memory cache is based on its own LRU Map implementation. This Map implementation is much faster compared to LRUMap implementation or any other type of Map.
  • Disk Cache (Indexed): The disk cache is used to store data when the memory cache is full. When indexing is used with the disk cache, the performance much faster. Queue based process is used to store data on the disk. The disk cache is completely configurable. A thread pool is used to manage the queue worker threads which also make the process faster.
  • JDBC Disk Cache: This is another type of disk cache which is reliable, fast and fully configurable. The basic storage is JDBC compatible database. Both the keys and values are stored in the database. The data is stored as a BLOB and the expired data elements are also removed periodically. Thread pool can also be used to manage queue worker threads for faster processing.
  • TCP Lateral Cache: This is a mechanism to distribute cached data on multiple distributed servers. This is also completely configurable caching system. It can also use UDP discovery mechanism to add nodes without disturbing the entire system. The TCP Lateral Cache establishes connections with socket servers on other nodes and performs the storage management. One server is sufficient to manage multiple regions.
  • Remote Cache using RMI: This is another caching option provided by JCS. In this mechanism each node is not required to connect with other node. The remote cache server can be used as a connection point and each node can connect with it. The remote cache server is used to hold a serialized version of your object. The remote server can be grouped with other servers to recover failover.

Dependency configuration: In the following section we will discuss a simple application using JCS. Before moving into it, we must have the following dependencies in the CLASSPATH. Please download the executable jars and put them in the CLASSPATH to build the application.

  • jcs-1.3.jar
  • commons-logging.jar
  • commons-lang-2.4.jar
  • jar

Working with JCS: In this section we will discuss different components of Java Caching System and the configuration details. We will explain each component with working example.

Following is a simple Employee class holding employee details. The class should implement Serializable interface as its data will be persisted. It contains getter/setter methods for reading and writing data.

清单1: Showing Employee class

public class Employee implements java.io.Serializable{

private String name;

private String address;

private String empid;

public Employee(String name, String address, String empid) {

this.name = name;

this.address = address;

this.empid = empid;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

public String getEmpid() {

return empid;

}

public void setEmpid(String empid) {

this.empid = empid;

}

}

Following is the main EmployeeManager class which imports JCS packages to use the framework APIs. It uses a cache region known as ‘empCache’ to put and get data into/from memory. The data is stored in the cache memory by using key/value pair. The retrieval is based on the key associated with each value. The application is showing addition/retrieval and removal of objects stored in a cache.

Listing2: Showing EmployeeManager class

import org.apache.jcs.JCS;

import org.apache.jcs.access.exception.CacheException;

public class EmployeeManager {

private JCS cache;

public EmployeeManager()

{

尝试

{

// Load the cache

cache = JCS.getInstance( “empCache” );

// Initialize the cache, Here data can be loaded during initialization

cache.put( “123”, new Employee( “缺口”, “Detroit.USA”, “123” ) );

cache.put( “143”, new Employee( “Ric”, “Seattle.USA”, “143” ) );

cache.put( “153”, new Employee( “Jhon”, “Chicago.USA”, “153” ) );

cache.put( “163”, new Employee( “Dan”, “Houston.USA”, “163” ) );

}

抓( CacheException e )

{
e.printStackTrace();

}

}

public void addEmployee( Employee emp )

{

尝试

{

cache.put( emp.getEmpid(), emp );

}

抓( CacheException e )

{

e.printStackTrace();

}

}

public Employee getEmployee( String empid )

{

return ( Employee )cache.get( empid );

}

public void removeEmployee( String empid )

{

尝试

{

cache.remove( empid );

}

抓( CacheException e )

{

e.printStackTrace();

}

}

公共静态无效的主要( 串[] 参数 )

{

// Create the employee manager

EmployeeManager empManager = new EmployeeManager();

// Add employees to the employee manager

/*empManager.addEmployee(new Employee(“Name1”, “address1”, “empid1”));

empManager.addEmployee(new Employee(“Name2”, “address2”, “empid2”));

empManager.addEmployee(new Employee(“Name3”, “address3”, “empid3”));*/

// Get employee

Employee emp = empManager.getEmployee(“123”);

System.out.println( “Employee details retrieved from cache: ” + emp.getName()+”-“+emp.getAddress());

// Remove employee

empManager.removeEmployee(“123”);

// After removal of employee

System.out.println( “Employee details after removal from cache: ” + empManager.getEmployee(“123”) );

}

}

Following is the configuration file cache.ccf. It holds all the configurable parameters with their values. The first section is the default cache region which holds the default values. If no custom region is configured then these default values will be used. The second section is defining the custom cache region which is empCache here. It defined the custom parameter values. The last section is the AUXILIARY CACHE region which describes the disk cache and other related parameters.

Listing3: Showing configuration file

# DEFAULT CACHE REGION

jcs.default=DC

jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes

jcs.default.cacheattributes.MaxObjects=1000

jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache

jcs.default.cacheattributes.UseMemoryShrinker=false

jcs.default.cacheattributes.MaxMemoryIdleTimeSeconds=3600

jcs.default.cacheattributes.ShrinkerIntervalSeconds=60

jcs.default.elementattributes=org.apache.jcs.engine.ElementAttributes

jcs.default.elementattributes.IsEternal=false

jcs.default.elementattributes.MaxLifeSeconds=21600

jcs.default.elementattributes.IdleTime=1800

jcs.default.elementattributes.IsSpool=true

jcs.default.elementattributes.IsRemote=true

jcs.default.elementattributes.IsLateral=true

# PRE-DEFINED CACHE REGIONS

jcs.region.empCache=DC

jcs.region.empCache.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes

jcs.region.empCache.cacheattributes.MaxObjects=1000

jcs.region.empCache.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache

jcs.region.empCache.cacheattributes.UseMemoryShrinker=false

jcs.region.empCache.cacheattributes.MaxMemoryIdleTimeSeconds=3600

jcs.region.empCache.cacheattributes.ShrinkerIntervalSeconds=60

jcs.region.empCache.cacheattributes.MaxSpoolPerRun=500

jcs.region.empCache.elementattributes=org.apache.jcs.engine.ElementAttributes

jcs.region.empCache.elementattributes.IsEternal=false

# AVAILABLE AUXILIARY CACHES

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=c:/temp

jcs.auxiliary.DC.attributes.MaxPurgatorySize=10000000

jcs.auxiliary.DC.attributes.MaxKeySize=1000000

jcs.auxiliary.DC.attributes.MaxRecycleBinSize=5000

jcs.auxiliary.DC.attributes.OptimizeAtRemoveCount=300000

jcs.auxiliary.DC.attributes.ShutdownSpoolTimeLimit=60

结论: In this article we have discussed about caching concepts and one of the most popular caching framework known as Java Caching System. The article also covered different components of JCS and their implementation. 最后,我们已经基于Java缓存系统框架,讨论了采样工作示例. 参谋长联席会议是非常强大的,用Java语言编写. 存储机制和它的内部设计是高读取,低放的应用非常强大. 实现完全可配置的,因此它可以与任何新的和现有的应用程序没有把很大的努力相结合.

Tagged on: ,
============================================= ============================================== 在亚马逊上购买最佳技术书籍,en,电工CT Chestnutelectric,en
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share