36078604b8ed674b445bf0e3a75b7a98703d0a6d
Release-Notes/CacheSample.txt
| ... | ... | @@ -0,0 +1,55 @@ |
| 1 | +using AdHoc.Cache.DataCache; |
|
| 2 | +using System; |
|
| 3 | +using System.Collections.Generic; |
|
| 4 | +using System.Linq; |
|
| 5 | +using System.Web; |
|
| 6 | +using System.IO; |
|
| 7 | +using StackExchange.Redis; |
|
| 8 | + |
|
| 9 | +public class CustomDbSchemaCache : IPermanentDataCache |
|
| 10 | +{ |
|
| 11 | + ConnectionMultiplexer redis; |
|
| 12 | + private string serverString; |
|
| 13 | + private string dbCachePrefix = "iz_db_"; // Used to determine which keys in redis DB are from Izenda DB Cache so they can be queried in a batch |
|
| 14 | + |
|
| 15 | + public CustomDbSchemaCache() |
|
| 16 | + { |
|
| 17 | + serverString = "localhost:6379"; |
|
| 18 | + redis = ConnectionMultiplexer.Connect(serverString); |
|
| 19 | + } |
|
| 20 | + |
|
| 21 | + #region IPermanentDataCache |
|
| 22 | + |
|
| 23 | + public byte[] Get(string key) |
|
| 24 | + { |
|
| 25 | + key = dbCachePrefix + key; |
|
| 26 | + return redis.GetDatabase().StringGet(key); |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + public void Set(string key, byte[] value) |
|
| 30 | + { |
|
| 31 | + key = dbCachePrefix + key; |
|
| 32 | + redis.GetDatabase().StringSet(key, value); |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + public void Remove(string key) |
|
| 36 | + { |
|
| 37 | + key = dbCachePrefix + key; |
|
| 38 | + redis.GetDatabase().KeyDelete(key); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + public void Invalidate() |
|
| 42 | + { |
|
| 43 | + var server = redis.GetServer(serverString); |
|
| 44 | + foreach (var key in server.Keys(pattern: dbCachePrefix + "*")) |
|
| 45 | + redis.GetDatabase().KeyDelete(key); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + public string[] AllKeys() |
|
| 49 | + { |
|
| 50 | + var server = redis.GetServer(serverString); |
|
| 51 | + return server.Keys(pattern: dbCachePrefix + "*").Select(k => k.ToString().Replace(dbCachePrefix, "")).ToArray(); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + #endregion |
|
| 55 | +} |
|
| ... | ... | \ No newline at end of file |