001--jcs Getting Started

Source: Internet
Author: User
Tags temporary file storage

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
    1. #WEB-INF/CLASSES/CACHE.CCF (do not break the following line)
    2. jcs.default=
    3. Jcs.default.cacheattributes=org.apache.jcs.engine.compositecacheattributes
    4. jcs.default.cacheattributes.maxobjects=1000
    5. 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
  1. #定义一个硬盘缓冲区产生器 (Factory), named DC
  2. Jcs.auxiliary.dc=org.apache.stratum.jcs.auxiliary.disk.indexed.indexeddiskcachefactory
  3. Jcs.auxiliary.dc.attributes=org.apache.stratum.jcs.auxiliary.disk.indexed.indexeddiskcacheattributes
  4. Jcs.auxiliary.dc.attributes.diskpath=g:/dev/jakarta-turbine-stratum/raf
  5. #这里其实就是指明了缓冲文件存放到那里去.
  6. #然后, make the following changes
  7. Jcs.default=dc
  8. #这样, 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.
  9. #横向式的并行缓冲
  10. #并行式的配置如下
  11. Jcs.auxiliary.ltcp=org.apache.jcs.auxiliary.lateral.lateralcachefactory
  12. Jcs.auxiliary.ltcp.attributes=org.apache.jcs.auxiliary.lateral.lateralcacheattributes
  13. Jcs.auxiliary.ltcp.attributes.transmissiontypename=tcp
  14. jcs.auxiliary.ltcp.attributes.tcpservers=192.168.10.129:1121,192.168.10.222:1121
  15. jcs.auxiliary.ltcp.attributes.tcplistenerport=1121
  16. 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
  1. Jcs.region.testcache1=dc,ltcp
  2. Jcs.region.testcache1.cacheattributes=org.apache.stratum.jcs.engine.compositecacheattributes
  3. jcs.region.testcache1.cacheattributes.maxobjects=1000
  4. Jcs.region.testcache1.cacheattributes.memorycachename=org.apache.stratum.jcs.engine.memory.lru.lrumemorycache
  5. Jcs.region.testcache1.cacheattributes.usememoryshrinker=true
  6. jcs.region.testcache1.cacheattributes.maxmemoryidletimeseconds=3600
  7. Jcs.region.testcache1.cacheattributes.shrinkerintervalseconds=60
  8. System. Groupidcache
  9. #这个概念我也不是很清楚. However, the JCS documentation indicates that it is better to configure the following content.
  10. Jcs.system.groupidcache=dc
  11. Jcs.system.groupidcache.cacheattributes=org.apache.stratum.jcs.engine.compositecacheattributes
  12. jcs.system.groupidcache.cacheattributes.maxobjects=10000
  13. Jcs.system.groupidcache.cacheattributes.memorycachename= Org.apache.stratum.jcs.engine.memory.lru.LRUMemoryCache
  14. #这可能是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
  1. # DEFAULT CACHE Region
  2. # Sets the default AUX value for any non configured caches
  3. Jcs.default=dc
  4. Jcs.default.cacheattributes=org.apache.jcs.engine.compositecacheattributes
  5. Jcs.default.cacheattributes.maxobjects=1
  6. Jcs.default.cacheattributes.memorycachename=org.apache.jcs.engine.memory.lru.lrumemorycache
  7. Jcs.default.elementattributes.iseternal=true
  8. jcs.default.elementattributes.maxlifeseconds=360000
  9. jcs.default.elementattributes.idletime=1800
  10. Jcs.default.elementattributes.isspool=true
  11. Jcs.default.elementattributes.isremote=true
  12. Jcs.default.elementattributes.islateral=true
  13. # CACHE Regions AVAILABLE
  14. # auxiliary CACHES AVAILABLE
  15. # Primary Disk Cache--faster than the rest because of memory key storage
  16. Jcs.auxiliary.dc=org.apache.jcs.auxiliary.disk.indexed.indexeddiskcachefactory
  17. Jcs.auxiliary.dc.attributes=org.apache.jcs.auxiliary.disk.indexed.indexeddiskcacheattributes
  18. Jcs.auxiliary.dc.attributes.diskpath=./kpicache
  19. jcs.auxiliary.dc.attributes.maxpurgatorysize=100000000
  20. jcs.auxiliary.dc.attributes.maxkeysize=10000000
  21. jcs.auxiliary.dc.attributes.optimizeatremovecount=300000
  22. jcs.auxiliary.dc.attributes.maxrecyclebinsize=7500

Some methods of operation:

[Java]View Plaincopy
  1. Package com.zyujie.util;
  2. Import Org.apache.jcs.JCS;
  3. Public class Jcsmanagerdto {
  4. private static jcsmanagerdto instance;
  5. private static int checkedout = 0;
  6. public static JCS Objcache;
  7. Private Jcsmanagerdto () {
  8. try {
  9. Objcache = Jcs.getinstance ("Objcache");
  10. } catch (Exception e) {
  11. E.printstacktrace ();
  12. }
  13. }
  14. public static Jcsmanagerdto getinstance () {
  15. synchronized (jcsmanagerdto. Class) {
  16. if (instance = = null) {
  17. Instance = new Jcsmanagerdto ();
  18. }
  19. }
  20. synchronized (instance) {
  21. instance.checkedout++;
  22. }
  23. return instance;
  24. }
  25. Public Object Getobj (Object key) {
  26. Object obj = null;
  27. obj = (Object) objcache.get (key);
  28. return obj;
  29. }
  30. public void Storeobject (Object key, Object obj) {
  31. try {
  32. if (!key.equals ("")) {
  33. //Objcache.remove (key);
  34. }
  35. Objcache.put (key, obj);
  36. } catch (Exception e) {
  37. }
  38. }
  39. public void Clearobject (Object key) {
  40. try {
  41. Objcache.remove (key);
  42. } catch (Exception e) {
  43. }
  44. }
  45. public void Clear () {
  46. try {
  47. Objcache.clear ();
  48. } catch (Exception e) {
  49. }
  50. }
  51. public void Clearmatchobject (String key) {
  52. //Cacheaccess access1=cacheaccess.getaccess ("DC");
  53. //Java.util.Map map=access1.get.getmatchingcacheelements (key);
  54. }
  55. public static void Main (string[] args) {
  56. Jcsmanagerdto dto = Jcsmanagerdto.getinstance ();
  57. Dto.storeobject ("Test1", "111");
  58. Dto.storeobject ("Test2", "222");
  59. Dto.storeobject ("test3", "333");
  60. Dto.storeobject ("test4", "444");
  61. System.out.println ("test1 is" + dto.getobj ("test1"));
  62. System.out.println ("test2 is" + dto.getobj ("test2"));
  63. System.out.println ("test3 is" + dto.getobj ("test3"));
  64. System.out.println ("test4 is" + dto.getobj ("test4"));
  65. }
  66. }
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

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.