Dependencies
Establishing a Dependency Relationship
This example illustrates how to establish a dependency relationship between a parent object and two child objects using a CreatePolicy that contains a list of DependencySpec entries. Removing the parent object causes all of its children (and their children, if any) to be removed.
async Task AddWithDependency(Cache<string, string> cache)
{
// Add parent object:
await cache.AddAsync("Primates", "large brains, stereoscopic vision");
// Create dependency specification for use with child objects:
var policyBuilder = cache.GetPolicyBuilder();
policyBuilder.AddDependency(cache, "Primates");
// Add dependent child objects:
var policy = policyBuilder.Build();
await cache.AddAsync("Strepsirrhini", "wet-nosed primates", policy);
await cache.AddAsync("Haplorhini", "dry-nosed primates", policy);
// Removing/updating a parent causes children to expire:
await cache.RemoveAsync("Primates");
var response = await cache.ReadAsync("Strepsirrhini");
Console.WriteLine(response.Result);
// output: NotFound
}
Retrieving Dependency Information
This example illustrates how to use the GetMetadataAsync(TKey, CancellationToken) method to retrieve dependency information about an existing object that resides in the ScaleOut service.
async Task GetDependencyMetadata(Cache<string, string> cache)
{
// Add 2 parent objects to be associated with a child:
await cache.AddAsync("Parent1 key", "Parent1 payload");
await cache.AddAsync("Parent2 key", "Parent2 payload");
// Create dependency specification for use with child object:
var policyBuilder = cache.GetPolicyBuilder();
policyBuilder.AddDependency(cache, "Parent1 key");
policyBuilder.AddDependency(cache, "Parent2 key");
// Add dependent child object to cache:
var policy = policyBuilder.Build();
await cache.AddAsync("Child key", "Child payload", policy);
// Retrieve metadata from the server to get dependency information
// about an existing child object:
var metadataResponse = await cache.GetMetadataAsync("Child key");
var metadata = metadataResponse.Value;
// Use DependencySpec.ToKey() to decode parent specs back to their original string type:
var decodedParentKeys = metadata.Parents.Select(depSpec => depSpec.ToKey(cache));
foreach (var dpk in decodedParentKeys)
Console.WriteLine(dpk);
// output:
// Parent1 key
// Parent2 key
}
Dependency Notifications
An application can be notified of the removal of an object due to a dependency relationship by supplying a callback to the ServiceEvents.SetExpirationHandlerAsync method See Expiration Events for details.