﻿
{"id":11055,"date":"2019-05-21T22:15:00","date_gmt":"2019-05-21T22:15:00","guid":{"rendered":"https:\/\/www.gencayyildiz.com\/blog\/?p=11055"},"modified":"2019-05-21T22:15:00","modified_gmt":"2019-05-21T22:15:00","slug":"asp-net-corede-in-memory-cache","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/asp-net-corede-in-memory-cache\/","title":{"rendered":"Asp.NET Core&#8217;de In-Memory Cache"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Bir uygulaman\u0131n performans\u0131n\u0131 \u00f6nemli \u00f6l\u00e7\u00fcde artt\u0131rabilecek en m\u00fchim yap\u0131lanmalardan biriside \u00f6nbelle\u011fe(Cache) alma sistemidir. Son kullan\u0131c\u0131ya g\u00f6sterilen veriler aras\u0131ndan nadiren g\u00fcncellenen veya geni\u015f aral\u0131klarla tazelenen verileri her istek neticesinde veritaban\u0131 \u00fczerinden elde ederek kullan\u0131c\u0131ya sunmaktansa, ilgili verileri varsa periyodik s\u00fcrelere uygun bir aral\u0131kta yahut taraf\u0131n\u0131zca belirlenmi\u015f bir zaman s\u0131n\u0131r\u0131nda \u00f6nbelle\u011fe alarak b\u00f6ylece orjinal veriyi tekrar elde etmek yerine daha h\u0131zl\u0131 sonu\u00e7lar \u00fcretebilirsiniz.<\/p>\n<p>Asp.NET Core, mod\u00fcler olarak i\u00e7erisinde birka\u00e7 farkl\u0131 \u00f6nbellek yap\u0131lanmas\u0131n\u0131 desteklemektedir. Bizler bu i\u00e7eri\u011fimizde, bu \u00f6nbellek yap\u0131lanmalar\u0131ndan en basit olan\u0131 In-Memory Cache yap\u0131lanmas\u0131n\u0131 inceleyece\u011fiz.<\/p>\n<p>In-Memory Cache, \u00f6nbellekleme s\u00fcrecinde web uygulamas\u0131n\u0131n bar\u0131nd\u0131r\u0131ld\u0131\u011f\u0131 sunucunun belle\u011fini kullanmaktad\u0131r ve &#8220;IMemoryCache&#8221; aray\u00fcz\u00fc arac\u0131l\u0131\u011f\u0131yla uygulamada \u00e7al\u0131\u015fmaktad\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public interface IMemoryCache : IDisposable\r\n    {\r\n        ICacheEntry CreateEntry(object key);\r\n        void Remove(object key);\r\n        bool TryGetValue(object key, out object value);\r\n    }\r\n<\/pre>\n<p>Yukar\u0131da &#8220;IMemoryCache&#8221; aray\u00fcz\u00fcn\u00fcn i\u00e7eri\u011fini incelerseniz e\u011fer &#8220;<em>CreateEntry<\/em>&#8220;, &#8220;<em>Remove<\/em>&#8221; ve &#8220;<em>TryGetValue<\/em>&#8221; metotlar\u0131 mevcuttur. Bu metotlar\u0131n s\u0131ras\u0131yla i\u015flevselliklerini incelersek e\u011fer;<\/p>\n<ul>\n<li>\n<strong>CreateEntry<\/strong><br \/>\nMemory Cache&#8217;de bir alan olu\u015fturur ve i\u00e7erisine de\u011fer atamam\u0131z\u0131 sa\u011flar.\n<\/li>\n<li>\n<strong>Remove<\/strong><br \/>\nMemory Cache&#8217;de olu\u015fturulan alan\u0131 temizlelemizi sa\u011flar.\n<\/li>\n<li>\n<strong>TryGetValue<\/strong><br \/>\nMemory Cache&#8217;de belirtilen alanda varsa de\u011feri getirir.\n<\/li>\n<\/ul>\n<p>Asp.NET Core uygulamalar\u0131nda In-Memory Cache \u00f6zelli\u011fini kullanabilmek i\u00e7in ilgili yap\u0131n\u0131n mod\u00fcl\u00fcn\u00fcn uygulamaya service olarak eklenmesi gerekmektedir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class Startup\r\n    {\r\n        public Startup(IConfiguration configuration)\r\n        {\r\n            Configuration = configuration;\r\n        }\r\n        public IConfiguration Configuration { get; }\r\n\r\n        public void ConfigureServices(IServiceCollection services)\r\n        {\r\n            services.AddMvc();\r\n            services.AddMemoryCache();\r\n        }\r\n\r\n        public void Configure(IApplicationBuilder app, IHostingEnvironment env)\r\n        {\r\n            app.UseMvc();\r\n        }\r\n    }\r\n<\/pre>\n<p>Bu i\u015flemden sonra Memory Cache&#8217;i kullanabilmek i\u00e7in ilgili controllerda IMemoryCache aray\u00fcz\u00fcn\u00fc inject etmemiz yeterlidir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    &#x5B;Route(&quot;api\/&#x5B;controller]&quot;)]\r\n    &#x5B;ApiController]\r\n    public class PersonelController : ControllerBase\r\n    {\r\n        IMemoryCache _memoryCache;\r\n        NorthwindContext _northwindContext;\r\n        public PersonelController(IMemoryCache memoryCache, NorthwindContext northwindContext)\r\n        {\r\n            _memoryCache = memoryCache;\r\n            _northwindContext = northwindContext;\r\n        }\r\n    }\r\n<\/pre>\n<p>Art\u0131k Cache&#8217;lenecek verileri Memory&#8217;e atabiliriz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    &#x5B;Route(&quot;api\/&#x5B;controller]&quot;)]\r\n    &#x5B;ApiController]\r\n    public class PersonelController : ControllerBase\r\n    {\r\n        IMemoryCache _memoryCache;\r\n        NorthwindContext _northwindContext;\r\n        public PersonelController(IMemoryCache memoryCache, NorthwindContext northwindContext)\r\n        {\r\n            _memoryCache = memoryCache;\r\n            _northwindContext = northwindContext;\r\n        }\r\n        &#x5B;HttpGet(&quot;&#x5B;action]&quot;)]\r\n        public ActionResult&lt;IEnumerable&lt;Personeller&gt;&gt; Get()\r\n        {\r\n            const string key = &quot;personeller&quot;;\r\n\r\n            if \\(_memoryCache.TryGetValue(key, out object list))\r\n                return Ok(list);\r\n            var personelList = _northwindContext.Personeller.Select(x =&gt; new\r\n            {\r\n                x.Adi,\r\n                x.SoyAdi,\r\n                x.Unvan\r\n            }).ToList();\r\n            _memoryCache.Set(key, personelList, new MemoryCacheEntryOptions\r\n            {\r\n                AbsoluteExpiration = DateTime.Now.AddSeconds(20),\r\n                Priority = CacheItemPriority.Normal\r\n            });\r\n            return Ok(personelList);\r\n        }\r\n    }\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funu incelerseniz e\u011fer &#8220;Set&#8221; metodunun 3. parametresinde &#8220;MemoryCacheEntryOptions&#8221; tipinden bir nesne istenmektedir. Bu nesne belle\u011fe at\u0131lan verinin temel konfig\u00fcrasyonunu sa\u011flamakta, ne kadar s\u00fcre bellekte tutulaca\u011f\u0131n\u0131 ve memory \u015fi\u015fti\u011finde cache objelerini hangi \u00f6nceli\u011fe g\u00f6re silece\u011fini vs. bilgilerini tutmaktad\u0131r. Bunlar\u0131n d\u0131\u015f\u0131nda; yukar\u0131da yap\u0131lan i\u015flem neticesinde ilk request ile orjinal veri elde edilecek ve Memory Cache&#8217;e &#8220;20&#8221; saniyeli\u011fine at\u0131lacakt\u0131r. Bu &#8220;20&#8221; saniye boyunca yap\u0131lan t\u00fcm requestler ilgili source&#8217;e gitmeksizin cacheden kar\u015f\u0131lanacakt\u0131r. Expire s\u00fcresi dolan datalar ilgili cacheden temizlenecektir.<\/p>\n<p>T\u00fcm bu i\u015flemlerin d\u0131\u015f\u0131nda Cache data set edildi\u011fi gibi remove&#8217;da edilebilmektedir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n_memoryCache.Remove(key);\r\n<\/pre>\n<p>Evet g\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere bu kadar basit&#8230;<\/p>\n<p>\u0130lgilenenlerin faydalanmas\u0131 dile\u011fiyle&#8230;<br \/>\nSonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Bir uygulaman\u0131n performans\u0131n\u0131 \u00f6nemli \u00f6l\u00e7\u00fcde artt\u0131rabilecek en m\u00fchim yap\u0131lanmalardan biriside \u00f6nbelle\u011fe(Cache) alma sistemidir. Son kullan\u0131c\u0131ya g\u00f6sterilen veriler aras\u0131ndan nadiren g\u00fcncellenen veya geni\u015f aral\u0131klarla tazelenen verileri her istek neticesinde veritaban\u0131 \u00fczerinden elde ederek kullan\u0131c\u0131ya&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":9596,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2668,2656,2811,2832],"tags":[2679,2868,2867,2865,2866,2869],"class_list":["post-11055","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-asp-net-core-2","category-asp-net-core-2-mvc","category-asp-net-core-2-1","category-asp-net-core-2-2","tag-asp-net-core","tag-asp-net-core-cache","tag-asp-net-core-in-memory-cache","tag-imemorycache","tag-in-memory-cache","tag-memorycacheentryoptions"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/11055","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=11055"}],"version-history":[{"count":30,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/11055\/revisions"}],"predecessor-version":[{"id":11086,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/11055\/revisions\/11086"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/9596"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=11055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=11055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=11055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}