NamedCacheAdd Method (IDictionary)

ScaleOut Software NamedCache API
Efficiently adds a large collection of objects to the cache.

Namespace:  Soss.Client
Assembly:  soss_namedcache (in soss_namedcache.dll) Version: 6.2.0.0
Syntax

public void Add(
	IDictionary objects
)

Parameters

objects
Type: System.CollectionsIDictionary
An IDictionary containing objects to store in the cache with keys of type CachedObjectId, string, or byte[], and with values that are either data objects to be stored or SossObjDescriptor objects.
Exceptions

ExceptionCondition
BulkAddExceptionThrown if errors are encountered during the add operation. If the objects parameter is a collection of SossObjDescriptor elements, those elements will contain specific exceptions in their InsertException property (or null, if the associated object was added to the cache successfully) after the add operation is complete.
Remarks

The Add(IDictionary) method takes an IDictionary of objects and adds them to the named cache in the SOSS distributed cache. Keys to objects in the dictionary must be of type string, Guid, byte[], or CachedObjectId. If the SOSS cache already contains objects corresponding to keys in the dictionary then those objects will be updated in the SOSS cache. If the dictionary contains a null value for a key then the corresponding item in the SOSS cache will be removed.

The Add(IDictionary) method leverages the distributed, multithreaded architecture of ScaleOut StateServer to reduce the total time needed to add a large number of objects. This method also can improve performance several times if a small number of large objects need to be added to the SOSS cache.

By default, the Add(IDictionary) method will use the default cache policy from the DefaultCreatePolicy property when adding an object to the cache, and no metadata for the object will be set. Alternatively, each object in the IDictionary optionally can specify its own cache policy and associated metadata. To assign cache policy and metadata on an object-by-object basis, the associated value within the dictionary should contain SossObjDescriptor objects instead of the data object to be stored in the SOSS cache. In this case, the SossObjDescriptor's SossObjDescriptor's Policy and Metadata properties will be used to assign the object's policy and metadata, and Add will use this descriptor to obtain a reference to the data object. The dictionary may contain a mix of simple data objects and SossObjDescriptor objects.

Examples

using System;
using System.Collections.Generic;
using Soss.Client;

class Program
{
    const int OBJECT_COUNT = 10000;

    static void Main(string[] args)
    {
        InsertObjects();
        InsertObjectsEx();
    }

    // Add objects using the NamedCache's DefaultCreatePolicy and no metadata
    static void InsertObjects()
    {
        // set up the collection of objects to be added to the cache:
        Dictionary<string, string> objColl = new Dictionary<string, string>();
        for (int i = 0; i < OBJECT_COUNT; i++)
        {
            string cacheKey = "key" + i.ToString();
            string cachedObj = "This is an object in the cache: " + i.ToString();
            objColl.Add(cacheKey, cachedObj);
        }

        NamedCache cache = CacheFactory.GetCache("cache1");
        cache.Add(objColl);
    }

    // Add objects objects with metadata and specific policy settings:
    static void InsertObjectsEx()
    {
        // put SossObjDescriptor instances in the collection to specify metadata and cache policy:
        Dictionary<string, SossObjDescriptor> objColl = new Dictionary<string, SossObjDescriptor>();
        for (int i = 0; i < OBJECT_COUNT; i++)
        {
            string cacheKey = "key" + i.ToString();
            string cachedObj = "This is an object in the cache: " + i.ToString();

            // set up metadata to associate with the cached object:
            ObjectMetadata meta = new ObjectMetadata();
            meta.IndexCollection[0] = new IndexValue("index value" + i.ToString());

            // set up a cache policy with a timeout for the object:
            CreatePolicy policy = new CreatePolicy(TimeSpan.FromMinutes(i));

            objColl.Add(cacheKey, new SossObjDescriptor(cachedObj, policy, meta));
        }

        NamedCache cache = CacheFactory.GetCache("cache2");
        cache.Add(objColl);
    }
}
See Also

Reference