Regions and Tagging for Azure Managed Cache Service

Important

Microsoft recommends all new developments use Azure Redis Cache. For current documentation and guidance on choosing an Azure Cache offering, see Which Azure Cache offering is right for me?

Microsoft Azure Cache supports the creation and use of user-defined regions. A region is a subgroup for cached items. Regions also support the annotation of cached items with additional descriptive strings called tags. Regions support the ability to perform search operations on any tagged items in that region.

Region Considerations

Regions are optional; if you want to use them, explicitly create them in code with the CreateRegion method. After a region is created, you can add tags to objects that are inserted into the region. There are overloads on the Add and Put methods which accept a region name. To search a region for objects with a given tag, use the methods GetObjectsByTag, GetObjectsByAnyTag, and GetObjectsByAllTags.

In Managed Cache Service, the memory of the cache is distributed throughout different areas of the service. Because of architectural requirements of the search functionality, objects in a region are located in a single area of memory in the cache service. If high availability is enabled, a backup of the region exists in a different area of memory in the cache service. This behavior differs from the scenario where regions are not used. In that scenario, new items in the cache are distributed throughout all areas of memory of the cache service.

Warning

Since all items in a region are required to be located in the same area of memory of the cache service, it is possible that items could be evicted from the cache even if there appears to be sufficient remaining memory. Potentially use multiple smaller regions to realize the benefits of regions and tagging.

Examples

The following example shows how to create a region named Test.

DataCache cache = new DataCache("default");
cache.CreateRegion("Test");

The following example shows how to add three objects to the Test region with tags.

List<DataCacheTag> tagList1 = new List<DataCacheTag>()
{ 
    new DataCacheTag("Tag1"),
    new DataCacheTag("Tag2")
};
List<DataCacheTag> tagList2 = new List<DataCacheTag>()
{
    new DataCacheTag("Tag3")
};
cache.Put("Key1", "Value1", tagList1, "Test");
cache.Put("Key2", "Value2", tagList1, "Test");
cache.Put("Key3", "Value3", tagList2, "Test");

The following example shows how to search the Test region for all objects that have the Tag1 tag.

foreach (KeyValuePair<string, object> result in 
    cache.GetObjectsByTag(new DataCacheTag("Tag1"), "Test"))
{
    string keyValue = result.Key;
    string objectValue = result.Value.ToString();
}

See Also

Other Resources

Azure Managed Cache Service Features