// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Collections.Generic; using Apache.Ignite.Core; using Apache.Ignite.Core.Client; using Apache.Ignite.Core.Client.Cache; using Apache.Ignite.Core.Cache.Query; namespace Cache.IgniteSql.Functional { public class Program { public delegate void Processor(IList row); private static int? Get(ICacheClient cache, String key) { SqlFieldsQuery sfq = new SqlFieldsQuery("SELECT cvalue FROM functional_cache WHERE ckey = ?"); sfq.Schema = "PUBLIC"; sfq.Arguments = new object[] { key }; IList> res = cache.Query(sfq).GetAll(); if(res.Count > 0) { return (int?)res[0][0]; } else { return null; } } private static void Put(ICacheClient cache, String key, int? value) { SqlFieldsQuery sfq = new SqlFieldsQuery("MERGE INTO functional_cache(ckey,cvalue) VALUES(?,?)"); sfq.Schema = "PUBLIC"; sfq.Arguments = new object[] { key, value }; cache.Query(sfq).GetAll(); } private static void Remove(ICacheClient cache, String key) { SqlFieldsQuery sfq = new SqlFieldsQuery("DELETE FROM functional_cache WHERE ckey = ?"); sfq.Schema = "PUBLIC"; sfq.Arguments = new object[] { key }; cache.Query(sfq).GetAll(); } private static void ListAll(ICacheClient cache, Processor f) { SqlFieldsQuery sfq = new SqlFieldsQuery("SELECT ckey,cvalue FROM functional_cache"); sfq.Schema = "PUBLIC"; sfq.Arguments = new object[] { }; foreach(IList row in cache.Query(sfq).GetAll()) { f(row); } } private static void Test(ICacheClient cache) { // basic put, get and remove Put(cache, "v", 123); Console.WriteLine(Get(cache, "v")); Remove(cache, "v"); Console.WriteLine(Get(cache, "v")); Put(cache, "v", 123); Console.WriteLine(Get(cache, "v")); // persistence int? counter = Get(cache, "counter"); if(counter == null) { counter = 0; } counter++; Console.WriteLine(counter); Put(cache, "counter", counter); // list all ListAll(cache, row => Console.WriteLine("{0} = {1}", row[0], row[1])); // listener not supported using thin } public static void Main(string[] args) { IgniteClientConfiguration cfg = new IgniteClientConfiguration { Endpoints = new List { "localhost:10800" } }; using (IIgniteClient cli = Ignition.StartClient(cfg)) { ICacheClient cache = cli.GetOrCreateCache("functional_cache"); SqlFieldsQuery sfq = new SqlFieldsQuery("CREATE TABLE IF NOT EXISTS functional_cache (ckey VARCHAR(16) NOT NULL, cvalue INTEGER NOT NULL, PRIMARY KEY(ckey))"); sfq.Schema = "PUBLIC"; cache.Query(sfq).GetAll(); Test(cache); } } } }