Overview
Velocity is a distributed cache solution launched by Microsoft. It provides support for developing scalable, available, and high-performance applications and caches various types of data, such as CLR objects, XML, binary data, and cache servers in cluster mode. Velocity will also be integrated in. NET Framework 4.0. This article will introduce the pessimistic locking, cache item version, log record, client cache, route table, and other knowledge in Velocity.
Pessimistic locking
Velocity provides a pessimistic locking model, that is, when a cache item is processed, the data is locked and cannot be processed by other client applications. The following code provides three methods for pessimistic locking:
GetAndLock (): Get cache items and lock data;
PutAndUnlock (): Update the locked data and release the lock;
Unlock (): Release the lock.
First, let's take a look at the GetAndLock () method and lock it when obtaining the cache item. If other clients attempt to obtain the data and lock it (that is, call the GetAndLock method), it will fail without blocking; however, if the client only wants to obtain data (that is, call the Get method), the corresponding data will be returned, which can be represented in Figure 1:
Figure 1
As you can see, ClientA successfully acquires and locks the data. If ClientB wants to obtain and lock the data again, it will fail. ClientC can obtain the data.
The GetAndLock () method can be used to specify the lock expiration time, and the output parameter LockHandle will be released in the PutAndUnlock () method or Unlock (), as shown in the following code:
Cache cache = GetCurrentCache();LockHandle handle = new LockHandle();Customer item = (Customer)cache.GetAndLock("C20081117005", new TimeSpan(0, 30, 0), out handle);Customer customer = new Customer(){ ID = "C20081117005", FirstName = "Terry", LastName = "Lee", Age = 25, Email = "lhj_cauc[#AT#]163.com"};cache.PutAndUnlock(customer.ID, customer, handle, null);
Log records
Velocity also provides the logging function. We can set it in the application configuration file. It supports three methods of recording Based on the console, file, and Windows event tracking, add the configuration area in the configuration file:
<section name="fabric" type="System.Data.Fabric.Common.ConfigFile, FabricCommon" allowLocation="true" allowDefinition="Everywhere"/>
Then, you can configure it, such as setting the log record level:
<fabric> <section name="logging" path=""> <collection name="sinks" collectionType="list"> <customType className="System.Data.Fabric.Common.EventLogger,FabricCommon" sinkName="System.Data.Fabric.Common.ConsoleSink,FabricCommon" sinkParam="" defaultLevel="-1"/> <customType className="System.Data.Fabric.Common.EventLogger,FabricCommon" sinkName="System.Data.Fabric.Common.FileEventSink,FabricCommon" sinkParam="CacheClientLog" defaultLevel="1"/> <customType className="System.Data.Fabric.Common.EventLogger,FabricCommon" sinkName="System.Data.Caching.ETWSink, CacheBaseLibrary" sinkParam="" defaultLevel="-1" /> </collection> </section></fabric>
You can also set it in the code to call the two static methods CreateLogSinks and DisableLogSinks of CacheFactory, as shown in the following code:
Private Cache GetCurrentCache () {List <LogSink> sinklist = new List <LogSink> (2); LogSink fileBasedSink = new LogSink (SinkType. FILE, TraceLevel. warning, "DCache/dd-hh-mm"); LogSink consoleBasedSink = new LogSink (SinkType. CONSOLE, TraceLevel. warning); sinklist. add (fileBasedSink); sinklist. add (lelebasedsink); // enables CacheFactory. createLogSinks (sinklist); // disable CacheFactory. disableLogSinks (); Cache dCache; ServerEndPoint [] servers = new ServerEndPoint [1]; servers [0] = new ServerEndPoint ("localhost", 22233, "DistributedCacheService "); bool routingClient = true; bool localCache = false; var factory = new CacheFactory (servers, routingClient, localCache); dCache = factory. getCache ("default"); return dCache ;}
Cache item version
Velocity provides a version-based update function. When the GetCacheItem () method is used, a cache item is returned with version information, when a cache item is updated, it is added internally. In the following example, two customer applications obtain the same cache item at the same time:
ClientA
CacheItem item = cache.GetCacheItem("Customers", "C2008");
ClientB
CacheItem item = cache.GetCacheItem("Customers", "C2008");
Modify the cache items at the same time:
ClientA
((Customer)item.CacheObject).FirstName = "Huijun";
ClientB
((Customer)item.CacheObject).FirstName = "Terry";
If the ClientA submits the changes first, the version information is included when the changes are submitted. Because the version information is consistent with the internal version, the submission is successful:
ClientA
cache.Put("Customers", "C2008", item.CacheObject, item.Version);
In this case, the internal version will be added. If ClientB submits the change, it will fail because the version cannot match. 2 indicates:
Figure 2
Client Cache
Velocity also supports client caching. If the client cache is enabled, data is stored in the Client Cache when it is retrieved from the cache cluster, in this way, data will be directly retrieved from the client cache next time, which can greatly improve the efficiency, a bit like the cache. When the data in the cluster changes, Velocity will use the event notification mechanism to notify the client to cache and refresh the data, as shown in 3:
Figure 3
To enable the client cache, use the configuration file and set the IsEnabled attribute to True, as shown in the following code:
<dcacheClient deployment="routing"> <localCache isEnabled="true" sync="TTLBased" ttlValue="300" />
You can directly enable the client cache, or specify it when creating CacheFactory, as shown in the following code:
Cache dCache;ServerEndPoint[] servers = new ServerEndPoint[1];servers[0] = new ServerEndPoint("localhost", 22233, "DistributedCacheService");bool routingClient = true;bool localCache = false;var factory = new CacheFactory(servers, routingClient, localCache);dCache = factory.GetCache("default");return dCache;
Routing Client
In the cache Client, Velocity provides a Routing Client that provides better performance than the Simple Client. In the Routing Client, there is a Routing Table in the route Table, it is used to track cache objects. It is a subset of partition ing in the global cache and distributes cache operations (Put, Get, etc.) to a specified cache host. The routing client uses this route table to optimize performance. Because this table can track cache objects, physical location can be performed when a request is sent to the cache host. 4:
Figure 4
Whether to enable the routing client in the application can be determined by the developer. For example, to enable the routing client in the configuration, you can set the routing client (routing) by specifying deployment) or simple client ):
<dcacheClient deployment="routing"> <localCache isEnabled="true" sync="TTLBased" ttlValue="300" />
You can also set the parameters by using the Code as follows:
Cache dCache;ServerEndPoint[] servers = new ServerEndPoint[1];servers[0] = new ServerEndPoint("localhost", 22233, "DistributedCacheService");bool routingClient = true;bool localCache = false;var factory = new CacheFactory(servers, routingClient, localCache);dCache = factory.GetCache("default");return dCache;
Composition of Velocity
Finally, let's look at the figure to learn about the composition of Velocity. We can see that it can be divided into three parts: Client Cache, server cache, and management tools, as shown in Figure 5:
Figure 5
Summary
This article introduces the pessimistic locking, cache item version, log record, client cache, route table, and other knowledge in Velocity. So far, Microsoft's distributed cache service Velocity is introduced here in just three articles. We look forward to seeing more surprises in. NET Framework 4.0.
Related Articles:
1. Use Microsoft Distributed cache service Velocity Part 1
2. Use Microsoft Distributed cache service Velocity Part 2