Share via


Programming Model (Velocity)

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

The Microsoft project code named "Velocity" programming model is customized for the cache-aside programming pattern. This means that if your data is not present in the cache, your application, not the "Velocity" distributed cache, must reload data into the cache from the original data source. Application code uses the DataCache class, also referred to as a cache client, once it is instantiated.

Caching Strategy

Application code should be designed so that it can function without cache, and not require that cached data always be available. Because data in the cache is not persisted in a durable fashion, there is always the possibility it could go away.

The high availability feature helps guard against machine and process failures from individual cache hosts while the cluster is running. But, there may be scenarios when the entire cluster goes down. For example, if one too many lead hosts go down, the whole cluster will shut down. For more information, see Lead Hosts and Cluster Management (Velocity).

There are many other reasons that your code may encounter a cache miss: the cache item may have expired or been evicted, the cache server may have rebooted, the cache host service may have been restarted, or the cache cluster may have been accidently restarted. Regardless of the reason, your application code should be able to access the database (or other data source) if the cached object is not available.

Cache Clients

In order to store data in the cache, the GetCache method is used to return a DataCache object for you to work with in application code. Once instantiated, this DataCache object is referred to as the cache client.

Note

For performance reasons, we recommend that you minimize the number of DataCacheFactory objects created in a cache-enabled application. Store the DataCacheFactory object in a variable available to all parts of the application that use cache clients.

There are many options available to configure the behavior of the cache client. You can specify these configuration settings programmatically or with an application configuration file, or by using both approaches. For more information about cache clients and the available application configuration settings see Cache Clients and Local Cache (Velocity) and Application Configuration Settings (Velocity),.

It is possible for more than one cache client to access a single cache at the same time. Applications running on different computers can do this by instantiating a cache client that is configured to use the same cache. The following code example demonstrates this concept. Note that comments are used to identify code executing on different cache client instances.

Note

Data in the cache is not encrypted and is available to any cache client that has the appropriate configuration settings. We highly recommend that you secure the XML-based application configuration files, if used, to specify the cache client.

'Each application has a similar GetCache method call
Dim myCacheFactory As DataCacheFactory = New DataCacheFactory()
Dim catalog As DataCache = myCacheFactory.GetCache("catalog")

'One cache client saves an object to the catalog named "toy101"
Call catalog.Put("toy101", New ToyObject("toy101", "Playschool"))

'The same or different cache client retrieves the object
Dim toy As ToyObject = CType(catalog.Get("toy101"), ToyObject)

'The same or a different cache client removes the object
catalog.Remove("toy101")
//Each application has a similar GetCache method call
DataCacheFactory myCacheFactory = new DataCacheFactory();
DataCache catalog = myCacheFactory.GetCache("catalog");

//One cache client saves an object to the catalog named "toy101"
catalog.Put("toy101", new ToyObject("toy101", "Playschool"));

//The same or different cache client retrieves the object
ToyObject toy = (ToyObject)catalog.Get("toy101");

//The same or a different cache client removes the object
catalog.Remove("toy101");

See Also

Concepts

Cache Clients and Local Cache (Velocity)
Concurrency Models (Velocity)
Expiration and Eviction (Velocity)
Cache Notifications (Velocity)
Physical Model (Velocity)
Logical Model (Velocity)
Configuration Model (Velocity)

Other Resources

Cache Concepts (Velocity)