Table of Contents

Basic Usage

This example illustrates how to instantiate a Cache<TKey, TValue> instance and use it to perform basic CRUD operations against a ScaleOut in-memory data grid.

using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Scaleout.Client;

class BasicCrud
{
    static async Task CreateReadUpdateDelete()
    {
        var conn = await GridConnection.ConnectAsync("bootstrapGateways=localhost:721");

        // Create cache of US presidents, keyed by an integer:
        var cacheBuilder = new CacheBuilder<int, string>("presidents", conn);
        var cache = cacheBuilder.Build();

        // Add an object to the cache:
        var addResponse = await cache.AddAsync(6, "John Adams");
        if (addResponse.Result != ServerResult.Added)
            Console.WriteLine($"Unexpected add result: {addResponse.Result}");

        // Retrieve the object:
        var readResponse = await cache.ReadAsync(6);
        if (readResponse.Result == ServerResult.Retrieved)
            Console.WriteLine("6th US president: " + readResponse.Value);
        else
            Console.WriteLine($"Unexpected read result: {readResponse.Result}");

        // Update the object:
        var updateResponse = await cache.UpdateAsync(6, "John *Quincy* Adams");
        if (updateResponse.Result != ServerResult.Updated)
            Console.WriteLine($"Unexpected update result: {updateResponse.Result}");

        // Remove the object:
        var removeResponse = await cache.RemoveAsync(6);
        if (removeResponse.Result != ServerResult.Removed)
            Console.WriteLine($"Unexpected remove result: {removeResponse.Result}");
    }
}