From 443bfe5aaf9ee3d57c031ef805b4a75935473374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Mon, 28 Mar 2022 00:24:39 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=B0=E5=A2=9ELFU=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/Cache/LFUCache.cs | 157 +++++++++++++++++++++++++ TextLocator/Cache/LFUCacheEntity.cs | 27 +++++ TextLocator/Properties/AssemblyInfo.cs | 4 +- TextLocator/TextLocator.csproj | 2 + TextLocator/Util/CacheUtil.cs | 22 ++-- 5 files changed, 201 insertions(+), 11 deletions(-) create mode 100644 TextLocator/Cache/LFUCache.cs create mode 100644 TextLocator/Cache/LFUCacheEntity.cs diff --git a/TextLocator/Cache/LFUCache.cs b/TextLocator/Cache/LFUCache.cs new file mode 100644 index 0000000..2987fb4 --- /dev/null +++ b/TextLocator/Cache/LFUCache.cs @@ -0,0 +1,157 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TextLocator.Cache +{ + + /// + /// LFU(Least Frequently Used)缓存机制 + /// 从数据集中,挑选最不经常使用的数据淘汰。 + /// + public class LFUCache + { + /// + /// 数据键值对 + /// + private Dictionary dataDic; + /// + /// 缓存频率数据节点 + /// + private Dictionary> frequenNodeListDic; + /// + /// 缓存容量大小 + /// + private int _capacity; + /// + /// 最小频率 + /// + private int _minFreq; + + /// + /// 构造函数 + /// + /// 缓存吃大小 + public LFUCache(int capacity) + { + _capacity = capacity; + _minFreq = 0; + + dataDic = new Dictionary(capacity); + frequenNodeListDic = new Dictionary>(); + + frequenNodeListDic.Add(0, new LinkedList()); + } + + /// + /// 获取缓存 + /// + /// 接收数据类型 + /// 缓存Key + /// 缓存Value + public T Get(string key) + { + // 验证缓存是否存在 + if (!Exists(key)) + return default(T); + + // 获取缓存值 + var value = dataDic[key].Value; + // 重新写入,频率计数器+1 + Put(key, value); + try + { + // 返回Value + return (T)value; + } + catch + { + return default(T); + } + } + + /// + /// 设置缓存 + /// + /// 缓存Key + /// 缓存Value + public void Put(string key, object value) + { + // 如果容量为0,则返回 + if (_capacity == 0) + return; + + // 构造新的缓存对象 + var newCacheData = new LFUCacheEntity { Key = key, Value = value, Frequen = 0 }; + + // 缓存已存在 + if (dataDic.ContainsKey(key)) + { + // 缓存数据对象 + var cacheEntity = dataDic[key]; + + // 旧的计数器 + var oldFrequen = cacheEntity.Frequen; + // 旧的计数器节点列表 + var oldFrequenNodeList = frequenNodeListDic[oldFrequen]; + // 从缓存频率数据节点 + oldFrequenNodeList.Remove(cacheEntity); + + // 频率计数器+1 + var newFrequen = oldFrequen + 1; + // 缓存频率数据节点不存在 + if (!frequenNodeListDic.ContainsKey(newFrequen)) + { + // 新频率添加节点列表 + frequenNodeListDic.Add(newFrequen, new LinkedList()); + } + + // 设置新缓存频率计数器 + newCacheData.Frequen = newFrequen; + // 缓存频率数据节点添加新缓存 + frequenNodeListDic[newFrequen].AddLast(newCacheData); + // 数据缓存对象设置新缓存 + dataDic[key] = newCacheData; + + // 缓存频率数据节点存在缓存 && 节点数据为0 + if (frequenNodeListDic.ContainsKey(_minFreq) && frequenNodeListDic[_minFreq].Count == 0) { + // 记录访问频率 + _minFreq = newFrequen; + } + return; + } + + // 缓存池 超出容量 + if (_capacity <= dataDic.Count) + { + // 根据记录的访问频率获取需要删除的节点列表 + var deleteNodeList = frequenNodeListDic[_minFreq]; + // 获取需要删除的节点,节点列表第一个元素 + var deleteFirstNode = deleteNodeList.First; + // 删除节点列表第一个元素 + deleteNodeList.RemoveFirst(); + // 数据缓存吃删除第一个元素对应的Key + dataDic.Remove(deleteFirstNode.Value.Key); + } + + // 缓存频率数据节点最后添加新缓存 + frequenNodeListDic[0].AddLast(newCacheData); + // 数据缓存添加新缓存 + dataDic.Add(key, newCacheData); + // 频率计数器归零 + _minFreq = 0; + } + + /// + /// 是否存在缓存 + /// + /// + /// + public bool Exists(string key) + { + return dataDic.ContainsKey(key); + } + } +} diff --git a/TextLocator/Cache/LFUCacheEntity.cs b/TextLocator/Cache/LFUCacheEntity.cs new file mode 100644 index 0000000..6d6b83b --- /dev/null +++ b/TextLocator/Cache/LFUCacheEntity.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TextLocator.Cache +{ + /// + /// LFU缓存对象 + /// + public class LFUCacheEntity + { + /// + /// 缓存Key + /// + public string Key { get; set; } + /// + /// 缓存Value + /// + public object Value { get; set; } + /// + /// 缓存使用频率计数器 + /// + public int Frequen { get; set; } + } +} diff --git a/TextLocator/Properties/AssemblyInfo.cs b/TextLocator/Properties/AssemblyInfo.cs index ec36ef2..666cd99 100644 --- a/TextLocator/Properties/AssemblyInfo.cs +++ b/TextLocator/Properties/AssemblyInfo.cs @@ -49,8 +49,8 @@ using System.Windows; //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 //通过使用 "*",如下所示: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.2.10")] -[assembly: AssemblyFileVersion("1.2.10.1")] +[assembly: AssemblyVersion("1.2.11")] +[assembly: AssemblyFileVersion("1.2.11.0")] // log4net [assembly: log4net.Config.XmlConfigurator(Watch = true)] \ No newline at end of file diff --git a/TextLocator/TextLocator.csproj b/TextLocator/TextLocator.csproj index a913e9b..687fbb9 100644 --- a/TextLocator/TextLocator.csproj +++ b/TextLocator/TextLocator.csproj @@ -177,6 +177,8 @@ MSBuild:Compile Designer + + diff --git a/TextLocator/Util/CacheUtil.cs b/TextLocator/Util/CacheUtil.cs index 82b2c6d..888f2ff 100644 --- a/TextLocator/Util/CacheUtil.cs +++ b/TextLocator/Util/CacheUtil.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using TextLocator.Cache; namespace TextLocator.Util { @@ -7,15 +8,22 @@ namespace TextLocator.Util /// public class CacheUtil { - //缓存容器 - private static Dictionary cacheDic = new Dictionary(); + /// + /// LFU缓存 + /// + private static LFUCache _cache; + + static CacheUtil() + { + _cache = new LFUCache(10000); + } /// /// 添加缓存 /// public static void Add(string key, object value) { - cacheDic[key] = value; + _cache.Put(key, value); } /// @@ -23,11 +31,7 @@ namespace TextLocator.Util /// public static T Get(string key) { - try - { - return (T)cacheDic[key]; - } catch { } - return default(T); + return _cache.Get(key); } /// @@ -37,7 +41,7 @@ namespace TextLocator.Util /// public static bool Exsits(string key) { - return cacheDic.ContainsKey(key); + return _cache.Exists(key); } } } -- Gitee From 6a2258fc2b7cd92c7bec43ea49663fd876ac78f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= Date: Tue, 29 Mar 2022 20:26:10 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=B1=A0=E5=AE=B9?= =?UTF-8?q?=E9=87=8F=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/Cache/LFUCache.cs | 2 +- TextLocator/Core/AppConst.cs | 4 ++ TextLocator/SettingWindow.xaml | 84 ++++++++++++++++++------------- TextLocator/SettingWindow.xaml.cs | 25 +++++++++ TextLocator/Util/CacheUtil.cs | 3 +- 5 files changed, 81 insertions(+), 37 deletions(-) diff --git a/TextLocator/Cache/LFUCache.cs b/TextLocator/Cache/LFUCache.cs index 2987fb4..b603214 100644 --- a/TextLocator/Cache/LFUCache.cs +++ b/TextLocator/Cache/LFUCache.cs @@ -33,7 +33,7 @@ namespace TextLocator.Cache /// /// 构造函数 /// - /// 缓存吃大小 + /// 缓存池容量 public LFUCache(int capacity) { _capacity = capacity; diff --git a/TextLocator/Core/AppConst.cs b/TextLocator/Core/AppConst.cs index 2851fb6..f96aaf9 100644 --- a/TextLocator/Core/AppConst.cs +++ b/TextLocator/Core/AppConst.cs @@ -36,6 +36,10 @@ namespace TextLocator.Core /// 压缩包解析大小 /// public static int ZIP_FILE_SIZE_LIMIT = int.Parse(AppUtil.ReadValue("AppConfig", "ZipFileSizeLimit", "20000000")); + /// + /// 缓存池容量 + /// + public static int CACHE_POOL_CAPACITY = int.Parse(AppUtil.ReadValue("AppConfig", "CachePoolCapacity", "1000000")); /// diff --git a/TextLocator/SettingWindow.xaml b/TextLocator/SettingWindow.xaml index b2a4b64..53a033c 100644 --- a/TextLocator/SettingWindow.xaml +++ b/TextLocator/SettingWindow.xaml @@ -10,50 +10,64 @@ Title="设置" Height="380" Width="520" WindowStartupLocation="CenterScreen" WindowStyle="ToolWindow" ResizeMode="CanMinimize" Icon="/Resource/App.ico" Loaded="Window_Loaded" Closed="Window_Closed" > - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - + + + + + + + - - + + - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + - - + +