{"id":621,"date":"2025-12-07T15:39:25","date_gmt":"2025-12-07T07:39:25","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=621"},"modified":"2025-12-07T15:39:25","modified_gmt":"2025-12-07T07:39:25","slug":"%e4%bd%bf%e7%94%a8-net%e6%a0%87%e5%87%86%e5%ba%93%e5%ae%9e%e7%8e%b0%e5%86%85%e5%ad%98%e7%bc%93%e5%ad%98%e6%9c%ba%e5%88%b6%e7%9a%84%e8%af%a6%e7%bb%86%e8%bf%87%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/07\/%e4%bd%bf%e7%94%a8-net%e6%a0%87%e5%87%86%e5%ba%93%e5%ae%9e%e7%8e%b0%e5%86%85%e5%ad%98%e7%bc%93%e5%ad%98%e6%9c%ba%e5%88%b6%e7%9a%84%e8%af%a6%e7%bb%86%e8%bf%87%e7%a8%8b\/","title":{"rendered":"\u4f7f\u7528.NET\u6807\u51c6\u5e93\u5b9e\u73b0\u5185\u5b58\u7f13\u5b58\u673a\u5236\u7684\u8be6\u7ec6\u8fc7\u7a0b"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u6574\u7406\u4e00\u4efd <strong>\u300a\u4f7f\u7528 .NET \u6807\u51c6\u5e93\u5b9e\u73b0\u5185\u5b58\u7f13\u5b58\u673a\u5236\u7684\u8be6\u7ec6\u8fc7\u7a0b\u300b<\/strong>\uff0c\u9002\u7528\u4e8e\uff1a<br>\u2714 .NET Standard<br>\u2714 .NET Core \/ .NET 5-8<br>\u2714 \u63a7\u5236\u53f0 \/ Web \/ \u670d\u52a1 \/ SDK \u7c7b\u5e93<br>\u65e0\u4efb\u4f55\u7b2c\u4e09\u65b9\u4f9d\u8d56\uff08\u53ea\u7528\u6807\u51c6\u5e93\uff09\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u4e00\u3001.NET \u6807\u51c6\u5e93\u5e76\u6ca1\u6709\u5185\u7f6e MemoryCache\uff1f<\/h1>\n\n\n\n<p>\u6709\uff01<br>\u4f60\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>System.Runtime.Caching.MemoryCache<\/strong><\/h3>\n\n\n\n<p>\u8be5\u547d\u540d\u7a7a\u95f4\u5728 .NET Standard 2.0+\u3001.NET Framework\u3001.NET Core \u90fd\u53ef\u7528\uff08\u7531\u517c\u5bb9\u5305\u652f\u6301\uff09\u3002<\/p>\n\n\n\n<p>\u2714 \u652f\u6301\u7f13\u5b58\u4f9d\u8d56<br>\u2714 \u652f\u6301\u8fc7\u671f\u7b56\u7565<br>\u2714 \u652f\u6301\u4f18\u5148\u7ea7<br>\u2714 \u652f\u6301\u7f13\u5b58\u9879\u79fb\u9664\u56de\u8c03<br>\u2714 \u4f01\u4e1a\u7ea7\u6210\u719f\u65b9\u6848<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u4e8c\u3001\u5b89\u88c5\uff08\u4ec5 .NET Core \/ .NET 5-8 \u9700\u8981\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndotnet add package System.Runtime.Caching\n\n<\/pre><\/div>\n\n\n<p>.NET Framework \u81ea\u5e26\uff0c\u65e0\u9700\u5b89\u88c5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u4e09\u3001MemoryCache \u7684\u57fa\u672c\u7528\u6cd5<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u521b\u5efa\u7f13\u5b58\u5b9e\u4f8b\uff08\u5355\u4f8b\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nusing System.Runtime.Caching;\n\nvar cache = MemoryCache.Default;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u653e\u5165\u7f13\u5b58<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncache.Set(&quot;user_1&quot;, &quot;Jack&quot;, DateTimeOffset.Now.AddMinutes(10));\n\n<\/pre><\/div>\n\n\n<p>\u5e26\u8fc7\u671f\u65f6\u95f4\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u8bfb\u53d6\u7f13\u5b58<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nvar name = cache.Get(&quot;user_1&quot;);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u5220\u9664\u7f13\u5b58<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncache.Remove(&quot;user_1&quot;);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u56db\u3001MemoryCache \u5b8c\u6574\u793a\u4f8b\uff1a\u5e26\u7b56\u7565\u3001\u56de\u8c03<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nusing System;\nusing System.Runtime.Caching;\n\nclass Program\n{\n    static void Main()\n    {\n        var cache = MemoryCache.Default;\n\n        var policy = new CacheItemPolicy\n        {\n            AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(10),\n\n            \/\/ \u7f13\u5b58\u88ab\u79fb\u9664\u65f6\u56de\u8c03\n            RemovedCallback = (args) =&gt;\n            {\n                Console.WriteLine($&quot;\u7f13\u5b58\u88ab\u79fb\u9664\uff1a{args.CacheItem.Key}, \u539f\u56e0\uff1a{args.RemovedReason}&quot;);\n            }\n        };\n\n        cache.Set(&quot;token&quot;, &quot;ABCDEFG&quot;, policy);\n\n        Console.WriteLine(&quot;\u5df2\u5199\u5165\u7f13\u5b58 token=ABCDEFG&quot;);\n\n        Console.WriteLine(&quot;\u6309\u4efb\u610f\u952e\u7ee7\u7eed\u2026\u7b49\u5f85\u8fc7\u671f&quot;);\n        Console.ReadKey();\n\n        var token = cache.Get(&quot;token&quot;);\n        Console.WriteLine($&quot;\u8bfb\u53d6 token: {token ?? &quot;\u5df2\u8fc7\u671f&quot;}&quot;);\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>\u529f\u80fd\uff1a<br>\u2714 \u6dfb\u52a0\u7f13\u5b58<br>\u2714 10\u79d2\u540e\u81ea\u52a8\u8fc7\u671f<br>\u2714 \u79fb\u9664\u56de\u8c03\u81ea\u52a8\u89e6\u53d1<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u4e94\u3001\u8fc7\u671f\u7b56\u7565\uff1aAbsoluteExpiration vs SlidingExpiration<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. AbsoluteExpiration\uff08\u7edd\u5bf9\u8fc7\u671f\uff09<\/strong><\/h2>\n\n\n\n<p>\u5728\u6307\u5b9a\u65f6\u95f4\u540e\u5931\u6548\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npolicy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(5);\n\n<\/pre><\/div>\n\n\n<p>\u9002\u7528\uff1a<br>\u2714 Token<br>\u2714 \u4e34\u65f6\u6570\u636e<br>\u2714 \u5f3a\u5236\u66f4\u65b0\u7684\u6570\u636e<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. SlidingExpiration\uff08\u6ed1\u52a8\u8fc7\u671f\uff09<\/strong><\/h2>\n\n\n\n<p>\u5728 <strong>\u6bcf\u6b21\u8bbf\u95ee\u540e\u5ef6\u957f\u8fc7\u671f\u65f6\u95f4<\/strong>\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npolicy.SlidingExpiration = TimeSpan.FromMinutes(10);\n\n<\/pre><\/div>\n\n\n<p>\u9002\u7528\uff1a<br>\u2714 \u6d3b\u8dc3\u7528\u6237\u4f1a\u8bdd<br>\u2714 \u7ecf\u5e38\u8bbf\u95ee\u7684\u6570\u636e<br>\u2714 \u9875\u9762\u7f13\u5b58<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u516d\u3001\u4f18\u5148\u7ea7\u63a7\u5236 (CacheItemPriority)<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npolicy.Priority = CacheItemPriority.NotRemovable;\n\n<\/pre><\/div>\n\n\n<p>\u9632\u6b62\u5185\u5b58\u7d27\u5f20\u65f6\u88ab\u56de\u6536\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u4e03\u3001\u68c0\u67e5\u7f13\u5b58\u662f\u5426\u5b58\u5728<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nif (cache.Contains(&quot;user_1&quot;))\n{\n    var value = cache.Get(&quot;user_1&quot;);\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u516b\u3001\u5b58\u50a8\u590d\u6742\u7c7b\u578b<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncache&#x5B;&quot;user&quot;] = new User { Id = 1, Name = &quot;Jack&quot; };\n\n<\/pre><\/div>\n\n\n<p>\u53ea\u8981\u4e0d\u662f <code>Span<\/code>, <code>ref struct<\/code>\uff0c\u90fd\u53ef\u4ee5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u4e5d\u3001\u7f13\u5b58\u4f9d\u8d56\uff08\u6587\u4ef6\u4f9d\u8d56\u793a\u4f8b\uff09<\/h1>\n\n\n\n<p>\u7f13\u5b58\u9879\u4f1a\u5728\u6587\u4ef6\u53d8\u66f4\u540e\u81ea\u52a8\u79fb\u9664\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npolicy.ChangeMonitors.Add(\n    new HostFileChangeMonitor(new List&amp;lt;string&gt; { &quot;config.json&quot; })\n);\n\n<\/pre><\/div>\n\n\n<p>\u9002\u7528\uff1a<br>\u2714 \u914d\u7f6e\u6587\u4ef6\u81ea\u52a8\u70ed\u66f4\u65b0<br>\u2714 \u6a21\u677f\u66f4\u65b0\u81ea\u52a8\u5237\u65b0\u7f13\u5b58<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u5341\u3001\u5c01\u88c5\u4e00\u4e2a CacheHelper\uff08\u9879\u76ee\u901a\u7528\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic static class CacheHelper\n{\n    private static readonly MemoryCache _cache = MemoryCache.Default;\n\n    public static T GetOrCreate&amp;lt;T&gt;(string key, Func&amp;lt;T&gt; factory, int seconds = 60)\n    {\n        if (_cache.Contains(key))\n            return (T)_cache.Get(key);\n\n        var value = factory();\n\n        _cache.Set(key, value,\n            DateTimeOffset.Now.AddSeconds(seconds));\n\n        return value;\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>\u8c03\u7528\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nvar user = CacheHelper.GetOrCreate(&quot;user_1&quot;, () =&gt;\n{\n    Console.WriteLine(&quot;\u4ece\u6570\u636e\u5e93\u83b7\u53d6\u7528\u6237...&quot;);\n    return new User { Id = 1, Name = &quot;Jack&quot; };\n});\n\n<\/pre><\/div>\n\n\n<p>\u2714 \u7b2c\u4e00\u6b21\u4ece\u6570\u636e\u5e93\u8bfb<br>\u2714 \u4e4b\u540e\u90fd\u4ece\u7f13\u5b58\u8bfb<br>\u2714 \u7b80\u6d01\u9ad8\u6548<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u5341\u4e00\u3001MemoryCache vs ASP.NET Core IMemoryCache<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u529f\u80fd<\/th><th>MemoryCache (\u6807\u51c6\u5e93)<\/th><th>IMemoryCache (.NET Core)<\/th><\/tr><\/thead><tbody><tr><td>.NET Standard \u652f\u6301<\/td><td>\u2714<\/td><td>\u274c<\/td><\/tr><tr><td>\u652f\u6301\u6587\u4ef6\u4f9d\u8d56<\/td><td>\u2714<\/td><td>\u274c<\/td><\/tr><tr><td>\u652f\u6301\u4f18\u5148\u7ea7<\/td><td>\u2714<\/td><td>\u2714<\/td><\/tr><tr><td>\u652f\u6301\u8fc7\u671f\u7b56\u7565<\/td><td>\u2714<\/td><td>\u2714<\/td><\/tr><tr><td>\u53ef\u7528\u4e8e\u63a7\u5236\u53f0 \/ \u670d\u52a1<\/td><td>\u2714<\/td><td>\u274c<\/td><\/tr><tr><td>ASP.NET Core \u96c6\u6210<\/td><td>\u4e00\u822c<\/td><td>\u539f\u751f<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5982\u679c\u4f60\u5728\u5199 <strong>\u7c7b\u5e93\u3001SDK\u3001Console\u3001Windows Service\u3001\u8de8\u6846\u67b6\u5e93<\/strong> \u2192 <strong>\u7528 MemoryCache<\/strong>\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u5728\u5199 <strong>ASP.NET Core Web \u9879\u76ee<\/strong> \u2192 \u4f18\u5148\u7528 IMemoryCache\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83c\udfaf \u603b\u7ed3\uff08\u6700\u91cd\u8981\uff09<\/h1>\n\n\n\n<p>\u8981\u5728 <strong>.NET \u6807\u51c6\u5e93<\/strong> \u5b9e\u73b0\u7f13\u5b58\uff0c\u6700\u4f73\u65b9\u6848\u662f\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u2714 \u4f7f\u7528 System.Runtime.Caching.MemoryCache<\/h2>\n\n\n\n<p>\u65e0\u9700\u7b2c\u4e09\u65b9\u5e93<br>\u8de8\u7248\u672c\u517c\u5bb9<br>\u529f\u80fd\u5f3a\u5927<br>\u7a33\u5b9a\u6210\u719f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u6574\u7406\u4e00\u4efd \u300a\u4f7f\u7528 .NET \u6807\u51c6\u5e93\u5b9e\u73b0\u5185\u5b58\u7f13\u5b58\u673a\u5236\u7684\u8be6\u7ec6\u8fc7\u7a0b\u300b\uff0c\u9002\u7528\u4e8e\uff1a&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/07\/%e4%bd%bf%e7%94%a8-net%e6%a0%87%e5%87%86%e5%ba%93%e5%ae%9e%e7%8e%b0%e5%86%85%e5%ad%98%e7%bc%93%e5%ad%98%e6%9c%ba%e5%88%b6%e7%9a%84%e8%af%a6%e7%bb%86%e8%bf%87%e7%a8%8b\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[],"class_list":["post-621","post","type-post","status-publish","format-standard","hentry","category-asp-net"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=621"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/621\/revisions"}],"predecessor-version":[{"id":622,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/621\/revisions\/622"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}