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