Out-of-process cache injection using Redis in windows azure

I am working on a webpage that displays a table from a database available in my cloudy blue. To reduce the number of calls directly to the database to improve performance, I would like to create a cache for the page. I currently store a cache in memory (in-process) for reading tables. Now I would like to make the cache outside the process, which should be updated from the moment write was created , which means inserts or updates (because after the value is updated or added, the cache in memory will be more invalid).

I was recommended by Redis and, in particular, Book Sleeve, my question is where can I find code samples to help me figure out how to start collecting the cache outside the process with it and combine it in my current project.

Thank you in advance

+6
source share
1 answer

If you want it purely because of the process, then it's quite simple - something like the following, but noting that BookSleeve is intended for sharing: it is completely thread safe and works as a multiplexer - t create / delete them for each call. Note also that in this context, I assume that you will handle serialization separately, so I just expose the byte[] API:

 class MyCache : IDisposable { public void Dispose() { var tmp = conn; conn = null; if (tmp != null) { tmp.Close(true); tmp.Dispose(); } } private RedisConnection conn; private readonly int db; public MyCache(string configuration = "127.0.0.1:6379", int db = 0) { conn = ConnectionUtils.Connect(configuration); this.db = db; if (conn == null) throw new ArgumentException("It was not possible to connect to redis", "configuration"); } public byte[] Get(string key) { return conn.Wait(conn.Strings.Get(db, key)); } public void Set(string key, byte[] value, int timeoutSeconds = 60) { conn.Strings.Set(db, key, value, timeoutSeconds); } } 

What is interesting if you need a two-level cache, i.e. using local memory and cache outside the process, since now you need cache invalidation. Pub / sub makes this convenient - the following shows it. This may not be obvious, but it will make far fewer redis calls (you can use monitor to see this) - since most requests are handled by the local cache.

 using BookSleeve; using System; using System.Runtime.Caching; using System.Text; using System.Threading; class MyCache : IDisposable { public void Dispose() { var tmp0 = conn; conn = null; if (tmp0 != null) { tmp0.Close(true); tmp0.Dispose(); } var tmp1 = localCache; localCache = null; if (tmp1 != null) tmp1.Dispose(); var tmp2 = sub; sub = null; if (tmp2 != null) { tmp2.Close(true); tmp2.Dispose(); } } private RedisSubscriberConnection sub; private RedisConnection conn; private readonly int db; private MemoryCache localCache; private readonly string cacheInvalidationChannel; public MyCache(string configuration = "127.0.0.1:6379", int db = 0) { conn = ConnectionUtils.Connect(configuration); this.db = db; localCache = new MemoryCache("local:" + db.ToString()); if (conn == null) throw new ArgumentException("It was not possible to connect to redis", "configuration"); sub = conn.GetOpenSubscriberChannel(); cacheInvalidationChannel = db.ToString() + ":inval"; // note that pub/sub is server-wide; use // a channel per DB here sub.Subscribe(cacheInvalidationChannel, Invalidate); } private void Invalidate(string channel, byte[] payload) { string key = Encoding.UTF8.GetString(payload); var tmp = localCache; if (tmp != null) tmp.Remove(key); } private static readonly object nix = new object(); public byte[] Get(string key) { // try local, noting the "nix" sentinel value object found = localCache[key]; if (found != null) { return found == nix ? null : (byte[])found; } // fetch and store locally byte[] blob = conn.Wait(conn.Strings.Get(db, key)); localCache[key] = blob ?? nix; return blob; } public void Set(string key, byte[] value, int timeoutSeconds = 60, bool broadcastInvalidation = true) { localCache[key] = value; conn.Strings.Set(db, key, value, timeoutSeconds); if (broadcastInvalidation) conn.Publish(cacheInvalidationChannel, key); } } static class Program { static void ShowResult(MyCache cache0, MyCache cache1, string key, string caption) { Console.WriteLine(caption); byte[] blob0 = cache0.Get(key), blob1 = cache1.Get(key); Console.WriteLine("{0} vs {1}", blob0 == null ? "(null)" : Encoding.UTF8.GetString(blob0), blob1 == null ? "(null)" : Encoding.UTF8.GetString(blob1) ); } public static void Main() { MyCache cache0 = new MyCache(), cache1 = new MyCache(); string someRandomKey = "key" + new Random().Next().ToString(); ShowResult(cache0, cache1, someRandomKey, "Initially"); cache0.Set(someRandomKey, Encoding.UTF8.GetBytes("hello")); Thread.Sleep(10); // the pub/sub is fast, but not *instant* ShowResult(cache0, cache1, someRandomKey, "Write to 0"); cache1.Set(someRandomKey, Encoding.UTF8.GetBytes("world")); Thread.Sleep(10); // the pub/sub is fast, but not *instant* ShowResult(cache0, cache1, someRandomKey, "Write to 1"); } } 

Note that with a full implementation, you probably want to handle random connection interruptions, with delayed reconnection, etc.

+8
source

Source: https://habr.com/ru/post/945175/


All Articles