{"id":233528,"date":"2022-01-14T10:13:45","date_gmt":"2022-01-14T02:13:45","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=233528"},"modified":"2022-01-04T10:14:33","modified_gmt":"2022-01-04T02:14:33","slug":"memorycache","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/memorycache.html","title":{"rendered":"MemoryCache \u7684\u539f\u751f\u63d2\u503c\u65b9\u5f0f\u6d45\u8c08"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u5728Github\u4e0a\u4e5f\u6709issue\u8ba8\u8bba\uff0c\u4ece2017\u5e74\u5f00\u59cb\u5c31\u6709\u5927\u4f6c\u8d28\u7591\u8fd9\u662f\u4e00\u4e2a\u53cd\u4eba\u7c7b\u7684\u8bbe\u8ba1\u601d\u8def\uff0c\u5b98\u65b9\u4e3a\u4e86\u4e0d\u5f15\u5165Break Change\uff0c\u4e00\u76f4\u4fdd\u6301\u5230\u73b0\u5728\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

.NET\u8fd0\u884c\u65f6\u5185\u7f6e\u4e86\u5e38\u7528\u7684\u7f13\u5b58\u6a21\u5757\uff1aMemoryCache
\n\"\"
\n\u6807\u51c6\u7684MemoryCache\u66b4\u9732\u4e86\u5982\u4e0b\u51e0\u4e2a\u5c5e\u6027\u548c\u65b9\u6cd5\uff1a<\/p>\n

\r\npublic int Count { get; }\r\npublic void Compact(double percentage);\r\npublic ICacheEntry CreateEntry(object key);\r\npublic void Dispose();\r\npublic void Remove(object key);\r\npublic bool TryGetValue(object key, out object result);\r\nprotected virtual void Dispose(bool disposing);\r\n<\/pre>\n

\u4f46\u662f\u4f60\u4f7f\u7528\u5e38\u89c4\u6a21\u5f0f\u53bb\u63d2\u503c\/\u83b7\u53d6\u503c\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u610f\u60f3\u4e0d\u5230\u7684\u60c5\u51b5\u3002<\/p>\n

\u5c31\u5982\u4e0b\u8fd9\u6837\u7684\u5e38\u89c4\u4ee3\u7801\uff1a<\/p>\n

\r\nvar s = new MemoryCache(new MemoryCacheOptions { });\r\nvar entry = s.CreateEntry(\"WeChatID\");\r\nentry.Value = \"\u7cbe\u76ca\u7801\u519c\";\r\n\r\nvar f = s.TryGetValue(\"WeChatID\",out object obj);\r\n\r\nConsole.WriteLine(f);\r\nConsole.WriteLine(obj);\r\n<\/pre>\n

\u4f1a\u8f93\u51fa\u5982\u4e0b\u7ed3\u679c\uff1a
\n\"\"
\n\u662f\u4e0d\u662f\u5f88\u610f\u5916\u3002<\/p>\n

\u4f46\u662f\u770b\u5b98\u4eec\u4e00\u822c\u4e0d\u4f1a\u4f7f\u7528MemoryCache\u7684\u539f\u751f\u65b9\u6cd5\uff0c\u800c\u662f\u4f7f\u7528\u4f4d\u4e8e\u540c\u4e00\u547d\u540d\u7a7a\u95f4\u7684 \u6269\u5c55\u65b9\u6cd5Set\u3002<\/p>\n

\r\nvar s = new MemoryCache(new MemoryCacheOptions { });\r\ns.Set(\"WeChatID\", \"\u7cbe\u76ca\u7801\u519c\");\r\nvar f = s.TryGetValue(\"WeChatID\", out object obj);\r\n\r\nConsole.WriteLine(f);\r\nConsole.WriteLine(obj);\r\n<\/pre>\n

\u5982\u6b64\u4fbf\u80fd\u6b63\u786e\u8f93\u51fa\u3002
\n\"\"
\n\u6269\u5c55\u7c7b\u6e90\u7801\u770b\u4e00\u770b<\/p>\n

\r\npublic static TItem Set(this IMemoryCache cache, object key, TItem value)\r\n{\r\nusing ICacheEntry entry = cache.CreateEntry(key);\r\nentry.Value = value;\r\nreturn value;\r\n<\/pre>\n

\u6269\u5c55\u65b9\u6cd5\u4e0e\u539f\u751f\u65b9\u6cd5\u7684\u5dee\u5f02\u5728\u4e8eusing\u5173\u952e\u5b57 (\u4e5f\u8bf4\u660e\u4e86CacheEntry\u7ee7\u627f\u81eaIDisposable\u63a5\u53e3)\u3002<\/p>\n

\u7ee7\u7eed\u8ffd\u6eafCacheEntry\u5b9e\u73b0\u7684Dispose\u65b9\u6cd5\uff1a<\/p>\n

\r\npublic void Dispose()\r\n{\r\nif (!_state.IsDisposed)\r\n{\r\n_state.IsDisposed = true;\r\n\r\nif (_cache.TrackLinkedCacheEntries)\r\n{\r\nCacheEntryHelper.ExitScope(this, _previous);\r\n}\r\n\r\n\/\/ Don't commit or propagate options if the CacheEntry Value was never set.\r\n\/\/ We assume an exception occurred causing the caller to not set the Value successfully,\r\n\/\/ so don't use this entry.\r\nif (_state.IsValueSet)\r\n{\r\n_cache.SetEntry(this);\r\n\r\nif (_previous != null && CanPropagateOptions())\r\n{\r\nPropagateOptions(_previous);\r\n}\r\n}\r\n\r\n_previous = null; \/\/ we don't want to root unnecessary objects\r\n}\r\n}\r\n<\/pre>\n

\u6ce8\u610f\u5176\u4e2d\u7684_cache.SetEntry(this)\uff0c\u8868\u793a\u5728MemoryCache\u5e95\u5c42\u7684ConcurrentDictionary<\/p>\n

\u7efc\u4e0a\uff1a\u7f13\u5b58\u9879CacheEntry\u9700\u8981\u88abDispose\uff0c\u624d\u80fd\u88ab\u63d2\u5165MemoeyCache\u3002<\/p>\n

\u8fd9\u662f\u600e\u6837\u7684\u8bbe\u8ba1\u6a21\u5f0f?IDisposable\u63a5\u53e3\u4e0d\u662f\u7528\u6765\u91ca\u653e\u8d44\u6e90\u5417?<\/p>\n

\u4e3a\u5565\u8981\u4f7f\u7528Dispose\u65b9\u6cd5\u6765\u5411MemoryCache\u63d2\u503c?<\/p>\n

\u4e0d\u80fd\u4f7f\u7528\u4e00\u4e2a\u660e\u786e\u7684Commit\u65b9\u6cd5\u5417?<\/p>\n

\u8fd9\u5728Github\u4e0a\u4e5f\u6709issue\u8ba8\u8bba\uff0c\u4ece2017\u5e74\u5f00\u59cb\u5c31\u6709\u5927\u4f6c\u8d28\u7591\u8fd9\u662f\u4e00\u4e2a\u53cd\u4eba\u7c7b\u7684\u8bbe\u8ba1\u601d\u8def\uff0c\u5b98\u65b9\u4e3a\u4e86\u4e0d\u5f15\u5165Break Change\uff0c\u4e00\u76f4\u4fdd\u6301\u5230\u73b0\u5728\u3002<\/p>\n

\u57fa\u4e8e\u6b64\u73b0\u72b6\uff0c\u6211\u4eec\u5982\u679c\u4f7f\u7528MemoryCache\u7684\u539f\u751f\u63d2\u503c\u65b9\u6cd5\uff0c \u9700\u8981\u8fd9\u6837\uff1a<\/p>\n

\r\nvar s = new MemoryCache(new MemoryCacheOptions { });\r\nusing (var entry = s.CreateEntry(\"WeChatID\"))\r\n{\r\nentry.Value = \"\u7cbe\u76ca\u7801\u519c\";\r\n}\r\nvar f = s.TryGetValue(\"WeChatID\", out object obj);\r\n...\r\n<\/pre>\n

\u5c3d\u91cf\u4e0d\u8981\u4f7f\u7528C#8.0\u63a8\u51fa\u7684\u4e0d\u5e26\u5927\u62ec\u53f7\u7684using\u8bed\u6cd5<\/p>\n

\r\nusing var entry = s.CreateEntry(\"WeChatID\");\r\nentry.Value = \"\u7cbe\u76ca\u7801\u519c\";\r\n\r\nvar f = s.TryGetValue(\"WeChatID\", out object obj);\r\n...\r\n<\/pre>\n

\u8fd9\u79cd\u6ca1\u660e\u786e\u6307\u5b9ausing\u4f5c\u7528\u8303\u56f4\u7684\u8bed\u6cd5\uff0c\u4f1a\u5728\u51fd\u6570\u672b\u5c3e\u624d\u6267\u884cDispose\u65b9\u6cd5, \u5bfc\u81f4\u6267\u884c\u5230TryGetValue\u65f6\uff0c\u7f13\u5b58\u9879\u5176\u5b9e\u8fd8\u6ca1\u63d2\u5165!!!<\/p>\n

Last<\/strong><\/div>\n

MemoryCache\u63d2\u503c\u7684\u5b9e\u73b0\u8fc7\u7a0b\u5f88\u5947\u8469
\n\u5c3d\u91cf\u4f7f\u7528\u5e26\u660e\u786e\u5927\u62ec\u53f7\u8303\u56f4\u7684using\u8bed\u6cd5\uff0cC#8.0\u63a8\u51fa\u7684\u4e0d\u5e26\u5927\u62ec\u53f7\u7684using\u8bed\u6cd5\u7cd6\u7684\u4f5c\u7528\u65f6\u523b\u5728\u51fd\u6570\u672b\u5c3e\uff0c\u4f1a\u5e26\u6765\u8bef\u5bfc\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

.NET\u8fd0\u884c\u65f6\u5185\u7f6e\u4e86\u5e38\u7528\u7684\u7f13\u5b58\u6a21\u5757\uff1aMemoryCache \u6807\u51c6\u7684MemoryCache\u66b4\u9732\u4e86\u5982\u4e0b\u51e0\u4e2a\u5c5e\u6027\u548c\u65b9 […]<\/p>\n","protected":false},"author":1898,"featured_media":233532,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[789],"class_list":["post-233528","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-memorycache"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/233528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=233528"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/233528\/revisions"}],"predecessor-version":[{"id":233533,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/233528\/revisions\/233533"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/233532"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=233528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=233528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=233528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}