Microsoft Distributed cache appfabric (velocity) Learning-Cache concept (2)

Source: Internet
Author: User

NextLearning, By the way translation, there are incorrect places welcome correction, reprint please indicate the source of http://cnblogs.com/xuf22

Ii. cache Concept

2. cache client and local cache

Use velocity cache, ApplicationProgramDatacache is used to store cached objects. This object is the cache client, which provides an application with a reference to the name cache of the cache cluster service. This object is created by the getcache method of the datacachefacory object.

For performance considerations, we recommend that you create as few datachefactory objects as possible in the application and create a global variable in the application so that the cache client can be used anywhere-in single-piece mode.

There are two types of cache clients: routing clients and common clients. Both cache clients can store the cache locally. The cache stored locally is called local cache. The storage options of the cache client can be specified through the application configuration file.

2.1 security considerations

The cache client should be used in applications on servers running data centers, within the enterprise firewall. The communication between the cache service and the cache host is not encrypted and is vulnerable to "sniffing" and "replay" attacks.

We strongly recommend that you use an encrypted xml configuration file to specify the cache client.

2.2 client type

There are two applications that use cache: routing and simple)

Routing Client)

The performance of the routing client is higher than that of the simple client because it saves a route table on the cached boot host (lead hots. This route table records and uniquely identifies the addresses of objects on All cached hosts in the cache cluster. The routing client uses the route table to directly find the physical address of the object.

Simple Client)

If possible, try to use the routing client. However, in the network topology, some clients may only access some hosts in the cache service cluster, simple cleint is used as the data source of distributed cache.

To work with limited network communication, the simple client is designed to access only one host in the cache cluster. It does not have the routing selection function and cannot track the location of objects. To obtain an object in the cache, it usually needs to traverse all hosts.

Local Cache

The cache host stores serialized objects. To use this object on the client, deserialization is required. To improve the efficiency of object retrieval, local cache can be enabled. Both the routing client and common client can use the local cache.

When the local cache is enabled, the object is saved to the local computer with the copied text. When the client application extracts the object from the cache host, it checks whether the local cache already exists. If the object already exists in the local cache, it is taken from the local cache directly.

The local cache has two expiration policies: timeout and notification expiration.

Object consistency)

Local cache objects and cache client objects are in the same process space. Only datacache is a reference method to obtain these objects. Thread Synchronization may occur in multi-threaded applications.

To avoid potential multithreading problems, multithreading applications should generate a copy of an object when using cached objects, or take other methods to avoid thread concurrency issues. Another method is to create a datacache object for each thread.

3 concurrency

Velocity allows any cache client with network permissions and related configurations to publicly access the cached data, thus facing the challenge of security and concurrency.

To reduce security issues, all cache clients, cache services, and primary data source servers should be in the same domain and in the firewall. This is a highly recommended method.

To solve concurrency problems, velocity provides open concurrency and concurrent concurrency.

3.1 open concurrency model (Optimistic Concurrency Model)

In the case of open concurrency, the object will not be locked when it is updated. when accessing the cache, the cached object will be read first, and the cached version will be checked when it is updated. If the local cache object has expired, the cache update fails.

For example, two cache clients (cacheclienta and cacheclientb) must update the same name in both independent applicationsThe cache of radioinventory.

Time Zero (t0): two objects fetch the same object at the same time

Each of the two cache clients provides a datacheitem class and extracts the same cached object. The two datacacheitem instances must be updated.

 T0: retrieve objects // Cacheclienta pulls the FM radio inventory from Cache Datachefactory clientacachefactory = New Datacachefactory (); datacache cacheclienta = clientachefactory. getcache (" Catalog "); Datacheitem radioinventorya = cacheclienta. getcacheitem (" Radioinventory "," Electronics "); // Cacheclientb pulls the same FM radio inventory from Cache Datachefactory clientbcachefactory = New Datacachefactory (); datacache cacheclientb = clientbcachefactory. getcache ("Catalog "); Datacheitem radioinventoryb = cacheclienta. getcacheitem (" Radioinventory "," Electronics ");

Time one (T1 ):CacheclientaObject updated

CacheclientaRadioinventory update,Add the version number of radioinventory.In cacheclientbThe radioinventory object has expired.

// At time t1, cacheclienta updates the FM radio inventoryIntNewradioinventorya = 155; cacheclienta. Put ("Radioinventory", Newradioinventorya, radioinventorya. version ,"Electronics");

Time two: the second update fails

Cacheclientb tries to modifyRadioinventory is a new value. To preventUpdate overwrite of cacheclienta,Update of cacheclientb will fail.

// Later, at time t2, cacheclientb tries// Update the FM radio inventory, triggers exception errca001IntNewradioinventoryb= 130; cacheclientb. Put ("Radioinventory", Newradioinventoryb, radioinventoryb. version ,"Electronics");

Concurrent write

When the client extracts an object, it clearly indicates that the object has been removed and is being updated. All requests to the cached object are rejected until the PIN is released. When the cache object is locked, a handle is returned for the read operation.

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.