From 3b6ddc548b4ac57c4d582492ce026c224d34723a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E5=A4=B4?= Date: Mon, 16 Sep 2024 01:27:05 +0800 Subject: [PATCH] =?UTF-8?q?NewLife.Core=E7=9A=84=E5=86=85=E5=AD=98?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E7=AA=81=E7=A0=B410=E4=BA=BFTPS=E3=80=82?= =?UTF-8?q?=E6=9C=80=E5=90=8E=E4=B8=80=E6=AC=A1=E4=BC=98=E5=8C=96=EF=BC=8C?= =?UTF-8?q?=E6=98=AF=E6=8A=8A=E5=BD=93=E5=89=8D=E6=B5=8B=E8=AF=95=E8=BF=9B?= =?UTF-8?q?=E7=A8=8B=E7=9A=84=E4=BC=98=E5=85=88=E7=BA=A7=E6=8F=90=E9=AB=98?= =?UTF-8?q?=EF=BC=8C=E6=8A=A2=E5=8D=A0=E5=B0=BD=E5=8F=AF=E8=83=BD=E5=A4=9A?= =?UTF-8?q?=E7=9A=84CPU=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewLife.Core/Caching/Cache.cs | 30 +++++++++++++++++++---------- NewLife.Core/Caching/MemoryCache.cs | 19 ++++++++++++++++-- Test/Program.cs | 8 ++++++-- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/NewLife.Core/Caching/Cache.cs b/NewLife.Core/Caching/Cache.cs index ec1e03e2a..acda9d5be 100644 --- a/NewLife.Core/Caching/Cache.cs +++ b/NewLife.Core/Caching/Cache.cs @@ -36,6 +36,16 @@ public abstract class Cache : DisposeBase, ICache #region 构造 /// 构造函数 protected Cache() => Name = GetType().Name.TrimEnd("Cache"); + + /// 销毁。释放资源 + /// + protected override void Dispose(Boolean disposing) + { + base.Dispose(disposing); + + _keys = null; + //_keys2 = null; + } #endregion #region 基础操作 @@ -367,12 +377,12 @@ public virtual Int64 Bench(Boolean rand = false, Int32 batch = 0) // 提前准备Keys,减少性能测试中的干扰 var key = "bstr_"; - var key2 = "bint_"; + //var key2 = "bint_"; var max = cpu > 64 ? cpu : 64; var maxTimes = times * max; if (!rand) maxTimes = max; _keys = new String[maxTimes]; - _keys2 = new String[maxTimes]; + //_keys2 = new String[maxTimes]; var sb = new StringBuilder(); for (var i = 0; i < _keys.Length; i++) @@ -382,10 +392,10 @@ public virtual Int64 Bench(Boolean rand = false, Int32 batch = 0) sb.Append(i); _keys[i] = sb.ToString(); - sb.Clear(); - sb.Append(key2); - sb.Append(i); - _keys2[i] = sb.ToString(); + //sb.Clear(); + //sb.Append(key2); + //sb.Append(i); + //_keys2[i] = sb.ToString(); } // 单线程 @@ -417,7 +427,7 @@ public virtual Int64 Bench(Boolean rand = false, Int32 batch = 0) protected virtual Int32 GetTimesPerThread(Boolean rand, Int32 batch) => 10_000; private String[]? _keys; - private String[]? _keys2; + //private String[]? _keys2; /// 使用指定线程测试指定次数 /// 次数 /// 线程 @@ -444,12 +454,12 @@ public virtual Int64 BenchOne(Int64 times, Int32 threads, Boolean rand, Int32 ba // 读取测试 rs += BenchGet(_keys, times, threads, rand, batch); + // 累加测试 + rs += BenchInc(_keys!, times, threads, rand, batch); + // 删除测试 rs += BenchRemove(_keys, times, threads, rand, batch); - // 累加测试 - rs += BenchInc(_keys2!, times, threads, rand, batch); - return rs; } diff --git a/NewLife.Core/Caching/MemoryCache.cs b/NewLife.Core/Caching/MemoryCache.cs index 70a4a631d..abf7f8753 100644 --- a/NewLife.Core/Caching/MemoryCache.cs +++ b/NewLife.Core/Caching/MemoryCache.cs @@ -17,7 +17,7 @@ public class KeyEventArgs : CancelEventArgs public String Key { get; set; } = null!; } -/// 默认字典缓存 +/// 内存缓存。并行字典实现,峰值性能10亿ops public class MemoryCache : Cache { #region 属性 @@ -497,7 +497,22 @@ protected class CacheItem /// 设置数值和过期时间 /// /// 过期时间,秒 - public void Set(T value, Int32 expire) => Set(value, TimeSpan.FromSeconds(expire)); + public void Set(T value, Int32 expire) + { + var type = typeof(T); + TypeCode = type.GetTypeCode(); + + if (IsInt()) + _valueLong = value.ToLong(); + else + _value = value; + + var now = VisitTime = Runtime.TickCount64; + if (expire <= 0) + ExpiredTime = Int64.MaxValue; + else + ExpiredTime = now + expire * 1000; + } /// 设置数值和过期时间 /// diff --git a/Test/Program.cs b/Test/Program.cs index 23066063e..0486ee387 100644 --- a/Test/Program.cs +++ b/Test/Program.cs @@ -204,8 +204,12 @@ private static void Test3() private static void Test4() { - var v = Rand.NextBytes(32); - Console.WriteLine(v.ToBase64()); + // 提升进程优先级 + var p = Process.GetCurrentProcess(); + p.PriorityClass = ProcessPriorityClass.High; + + //var v = Rand.NextBytes(32); + //Console.WriteLine(v.ToBase64()); ICache ch = null; //ICache ch = new DbCache();