How to: Enable Local Cache (Code) (Velocity)
To programmatically enable local cache when creating your cache client, you must make sure the localCache parameter in the DataCacheFactory class constructor is equal to true. For more information about the application configuration settings, see Application Configuration Settings (Velocity).
Note
These procedures assume that you have already prepared your development environment and set references to the "Velocity" assemblies, and so on. For more information, see How to: Prepare the Development Environment (Velocity)
To create a cache client that has local cache enabled
Create an array of DataCacheServerEndPoint objects to specify the cache hosts for the client.
Configure your cache hosts by assigning the cache host array from the previous step to the servers parameter of the DataCacheFactory constructor. 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.
Configure your cache client type by assigning a true or false value to the routingClient parameter of the DataCacheFactory constructor. Use the true value for a routing client, or a false value for a simple client.
Enable local cache by assigning a true value to the localCache parameter of the DataCacheFactory constructor.
(optional) Configure your cache client for cache notifications by using the DataCacheFactory constructor with the following additional parameters.
syncPolicy: Use the DataCacheLocalCacheSyncPolicy enumeration to choose how locally cached objects are invalidated. Use TimeoutBased to indicate that a time-out value should be used or NotificationBased to indicate that cache notifications will be used. For more information, see Cache Clients and Local Cache (Velocity).
localCacheTimeout: Use this parameter to specify the number of seconds that an object will remain in local cache before it is invalidated. This parameter is ignored if syncPolicy is set to NotificationBased.
pollInterval: Use this parameter to specify the interval of frequency, in seconds, that the cache client will check with the cache cluster for cache notifications. The default value is 300 seconds. Note: Local cache is not required for cache notifications. For more information, see Cache Notifications (Velocity).
Use the GetCache method to obtain an instance of the routing client.
Posted in: C# and .NET| Tags: Velocity CTP CTP 3 .NET 4.0 Cache Microsoft Application Configuration Code client parameter enable local localcacheHow to: Get Started with a Simple Client (Code) (Velocity)
When you programmatically configure your cache client, the configuration settings are passed to the DataCacheFactory class constructor.
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.
The cache client type is defined by the routingClient parameter in the DataCacheFactory class constructor. For a simple client, the routingClient parameter must be false. For more information about the application configuration settings, see Application Configuration Settings (Velocity).
Note
These procedures assume that you have already prepared your development environment and set references to the "Velocity" assemblies, and so on. For more information, see How to: Prepare the Development Environment (Velocity)
To configure a simple client programmatically
Create an array of DataCacheServerEndpoint objects to specify the cache hosts for the client.
Configure your cache hosts by assigning the cache host array from the previous step to the servers parameter of the DataCacheFactory constructor.
Select a simple client type by assigning a false value to the routingClient parameter of the DataCacheFactory constructor.
Configure local cache by assigning a true or false value to the localCache parameter of the DataCacheFactory constructor. Use the true value to enable local cache or a false value to disable local cache.
Use the GetCache method to obtain an instance of the routing client.
Posted in: C# and .NET| Tags: Velocity CTP CTP 3 .NET 4.0 Cache Microsoft Configuration Code client class datacachefactory constructor configure simpleHow to: Get Started with a Routing Client (Code) (Velocity)
When you programmatically configure your cache client, the configuration settings are passed to the DataCacheFactory class constructor.
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.
The cache client type is defined by the routingClient parameter in the DataCacheFactory class constructor. For a routing client, the routingClient parameter must be true. For more information about the application configuration settings, see Application Configuration Settings (Velocity).
Note
These procedures assume that you have already prepared your development environment and set references to the "Velocity" assemblies, and so on. For more information, see How to: Prepare the Development Environment (Velocity)
To configure a routing client programmatically
Create an array of DataCacheServerEndpoint objects to specify the cache hosts for the client.
Configure your cache hosts by assigning the cache host array from the previous step to the servers parameter of the DataCacheFactory constructor.
Select a routing client type by assigning a true value to the routingClient parameter of the DataCacheFactory constructor.
Configure local cache by assigning a true or false value to the localCache parameter of the DataCacheFactory constructor. Use the true value to enable local cache, or a false value to disable local cache.
Use the GetCache method to obtain an instance of the routing client.
Posted in: C# and .NET| Tags: Velocity CTP CTP 3 .NET 4.0 Cache Microsoft Configuration Code client class datacachefactory constructor configure routingHow to: Remove an Object from Cache (Velocity)
The following examples show the ways you can remove objects from the cache. These procedures assume that you have already set up your cache cluster and have prepared your development environment to write cache-enabled applications. For more information about how to do this, see Installation and Deployment (Velocity) and How to: Prepare the Development Environment (Velocity).
For more details about the methods that are used in these examples, see these class library topics:
Remove
Item
Data in the cache is not encrypted and is available to any cache client with the appropriate configuration settings. We highly recommend that you secure the XML-based application configuration files, if used, to specify the cache client.
To remove an object from the cache
Make sure that the using statement (Imports in Visual Basic) is at the top of your application code to reference the Microsoft.Data.Caching namespace.
Create a DataCacheFactory object that is accessible to all parts of the application that need a cache client. We recommend reusing the same DataCacheFactory object to conserve memory and optimize performance.
Use the DataCacheFactory object to create a DataCache object (also referred to as the cache client) if you have not already done this. In the following examples, the DataCacheFactory instance is called CacheFactory1.
After you have the DataCache object, the Remove method or Item property may be used to remove an object from cache.
Example
The following example uses the Remove method to remove an object from cache.
Note
There are many other parameters available for this method. See the Remove class library for more information.
C# Copy Code
//remove object in cache using key "Key0"
myCache.Remove("Key0");
The following example uses the Item property that has array notation to remove an object from cache.
C# Copy Code
//remove object in cache using array notation
myCache["Key0"] = null;
How to: Get an Object from Cache (Velocity)
The following examples show the ways you can retrieve objects from the cache. These procedures assume that you have already set up your cache cluster and have prepared your development environment to write cache-enabled applications. For more information about how to do this, see Installation and Deployment (Velocity) and How to: Prepare the Development Environment (Velocity).
For more details about the methods that are used in these examples, see these class library topics:
Get
Item
Data in the cache is not encrypted and is available to any cache client with the appropriate configuration settings. We highly recommend that you secure the XML-based application configuration files, if used, to specify the cache client.
To get an object from the cache
Make sure that the using statement (Imports in Visual Basic) is at the top of your application code to reference the Microsoft.Data.Caching namespace.
Create a DataCacheFactory object that is accessible to all parts of the application that need a cache client. We recommend reusing the same DataCacheFactory object to conserve memory and optimize performance.
Use the DataCacheFactory object to create a DataCache object (also referred to as the cache client) if you have not already done this. In the following examples, the DataCacheFactory instance is called CacheFactory1.
After you have the DataCache object, the Get method or Item property may be used to retrieve an object from cache.
Posted in: C# and .NET| Tags: Velocity CTP CTP 3 .NET 4.0 MicrosoftHow to: Update an Object in Cache (Velocity)
To update an object in cache
Make sure that the using statement (Imports in Visual Basic) is at the top of your application code to reference the Microsoft.Data.Caching namespace.
Create a DataCacheFactory object that is accessible to all parts of the application that need a cache client. We recommend reusing the same DataCacheFactory object to conserve memory and optimize performance.
Use the DataCacheFactory object to create a DataCache object (also referred to as the cache client) if you have not already done this. In the following examples, the DataCacheFactory instance is called CacheFactory1.
After you have the DataCache object, the Put method or Item property may be used to update an object in cache.
Example
The following example uses the Put method to add an object to cache. If the object is not present when this method is called, it will be added to the cache. If the object is already present, it will be replaced.
Note
There are many other parameters available for this method. See the Put class library for more information.
C# Copy Code
//add or replace object in cache using key "Key0"
myCache.Put("Key0", "object replaced or added using Key0");
The following example uses the Item property that has array notation to add an object to cache. If the object is not present when this method is called, it will be added to the cache. If the object is already present, it will be replaced.
C# Copy Code
//add or replace object in cache using array notation
myCache["Key0"] = "object replaced or added using Key0";
How to: Add an Object to Cache (Velocity)
The following examples show the ways you can add objects to the cache. These procedures assume that you have already set up your cache cluster and have prepared your development environment to write cache-enabled applications. For more information about how to do this, see Installation and Deployment (Velocity) and How to: Prepare the Development Environment (Velocity).
For more details about the methods that are used in these examples, follow these links to view the class library topics:
Add
Put
Item
Data in the cache is not encrypted and is available to any cache client with the appropriate configuration settings. We highly recommend that you secure the XML-based application configuration files, if used, to specify the cache client.
To add an object to cache
Make sure that the using statement (Imports in Visual Basic) is at the top of your application code to reference the Microsoft.Data.Caching namespace.
Create a DataCacheFactory object that is accessible to all parts of the application that need a cache client. We recommend reusing the same DataCacheFactory object to conserve memory and optimize performance.
Use the DataCacheFactory object to create a DataCache object (also referred to as the cache client) if you have not already done this. In the following examples, the DataCacheFactory instance is called CacheFactory1.
After you have the DataCache object, the Add method, Put method, or Item property may be called to add an object to the cache.
Example
The following example demonstrates how you can use the Add method to add an object to cache. You will get an exception if an object has already been cached by the same key (the first parameter).
Note
There are many other parameters available for this method. See the Add class library for more information.
C# Copy Code
//add object to cache with key "Key0"
myCache.Add("Key0", "object added with Key0");
How to: Prepare the Development Environment (Velocity)
Preparing Your Project for "Velocity"
The assemblies required for developing cache-enabled applications are located in the "Velocity" installation folder. To develop applications that use these assemblies, the following three steps must be performed to prepare your development environment:
Copy the assemblies to your development workstation.
Set references to the assemblies in your Visual Studio project.
Configure the cache client by using an XML-based application configuration file, or programmatically with code.
To copy assemblies
Copy the assemblies to the development workstation from the installation folder of the cache server. This location was selected during installation of the "Velocity" cache host service.
Copy the following assemblies from the cache server to the same folder on the development workstation: CacheBaseLibrary.dll, ClientLibrary.dll, FabricCommon.dll, and CASBase.dll.
Note
It is important that your application or development environment uses the same assemblies as the cache servers. During any upgrade of the distributed cache system, make sure that all cache clients using that system have the same versions of assemblies. Check this by comparing the Product version of the cache client's ClientLibrary.dll file with the Product version of the cache server's ServiceLibrary.dll file that is located in the installation folder.
Once the assemblies have been copied to the same folder in your development workstation, set references to them by following these steps.
To set references
After creating a new project in Visual Studio, from the Solution Explorer, right-click References and then select Add Reference.
Select the Browse tab, and then locate the copies of the assemblies from step 1 and select CacheBaseLibrary.dll and ClientLibrary.dll. To select more than one file, hold down Ctrl as you select them with the mouse. When all the files have been selected, click OK. This adds Microsoft.Data.Caching assemblies to the reference list for your application.
(optional) Add the using statement (Imports in Visual Basic) at the top of the code file to reference the Microsoft.Data.Caching namespace. This prevents having to add the "Microsoft.Data.Caching" prefix to each "Velocity" class when you program cache operations.
In order for the "Velocity" assemblies to communicate with the cache cluster, the cache client must be configured to run as either a simple client or a routing client.
To configure the cache client
Decide whether you want a routing or simple client. For best performance, use a routing client if your application (and development workstation) will have network connectivity to all cache hosts in the cluster. For more about the cache client types, see Cache Clients and Local Cache (Velocity).
Configure your cache client by using an application configuration file or programmatically. For examples about how to do this, see How to: Get Started with a Routing Client (XML) (Velocity) and How to: Get Started with a Routing Client (Code) (Velocity).
Posted in: Software Programming C# and .NET| Tags: Velocity CTP CTP 3 .NET 4.0 Microsoft