Using APIs to Manage Commerce Server Caches
The following cache operations are available in the Commerce Foundation API for managing Microsoft Commerce Server 2009 R2 caches:
Requesting a cache refresh (see Developing Code to Request a Cache Refresh)
Querying for pending cache refresh requests (see Developing Code to Query for Pending Cache Refresh Requests)
Clearing caches (see Developing Code to Clear Caches)
Developing Code to Request a Cache Refresh
The following code sample shows a request for a cache refresh:
// Request that the catalog cache be refreshed
var updateRequest = new CommerceUpdate<CommerceEntity>("CommerceCache");
updateRequest.SearchCriteria.Model.Id = "CatalogCache";
// Send the request. Assumes a method GetCurrentRequestContext()
// that returns a new instance of the CommerceRequestContext class.
OperationService.ProcessRequest(GetCurrentRequestContext(), updateRequest.ToRequest());
In the code, update the Id property with the name of the cache to be refreshed. The Id must match one of the cache IDs listed in the Commerce Server Core System Caches Table.
Commerce Server Core System Caches Table
Cache ID |
Core system |
Description |
---|---|---|
Advertising |
Marketing |
All cached advertising data, such as ads and campaigns |
AllCaches |
All |
All cached data |
AllMarketingCaches |
Marketing |
All Advertising and Discounts caches
Note:
The list of commerce caches containing Advertisements and Discounts must be configured in the CommerceDeleteOperation_CommerceCache message handler. See the sectionClearing Caches in Overview of CommerceCache Operation Sequences.
|
CatalogCache |
Catalog |
All cached catalog item data |
CommerceCaches |
Marketing and Orders |
All caches configured in the caches element of the application configuration file, such as Advertising, Discounts, Orders Configuration, Shipping Methods, Payment Methods, and Taxes |
Discounts |
Marketing |
All cached discounts data, such as promotion codes |
PaymentMethodCache |
Orders |
All cached payment methods available to shoppers |
ProfileCache |
Profile |
All cached profile data or a single profile
Note:
Refreshing this cache is not supported when the Microsoft Multi-Channel Commerce Foundation is configured to use shared commerce contexts (useSharedCommerceContexts="true").
|
ShippingManagerCache |
Orders |
All cached Shipping methods available to shoppers |
SiteTermCache |
Profiles |
All cached site term data |
Developing Code to Query for Pending Cache Refresh Requests
The code sample below shows a query for pending cache refresh requests. When you query for pending cache refresh request, you must specify a CacheStamp value as search criterion. The CacheStamp is a SQL Server timestamp assigned to a cache refresh request when the request is added for the first time or updated in a SQL Server database table. The code returns all the cache refresh requests made since the time specified in the CacheStamp property for the channel specified in the RequestContext.
// Query for all cache refresh requests with a timestamp
// value greater than that stored in lastCacheStamp.
long lastCacheStamp = 0;
var queryRequest = new CommerceQuery<CommerceEntity>("CommerceCache");
// Cache stamp is an Int64 value that is the
// equivalent of a timestamp
queryRequest.SearchCriteria.Model.Properties[“CacheStamp”] = lastCacheStamp;
// A list to hold the names of stale caches
List<string> cacheNames = new List<string>();
// Assumes a method called GetCurrentRequestContext() that
// returns an instance of the CommerceRequestContext class.
CommerceResponse response;
response = OperationService.ProcessRequest(GetCurrentRequestContext(), queryRequest.ToRequest());
CommerceQueryOperationResponse queryResponse = response.OperationResponses[0] as CommerceQueryOperationResponse;
// the cache refresh requests will be in the query response
// queryResponse.CommerceEntities collection
if (queryResponse.CommerceEntities != null && queryResponse.CommerceEntities.Count > 0)
{
foreach (CommerceEntity entity in queryResponse.CommerceEntities)
{
// Add the name of the cache to the list
// of stale caches and update our lastCacheStamp
cacheNames.Add((string)entity.Id);
lastCacheStamp = Convert.ToInt64 (entity.Properties["CacheStamp"], CultureInfo.InvariantCulture);
}
}
Developing Code to Clear Caches
The delete cache operation clears all the information contained in the cache; subsequent data requests to the Commerce Foundation refill the cache with the latest information from the database.
The code sample below shows a request to delete a cache:
// Delete the catalog cache
var deleteRequest = new CommerceDelete<CommerceEntity>(“CommerceCache”);
deleteRequest.SearchCriteria.Model.Id = “CatalogCache”;
deleteCache.DeleteOptions.ReturnDeletedCount = true;
CommerceResponse response;
response = OperationService.ProcessRequest(GetCurrentRequestContext(), deleteRequest.ToRequest());
In the code, update the Id property with the name of the cache to be deleted. The Id must match one of the cache IDs listed in the Commerce Server Core System Caches Table.
In the case of the ProfileCache, you can clear the entire profile cache or clear a single profile. In the case of a single profile, use the following format for the deleteRequest.SearchCriteria.Model.Id:
deleteRequest.SearchCriteria.Model.Id = “ProfileCache.{profileID}.ProfileType”;
For example,
deleteRequest.SearchCriteria.Model.Id = “ProfileCache.{dcc54f37-4a2b-496b-b7a7-93311867c49e}.UserObject”
See Also
Other Resources
Working with Cache Refresh (Pull or Polling Model)
About the CommerceCache Entity
Overview of CommerceCache Operation Sequences
Configuring Authorization Security for the CommerceCache Entity