Model-Oriented Software Architecture 3-resource management model Reading Notes (5)-caching Model

Source: Internet
Author: User

3.1 caching Mode

The Caching mode describes how to avoid expensive re-acquisition of resources by not releasing resources immediately after resources are used up. Resources maintain their identifiers and keep them in a memory that can be accessed quickly. They can be reused to avoid re-acquisition.

 

1. Problem

Unnecessary overhead is incurred for repeated acquisition, initialization, and release of the same resource. When one or more components of the system access the same resource, repeated acquisition and initialization results in overhead of the CPU cycle and overall system performance. The overhead of frequently used resources should be reduced to improve performance. To solve this problem, pay attention to the following points:

1) performance ). The overhead for repeatedly obtaining, initializing, and releasing resources must be minimized.

2) complexity ). The solution should not make operations on obtaining and releasing resources more complex and troublesome. In addition, the solution should not add unnecessary indirect layers for resource access.

3) availability ). The solution should also make resources accessible when the resource provider is temporarily unavailable.

4) scalability ). The solution should be scalable for the number of resources.

 

2. Solution

Currently, resources are stored in a cache-based access buffer. When a resource is subsequently accessed, the resource is obtained and returned through the cache, instead of being retrieved from the resource provider (such as the resource management operating system. The cache identifies resources through resource identifiers (such as pointers, references, and primary keys.

Keep frequently accessed resources and do not release them, which avoids re-obtaining and releasing resource overhead. Using Cache simplifies the management of components that access resources.

When resources in the cache are no longer needed, they are released. The implementation of cache determines how to remove unnecessary resources. This row is under policy control.

 

3. Structure

Resource users use resources.

A resource is an entity (such as a connection ).

The resource cache caches resources released by resource users.

Resource providers possess and manage multiple types of resources.

 

4. Implementation

1) Select a resource. Select the resources that can benefit from the cache. These resources are usually expensive to obtain and frequently used. Cache is often introduced as an optimization method after performance bottlenecks are discovered.

In a distributed system, there are two types of caches: Client Cache and server cache. The client cache is effective for saving bandwidth and saving time for repeatedly transferring data from the server to the client. The server cache helps solve the problem of repeatedly obtaining and releasing the same resources of the server due to multiple customer requests.

2) determines the cache interface. If a resource is to be released and directly retrieved from the cache by the resource user, a proper interface must be designed. This interface requires the release () and acquire () methods.

Public Interface Cache {

Public Void Release (resource Resource );

PublicResource acquire (identity ID );
}

The preceding interface depends on an independent resource ID. But not all resources have independent identifiers. In some cases, the resource identifier may need to be calculated from the resource attribute.

The release () method is called when a resource user releases the resource to the cache instead of to the resource provider.

3) Implement cache. The acquire () and release () methods in the cache interface provide the main functions of caching.

Public Class Cacheimpl Implements Cache {

Public VoidRelease (resource Resource ){
String id = resource. GETID ();
Map. Put (ID, resource );
}

//....

Hashmap map;
}

The release () method adds the resource to the map, so that the resource can be found by calling acquire () later. To achieve optimization, we recommend that you use a hash table because the hash table can be searched for almost a constant time complexity.

The acquire () method in the cache implementation should be responsible for searching resources in the table based on the identity. Another variation is that when a resource fails to be obtained from the cache, it means that the resource to be identified is not found, and the cache itself can get the resource from the resource provider. Public Class Cacheimpl Implements Cache {

Public resource acquire (identity ID) {
resource = map. get (ID);
If (resource = null )
throw New resourcenotfound ();
return resource;

}< BR >}4) Determine how to integrate the cache (optional ). If you must transparently integrate the cache, you can use interceptor or cache proxy. The introduction of interceptor or cache proxy makes the operation transparent, reducing the complexity of explicitly releasing resources to the cache and re-obtaining resources from the cache. 5) Decide to clear the policy. Resources stored in the cache occupy storage space. If it is not used for a long time, it becomes inefficient to retain these resources. Therefore, you should use the Evictor mode to delete resources that are no longer needed. 6) ensure consistency. Many resources have associated statuses and must be correctly initialized at Resource Creation. In addition, when a resource is accessed by a write operation, it must be consistent between the original resource and the cached resources of the original image. 5. Conclusion advantages: 1) performance. Quick access to frequently-used resources is a significant benefit of caching. Unlike the pooling mode, the cache ensures that resources maintain their identities. Therefore, when the same resource needs to be accessed again, the resource does not need to be obtained from other places, and it is already there. 2) scalability. Avoiding resource acquisition and release is a "hidden" benefit of the cache. Essentially, cache is implemented by storing frequently accessed resources. Therefore, like the pooling mode, the caching mode helps avoid the overhead of obtaining and releasing resources. This is especially beneficial for frequent use, thus improving scalability. 3) complexity. The cache ensures that the complexity of obtaining and releasing resources from the user's perspective does not increase, although additional steps are required to check whether resources are available in the cache. 4) availability. Cache resources increase resource availability when the resource provider is temporarily unavailable because the cached resources are still available. 5) stability. Because the caching mode reduces the amount of operations to release and re-obtain resources, it reduces the possibility of memory fragmentation and brings better system stability, which is similar to the pooling mode. Disadvantages: 1) Synchronization complexity. Depending on the type of the resource, the complexity will increase, because it is necessary to ensure the consistency of the status of the cached resources and the original data represented by the cached resources. This complexity has become more significant in the cluster environment and makes this mode useless in some extreme situations. 2) persistence. Changes to cached resources may be lost when the system crashes. However, if synchronous cache is used, this problem can be avoided. 3) space overhead. The system's runtime time-space overhead will increase, because it is very likely that unused resources are also cached. However, if you use the Evictor mode, you can minimize the number of unused cached resources.

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.