From 45f9b41d4e33acea97467b42abeb636cb704af87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=BA=E8=83=BD=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Wed, 6 Dec 2023 23:23:18 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=E4=BF=AE=E6=AD=A3=E6=99=AE=E9=80=9A?= =?UTF-8?q?=E5=8F=AF=E7=A9=BA=E5=AD=97=E6=AE=B5=E9=81=87=E5=88=B0=E5=B9=B6?= =?UTF-8?q?=E8=A1=8C=E5=86=B2=E7=AA=81=E9=97=AE=E9=A2=98=EF=BC=8C=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E5=B9=B6=E8=A1=8C=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewLife.CubeNC/Session/SessionProvider.cs | 48 +++++++++++++++++++++-- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/NewLife.CubeNC/Session/SessionProvider.cs b/NewLife.CubeNC/Session/SessionProvider.cs index f7a472b2..700c69fc 100644 --- a/NewLife.CubeNC/Session/SessionProvider.cs +++ b/NewLife.CubeNC/Session/SessionProvider.cs @@ -1,5 +1,5 @@ -using NewLife.Caching; -using NewLife.Collections; +using System.Collections.Concurrent; +using NewLife.Caching; namespace NewLife.Cube; @@ -24,12 +24,52 @@ public class SessionProvider Cache.SetExpire(sessionKey, Expire); //!! 临时修正可空字典的BUG - if (Cache is MemoryCache mc && dic is not NullableDictionary) + if (Cache is MemoryCache mc && dic is not NullableDictionary2) { - dic = new NullableDictionary(dic, StringComparer.Ordinal); + dic = new NullableDictionary2(dic, StringComparer.Ordinal); mc.Set(sessionKey, dic); } return dic; } +} + + +/// 可空字典。获取数据时如果指定键不存在可返回空而不是抛出异常 +/// +/// +class NullableDictionary2 : ConcurrentDictionary, IDictionary where TKey : notnull +{ + /// 实例化一个可空字典 + public NullableDictionary2() { } + + /// 指定比较器实例化一个可空字典 + /// + public NullableDictionary2(IEqualityComparer comparer) : base(comparer) { } + + /// 实例化一个可空字典 + /// + public NullableDictionary2(IDictionary dic) : base(dic) { } + + /// 实例化一个可空字典 + /// + /// + public NullableDictionary2(IDictionary dic, IEqualityComparer comparer) : base(dic, comparer) { } + + /// 获取 或 设置 数据 + /// + /// + public new TValue this[TKey item] + { + get + { + if (TryGetValue(item, out var v)) return v; + + return default!; + } + set + { + base[item] = value; + } + } } \ No newline at end of file