Cache, java cache technology

Source: Internet
Author: User

Cache, java cache technology

Cache refers to the intermediate storage, which is equivalent to a transfer station, accumulating a certain amount of goods and then transporting it to the destination. If there is no transfer station, one piece of shipping will occur, which consumes a lot of manpower and material resources.

The cache base class is: Buffer

The basic subclasses of the cache are Char/Byte/Short/Int/Long/Float/Double + Buffer.

 

Cache is usually stored using an array. The array () method can obtain this array, but not all buffers can obtain this array. the return value of hasArray () method is used, check whether the Buffer can obtain the array. In either case, the Buffer cannot obtain an array.

First, directly store the Buffer: Use new to create an array. The storage space used for this array is the heap space. If JIN is used, then open up the space directly from the memory, in this way, you can save the heap space and solve the problem of insufficient heap space. Therefore, Buffer sub-Heap Storage and direct storage. The Buffer in Heap Storage is created using the allocate (int) static method, and the directly stored Buffer is created using the allocateDirect (int) static method, wrap (...) the static method is to provide an array to create a Buffer. The array provided by itself must belong to the heap space. Using the isDirect () method, you can check whether the Buffer is directly stored; otherwise, it is Heap Storage.

In the second case, Buffer in read-only mode: the basic operations of the cache are read and write. The get () method is used for read operations and the put () method is used for write operations. When a Buffer is created, it can be read or written. There is a requirement that the Buffer must be provided to other operators, but it can only be read and cannot be written. The asReadOnlyBuffer () method can be used to create a read-only Buffer. the read-only Buffer shares the same array with the original Buffer, and the shared array can save space. Therefore, in order not to allow the operator to perform write operations, the Buffer in read-only mode does not allow the operator to obtain the array. Use the isReadOnly () method to check whether the Buffer is read-only.

 

When the cache reads and writes an array unit, its operation bit is different from the actual operation bit. The arrayOffset () method can be used to obtain this deviation. The parameters and return values of the following methods are relative to the deviation.

  • Current Operation bit: position ()/position (int)
  • Operation bit: limit ()/limit (int)
  • Maximum number of Operation bits: capacity ()

After each operation, the current operation bit points to the next array unit. The cache does not allow the operation until the operation is completed. Use limit ()-position () or the remaining () method to view the remaining operation volume. Use the hasRemaining () method to check whether the operation can be performed.

You can use the position (int) and limit (int) Methods to adjust the current bitwise and the bitwise of the operation. You can also use other methods to adjust the entire bitwise.

  • Clear () method, adjust position to 0, limit to capacity, indicates to clear all data, waiting to write data.
  • In the flip () method, adjust limi to position and position to 0, indicating that the data has been written and is waiting to be read.
  • In the rewind () method, adjust position to 0, which means to re-write or re-read data.
  • The reset () method. Adjusting position to the return position indicates that the operation starts from the return position. The mark () method must be used to mark the current operation position as the return position. The return position cannot be greater than the current operation position. Otherwise, the return position is eliminated during the adjustment process.
  • In the compact () method, the unread data is copied to the start position, the position is adjusted to the remaining amount, and the limit is adjusted to capacity, which indicates that the read data is cleared, the unread data is retained, and the data is waiting to be written.

(Note: Map source in http://www.cnblogs.com/hvicen/p/6138690.html)

This figure shows the location of the limit when the Buffer is written and read. The Write mode is on the left and the Read mode is on the right. Use the flip () method to convert the write mode to the Read mode. Use the clear () and compact () Methods to convert the Read mode to the write mode. clear () clears all data. compact () only clears read data and retains unread data.

 

Use the duplicate () method to copy a Buffer with the same attribute and status. The two buffers are shared arrays.

Use the slice () method to construct a Buffer. This Buffer uses the actual operation bit of the current operation bit as the deviation, and the remaining operation volume as the maximum operation bit. In other words, slice refers to slice, which refers to splitting a Buffer from the current operation bit to the operation bit. The read and write operations of this Buffer can only be performed on this segment, the split Buffer shares an array with the original Buffer.

A data unit can be divided into multiple bytes. For example, an integer data can be divided into four bytes. When stored in an array, it can be stored from high to low, or from low to high. Using the order ()/order (ByteOrder) method, you can view and set the arrangement of a data unit. The order from low to high is ByteOrder. BIG_ENDIAN, and the order from high to low is ByteOrder. LITTLE_ENDIAN. Use the ByteOrder. nativeOrder () method to view the local data arrangement.

 

This article is original and will be updated soon!

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.