Skip to content

Commit

Permalink
v1.2.8 - add ClientSideCaching hgetall/hget/hmget
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Dec 4, 2023
1 parent 12dee8b commit 67f40cf
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
13 changes: 10 additions & 3 deletions examples/console_net8_client_side_caching/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class Program
{
static Lazy<RedisClient> _cliLazy = new Lazy<RedisClient>(() =>
{
//var r = new RedisClient("127.0.0.1:6379", false); //redis 3.2 Single test
var r = new RedisClient("192.168.164.10:6379"); //redis 3.2 Single test
var r = new RedisClient("127.0.0.1:6379"); //redis 3.2 Single test
//var r = new RedisClient("192.168.164.10:6379"); //redis 3.2 Single test
//var r = new RedisClient("127.0.0.1:6379,database=1,min pool size=500,max pool size=500"); //redis 3.2
//var r = new RedisClient("127.0.0.1:6379,database=10", "127.0.0.1:6380,database=10", "127.0.0.1:6381,database=10");
//var r = new RedisClient(new [] { (ConnectionStringBuilder)"192.168.164.10:6379,database=1", (ConnectionStringBuilder)"192.168.164.10:6379,database=2" }); //redis 6.0
Expand All @@ -32,11 +32,18 @@ static void Main(string[] args)
//本地缓存的容量
Capacity = 3,
//过滤哪些键能被本地缓存
KeyFilter = key => key.StartsWith("Interceptor"),
//KeyFilter = key => key.StartsWith("Interceptor"),
//检查长期未使用的缓存
CheckExpired = (key, dt) => DateTime.Now.Subtract(dt) > TimeSpan.FromSeconds(600)
});

while (Console.ReadKey().Key != ConsoleKey.Escape)
{
Console.WriteLine(cli.HGetAll("hash01"));
Console.WriteLine(cli.HGet("hash01", "f3"));
Console.WriteLine(cli.HMGet("hash01", "f3", "f2"));
}

cli.Set("Interceptor01", "123123"); //redis-server

var val1 = cli.Get("Interceptor01"); //redis-server
Expand Down
44 changes: 42 additions & 2 deletions src/FreeRedis/ClientSideCaching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,39 @@ public void Before(InterceptorBeforeEventArgs args)
_iscached = true;
}
break;
}
case "HGETALL":
if (_cscc.TryGetCacheValue(args.Command.GetKey(0), args.ValueType, out var hgetallval))
{
args.Value = hgetallval;
_iscached = true;
}
break;
case "HGET":
if (_cscc.TryGetCacheValue(args.Command.GetKey(0), typeof(Dictionary<string, string>), out var hvals2))
{
var dict = hvals2 as Dictionary<string, string>;
var keyIndex = args.Command._keyIndexes.Max(a => a);
if (dict.TryGetValue(args.Command._input[keyIndex + 1]?.ToString(), out var hval))
{
args.Value = hval;
_iscached = true;
}
}
break;
case "HMGET":
if (_cscc.TryGetCacheValue(args.Command.GetKey(0), typeof(Dictionary<string, string>), out var hvals3))
{
var dict = hvals3 as Dictionary<string, string>;
var keyIndex = args.Command._keyIndexes.Max(a => a);
var vals = new string[args.Command._input.Count - keyIndex - 1];
for (var a = 0; a < vals.Length; a++)
if (dict.TryGetValue(args.Command._input[keyIndex + 1 + a]?.ToString(), out var hval))
vals[a] = hval;
args.Value = vals;
_iscached = true;
}
break;
}
}

public void After(InterceptorAfterEventArgs args)
Expand Down Expand Up @@ -323,7 +355,15 @@ public void After(InterceptorAfterEventArgs args)
}
}
break;
default:
case "HGETALL":
if (_iscached == false && args.Exception == null)
{
var getkey = args.Command.GetKey(0);
if (_cscc._options.KeyFilter?.Invoke(getkey) != false)
_cscc.SetCacheValue(args.Command, args.Command._command, getkey, args.ValueType, args.Value);
}
break;
default:
if (args.Command._keyIndexes.Any())
{
var cmdset = CommandSets.Get(args.Command._command);
Expand Down
2 changes: 1 addition & 1 deletion src/FreeRedis/FreeRedis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AssemblyName>FreeRedis</AssemblyName>
<PackageId>FreeRedis</PackageId>
<RootNamespace>FreeRedis</RootNamespace>
<Version>1.2.7</Version>
<Version>1.2.8</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/2881099/FreeRedis</PackageProjectUrl>
<Description>FreeRedis is .NET redis client, supports cluster, sentinel, master-slave, pipeline, transaction and connection pool.</Description>
Expand Down

0 comments on commit 67f40cf

Please sign in to comment.