How to get all objects inside a NameCache?

markw

Administrator
Staff member
#2
You can use the Values property or perform a query to get all the objects or keys in a NamedCache. However, please be aware that these approaches will perform a round trip to the ScaleOut service and invoke a parallel query on the distributed store, which involves all hosts. These are expensive operations that should only be done on a rare/occasional basis.

C#:
// reference soss_namedcache.dll and soss_svcdotnet.dll

using Soss.Client;
using System;

class Program
{
    static void Main(string[] args)
    {
        var nc = CacheFactory.GetCache("My Named Cache");

        // Add 10 objects (Guid values with string keys).
        for (int i = 0; i < 10; i++)
            nc.Insert(i.ToString(), Guid.NewGuid(), nc.DefaultCreatePolicy,
                      updateIfExists: true, lockAfterInsert: false);

        // Use NamedCache.Values property to get all 10 values.
        foreach (object obj in nc.Values)
        {
            Console.WriteLine(obj); // prints Guid Value
        }

        // Alternatively, use Query() to get back all keys in the cache
        // and then retrieve the object values manually.
        foreach (CachedObjectId key in nc.Query(filter: null))
        {
            Console.WriteLine(nc.Get(key)); // prints Guid Value
        }
    }
}
For complex types, you can use NamedCache.QueryObjects<T>() to access a LINQ IQueryable if you need to do things like use a Where clause to filter results on the server.
 
#3
You can use the Values property or perform a query to get all the objects or keys in a NamedCache. However, please be aware that these approaches will perform a round trip to the ScaleOut service and invoke a parallel query on the distributed store, which involves all hosts. These are expensive operations that should only be done on a rare/occasional basis.

C#:
// reference soss_namedcache.dll and soss_svcdotnet.dll

using Soss.Client;
using System;

class Program
{
    static void Main(string[] args)
    {
        var nc = CacheFactory.GetCache("My Named Cache");

        // Add 10 objects (Guid values with string keys).
        for (int i = 0; i < 10; i++)
            nc.Insert(i.ToString(), Guid.NewGuid(), nc.DefaultCreatePolicy,
                      updateIfExists: true, lockAfterInsert: false);

        // Use NamedCache.Values property to get all 10 values.
        foreach (object obj in nc.Values)
        {
            Console.WriteLine(obj); // prints Guid Value
        }

        // Alternatively, use Query() to get back all keys in the cache
        // and then retrieve the object values manually.
        foreach (CachedObjectId key in nc.Query(filter: null))
        {
            Console.WriteLine(nc.Get(key)); // prints Guid Value
        }
    }
}
For complex types, you can use NamedCache.QueryObjects<T>() to access a LINQ IQueryable if you need to do things like use a Where clause to filter results on the server.
Thank you Markw, your comment are a lot helping us.
 
Top