﻿
{"id":15336,"date":"2020-04-19T01:07:42","date_gmt":"2020-04-19T01:07:42","guid":{"rendered":"https:\/\/www.gencayyildiz.com\/blog\/?p=15336"},"modified":"2020-04-19T01:07:42","modified_gmt":"2020-04-19T01:07:42","slug":"redis-yazi-serisi-11-redis-ile-distributedcache-kullanimi","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/redis-yazi-serisi-11-redis-ile-distributedcache-kullanimi\/","title":{"rendered":"Redis Yaz\u0131 Serisi 11 &#8211; Redis \u0130le DistributedCache Kullan\u0131m\u0131"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p><a href=\"https:\/\/www.gencayyildiz.com\/blog\/redis-yazi-dizisi\/\" rel=\"noopener noreferrer\" target=\"_blank\">Redis yaz\u0131 serimizin<\/a> bu makalesinde; uygulaman\u0131n, verisel trafi\u011fini minimize etmek ve performans ile birlikte \u00f6l\u00e7eklendirilebilirli\u011fini artt\u0131rmak i\u00e7in birden fazla instance ile aya\u011fa kald\u0131r\u0131lmas\u0131 sonucu in-memory cache kullan\u0131ld\u0131\u011f\u0131nda olas\u0131 veri tutars\u0131zl\u0131\u011f\u0131na mahal verebilecek durumlar\u0131n olu\u015fmas\u0131na kar\u015f\u0131l\u0131k verileri merkezi olarak store etmeyi tercih eden DistributedCache&#8217;i Redis ile inceleyece\u011fiz.<\/p>\n<p><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/04\/Redis-Yaz\u0131-Serisi-3-Distributed-Caching-Nedir.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/04\/Redis-Yaz\u0131-Serisi-3-Distributed-Caching-Nedir.png\" alt=\"Redis Yaz\u0131 Serisi 3 - Distributed Caching Nedir?\" width=\"553\" height=\"403\" class=\"aligncenter size-full wp-image-15094\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/04\/Redis-Yaz\u0131-Serisi-3-Distributed-Caching-Nedir.png 553w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/04\/Redis-Yaz\u0131-Serisi-3-Distributed-Caching-Nedir-300x219.png 300w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/a><\/p>\n<p>\u0130\u00e7erik olarak Redis veri t\u00fcrlerine \u00e7ok temas etmeden y\u00fczeysel cacheleme yapmam\u0131z\u0131 sa\u011flayan &#8216;StackExchangeRedisCache&#8217; y\u00f6ntemini uygulayaca\u011f\u0131m\u0131z\u0131, esas Redis&#8217;e tam teferruatl\u0131 y\u00f6netimi bir sonraki makalede ele alaca\u011f\u0131m\u0131z\u0131 bildirerek konuya ba\u015flayal\u0131m.<\/p>\n<h3>Kullan\u0131lacak K\u00fct\u00fcphane ve Servisler<\/h3>\n<p>Bir Asp.NET Core uygulamas\u0131nda Redis DistributedCache kullanabilmek i\u00e7in ilk olarak uygulamaya <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.Extensions.Caching.StackExchangeRedis\" rel=\"noopener noreferrer\" target=\"_blank\">Microsoft.Extensions.Caching.StackExchangeRedis<\/a> k\u00fct\u00fcphanesinin dahil edilmesi gerekmektedir. Ard\u0131ndan &#8216;Startup.cs&#8217; dosyas\u0131ndan a\u015fa\u011f\u0131daki gibi &#8216;AddStackExchangeRedisCache&#8217; mod\u00fcl\u00fc uygulamaya eklenmelidir. Burada dikkat edilmesi gereken husus Redis server\u0131n ayakta olmas\u0131d\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class Startup\r\n    {\r\n        .\r\n        .\r\n        .\r\n        public void ConfigureServices(IServiceCollection services)\r\n        {\r\n            .\r\n            .\r\n            .\r\n            services.AddStackExchangeRedisCache(options =&gt; options.Configuration = &quot;localhost:6379&quot;);\r\n            .\r\n            .\r\n            .\r\n        }\r\n        .\r\n        .\r\n        .\r\n    }\r\n<\/pre>\n<p>Bu i\u015flemden sonra art\u0131k uygulamada &#8216;IDistributedCache&#8217; interface&#8217;\u0131n\u0131 kullanarak a\u015fa\u011f\u0131daki \u00f6rnekte oldu\u011fu gibi dependency injection&#8217;dan servisimizi talep edebiliriz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    using Microsoft.Extensions.Caching.Distributed;\r\n    public class ProductController : Controller\r\n    {\r\n        IDistributedCache _distributedCache;\r\n        public ProductController(IDistributedCache distributedCache)\r\n        {\r\n            _distributedCache = distributedCache;\r\n        }\r\n    }\r\n<\/pre>\n<h3>Metotlar<\/h3>\n<p><strong>SetString<\/strong><br \/>\nMetinsel t\u00fcrde key-value tarz\u0131nda veri depolamas\u0131n\u0131 ger\u00e7ekle\u015ftiren metottur.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        public IActionResult CacheSetString()\r\n        {\r\n            _distributedCache.SetString(&quot;date&quot;, DateTime.Now.ToString());\r\n            return View();\r\n        }\r\n<\/pre>\n<p>ya da<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        public IActionResult CacheSetString()\r\n        {\r\n            _distributedCache.SetString(&quot;date&quot;, DateTime.Now.ToString(), new DistributedCacheEntryOptions\r\n            {\r\n                AbsoluteExpiration = DateTime.Now.AddSeconds(1200),\r\n                SlidingExpiration = TimeSpan.FromSeconds(60)\r\n            });\r\n            return View();\r\n        }\r\n<\/pre>\n<p><strong>GetString<\/strong><br \/>\nMetinsel t\u00fcrde depolanm\u0131\u015f verilerden key de\u011ferine kar\u015f\u0131l\u0131k value de\u011ferini d\u00f6nd\u00fcren fonksiyondur.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        public IActionResult CacheGetString()\r\n        {\r\n            string value = _distributedCache.GetString(&quot;date&quot;);\r\n            return View();\r\n        }\r\n<\/pre>\n<p><strong>Remove<\/strong><br \/>\nKey de\u011feri verilen datay\u0131 silen metottur.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        public IActionResult CacheRemove()\r\n        {\r\n            _distributedCache.Remove(&quot;date&quot;);\r\n            return View();\r\n        }\r\n<\/pre>\n<p><strong>Set<\/strong><br \/>\nCache&#8217;de binary olarak data tutmam\u0131z\u0131 sa\u011flayan fonksiyondur.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        public IActionResult CacheSet()\r\n        {\r\n            byte&#x5B;] dateByte = Encoding.UTF8.GetBytes(DateTime.Now.ToString());\r\n            _distributedCache.Set(&quot;date&quot;, dateByte);\r\n            return View();\r\n        }\r\n<\/pre>\n<p>ya da<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        public IActionResult CacheSet()\r\n        {\r\n            byte&#x5B;] dateByte = Encoding.UTF8.GetBytes(DateTime.Now.ToString());\r\n            _distributedCache.Set(&quot;date&quot;, dateByte, new DistributedCacheEntryOptions\r\n            {\r\n                AbsoluteExpiration = DateTime.Now.AddSeconds(1200),\r\n                SlidingExpiration = TimeSpan.FromSeconds(60)\r\n            });\r\n            return View();\r\n        }\r\n<\/pre>\n<p><strong>Get<\/strong><br \/>\nBinary olarak tutulan datay\u0131 geri binary olarak elde etmemizi sa\u011flayan fonksiyondur.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        public IActionResult CacheGet()\r\n        {\r\n            byte&#x5B;] dateByte = _distributedCache.Get(&quot;date&quot;);\r\n            string value = Encoding.UTF8.GetString(dateByte);\r\n            return View();\r\n        }\r\n<\/pre>\n<p><strong>Dosya Cacheleme<\/strong><br \/>\nA\u015fa\u011f\u0131da resim dosyas\u0131n\u0131n cachelendi\u011fi \u00f6rnek kod blo\u011funu inceleyebilirsiniz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        public IActionResult CacheFile()\r\n        {\r\n            string path = Path.Combine(Directory.GetCurrentDirectory(), &quot;wwwroot\/resim.jpg&quot;);\r\n            byte&#x5B;] fileByte = System.IO.File.ReadAllBytes(path);\r\n            _distributedCache.Set(&quot;file&quot;, fileByte);\r\n            return View();\r\n        }\r\n<\/pre>\n<p><strong>Cachelenmi\u015f Dosyay\u0131 Okuma<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        public IActionResult CacheFileRead()\r\n        {\r\n            byte&#x5B;] fileByte = _distributedCache.Get(&quot;file&quot;);\r\n            return File(fileByte, &quot;image\/jpg&quot;);\r\n        }\r\n<\/pre>\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, Redis yaz\u0131 serimizin bu makalesinde; uygulaman\u0131n, verisel trafi\u011fini minimize etmek ve performans ile birlikte \u00f6l\u00e7eklendirilebilirli\u011fini artt\u0131rmak i\u00e7in birden fazla instance ile aya\u011fa kald\u0131r\u0131lmas\u0131 sonucu in-memory cache kullan\u0131ld\u0131\u011f\u0131nda olas\u0131 veri tutars\u0131zl\u0131\u011f\u0131na mahal verebilecek durumlar\u0131n&#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":15094,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3383],"tags":[3441,3443,3440,3442,3384,3438,3439,3437],"class_list":["post-15336","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-redis","tag-addstackexchangerediscache","tag-cachelenmis-dosyayi-elde-etme","tag-distributedcache","tag-docya-cacheleme","tag-redis","tag-redis-distributedcache","tag-redis-distributedcache-kullanimi","tag-redis-ile-distributedcache-kullanimi"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/15336","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=15336"}],"version-history":[{"count":12,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/15336\/revisions"}],"predecessor-version":[{"id":15348,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/15336\/revisions\/15348"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/15094"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=15336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=15336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=15336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}