// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using StackExchange.Redis; namespace Cache.Redis.Functional { public class Program { private static void Test(IDatabase cache, IServer srv) { // basic put, get and remove cache.StringSet("v", 123); Console.WriteLine(cache.StringGet("v")); cache.KeyDelete("v"); Console.WriteLine(cache.StringGet("v")); cache.StringSet("v", 123); Console.WriteLine(cache.StringGet("v")); // persistence int? counter = (int?)cache.StringGet("counter"); if (counter == null) { counter = 0; } counter++; Console.WriteLine(counter); cache.StringSet("counter", counter); // list all foreach(RedisKey key in srv.Keys(pattern: "*")) { Console.WriteLine("{0} = {1}", key, cache.StringGet(key)); } // listener not supported } private const int FUNCTIONAL_CACHE = 0; public static void Main(string[] args) { ConnectionMultiplexer con = ConnectionMultiplexer.Connect("localhost:6379"); IDatabase cache = con.GetDatabase(FUNCTIONAL_CACHE); IServer srv = con.GetServer("localhost", 6379, FUNCTIONAL_CACHE); Test(cache, srv); } } }