﻿
{"id":15350,"date":"2020-04-20T18:01:22","date_gmt":"2020-04-20T18:01:22","guid":{"rendered":"https:\/\/www.gencayyildiz.com\/blog\/?p=15350"},"modified":"2020-04-20T18:01:22","modified_gmt":"2020-04-20T18:01:22","slug":"redis-yazi-serisi-12-stackexchange-redis-api-ile-redis-kullanimi","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/redis-yazi-serisi-12-stackexchange-redis-api-ile-redis-kullanimi\/","title":{"rendered":"Redis Yaz\u0131 Serisi 12 &#8211; StackExchange.Redis API \u0130le Redis 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\/\" target=\"_blank\" rel=\"noopener noreferrer\">Redis yaz\u0131 serimizin<\/a> bir \u00f6nceki <a href=\"https:\/\/www.gencayyildiz.com\/blog\/redis-yazi-serisi-11-redis-ile-distributedcache-kullanimi\/\" target=\"_blank\" rel=\"noopener noreferrer\">Redis \u0130le DistributedCache Kullan\u0131m\u0131<\/a> ba\u015fl\u0131kla yay\u0131nlad\u0131\u011f\u0131m\u0131z makalemizde Redis ile DistributedCache&#8217;i y\u00fczeysel olarak nas\u0131l kulland\u0131\u011f\u0131m\u0131z\u0131 incelemi\u015ftik. Bu i\u00e7eri\u011fimizde ise <em>StackExchange.Redis<\/em> ile datalar\u0131m\u0131z\u0131 Redis t\u00fcrlerinde tutarak Redis&#8217;i daha hakim nas\u0131l kullanaca\u011f\u0131m\u0131z\u0131 inceleyece\u011fiz.<\/p>\n<h3>K\u00fct\u00fcphane Entegrasyonu ve Servis \u0130n\u015fas\u0131<\/h3>\n<p>\u0130lk olarak Asp.NET Core uygulaman\u0131za <a href=\"https:\/\/www.nuget.org\/packages\/StackExchange.Redis\/\" rel=\"noopener noreferrer\" target=\"_blank\">StackExchange.Redis<\/a> k\u00fct\u00fcphanesini entegre ediniz. Ard\u0131ndan bu k\u00fct\u00fcphaneyi kullanarak a\u015fa\u011f\u0131dakine benzer bir servis olu\u015fturunuz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    using StackExchange.Redis;\r\n    public class RedisService\r\n    {\r\n        ConnectionMultiplexer connectionMultiplexer;\r\n        public void Connect() =&gt; connectionMultiplexer = ConnectionMultiplexer.Connect(&quot;localhost:1453&quot;);\r\n        public IDatabase GetDb(int db) =&gt; connectionMultiplexer.GetDatabase(db);\r\n    }\r\n<\/pre>\n<p>Burada &#8216;Connect&#8217; metodu i\u00e7erisinde &#8216;ConnectionMultiplexer&#8217; s\u0131n\u0131f\u0131yla ilgili Redis sunucusuna ba\u011flant\u0131 ger\u00e7ekle\u015ftirilmekte ve ard\u0131ndan &#8216;GetDb&#8217; metodu ile de ba\u011flant\u0131 ger\u00e7ekle\u015ftirilmi\u015f ilgili s\u0131n\u0131f \u00fczerinden Redis sunucusundaki veritabanlar\u0131 \u00e7a\u011fr\u0131larak elde edilmektedir.<\/p>\n<p>Bizler bu servisi kullanabilmek i\u00e7in dependency injection provider&#8217;\u0131na singleton olarak ekleyip tekbir nesne olarak y\u00f6netebiliriz. \u015e\u00f6yle ki; uygulaman\u0131n &#8216;Startup.cs&#8217; dosyas\u0131nda<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class Startup\r\n    {\r\n        public void ConfigureServices(IServiceCollection services)\r\n        {\r\n            .\r\n            .\r\n            .\r\n            services.AddSingleton&lt;RedisService, RedisService&gt;();\r\n            .\r\n            .\r\n            .\r\n        }\r\n\r\n        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, RedisService redisService)\r\n        {\r\n            .\r\n            .\r\n            .\r\n            redisService.Connect();\r\n            .\r\n            .\r\n            .\r\n        }\r\n    }\r\n<\/pre>\n<p>\u015feklinde bir \u00e7al\u0131\u015fma yapmam\u0131z yeterli olacakt\u0131r. Yap\u0131ya dikkat ederseniz uygulamada 8. sat\u0131rda provider&#8217;a singleton olarak dahil edilen &#8216;RedisService&#8217; nesnesi, 14. sat\u0131rda dependency injection&#8217;dan talep edilmekte ve 19. sat\u0131rda da &#8216;Connect&#8217; fonksiyonu tetiklenerek Redis sunucusuyla hali haz\u0131rda ba\u011flant\u0131s\u0131 kurulmu\u015f vaziyete getirilmektedir. Nihayetinde ilgili servisin nesnesi singleton olaca\u011f\u0131 i\u00e7in bundan sonraki ilgili nesneye dair yap\u0131lacak t\u00fcm taleplerde ba\u011flant\u0131s\u0131 ger\u00e7ekle\u015ftirilmi\u015f bu nesne g\u00f6nderilecektir.<\/p>\n<h3>StackExchange.Redis Kullan\u0131m\u0131<\/h3>\n<p>Olu\u015fturulan servisi dependency injection ile a\u015fa\u011f\u0131daki gibi talep ettikten sonra Redis t\u00fcrlerinden \u00f6rneklendirmelere ge\u00e7ebiliriz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class RedisController : Controller\r\n    {\r\n        RedisService _redisService;\r\n        public RedisController(RedisService redisService)\r\n        {\r\n            _redisService = redisService;\r\n        }\r\n    }\r\n<\/pre>\n<h4 style=\"color:purple;\">Redis String<\/h4>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n            IDatabase database = _redisService.GetDb(1);\r\n            \/\/SET\r\n            database.StringSet(&quot;name&quot;, &quot;gencay&quot;);\r\n            \/\/GET\r\n            string value_get = database.StringGet(&quot;name&quot;);\r\n            Console.WriteLine($&quot;Get : {value_get}&quot;);\r\n            \/\/APPEND\r\n            database.StringAppend(&quot;name&quot;, &quot; y\u0131ld\u0131z&quot;);\r\n            string value_append = database.StringGet(&quot;name&quot;);\r\n            Console.WriteLine($&quot;Append : {value_append}&quot;);\r\n            \/\/INCR\r\n            database.StringSet(&quot;count&quot;, 1);\r\n            long value_count = database.StringIncrement(&quot;count&quot;);\r\n            Console.WriteLine($&quot;Incr : {value_count}&quot;);\r\n            \/\/GETRANGE\r\n            string value_getrange = database.StringGetRange(&quot;name&quot;, 1, 2);\r\n            Console.WriteLine($&quot;Getrange : {value_getrange}&quot;);\r\n<\/pre>\n<p><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/04\/Redis-Yaz\u0131-Serisi-12-StackExchange.Redis-API-\u0130le-Redis-Kullan\u0131m\u0131.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/04\/Redis-Yaz\u0131-Serisi-12-StackExchange.Redis-API-\u0130le-Redis-Kullan\u0131m\u0131.jpg\" alt=\"Redis Yaz\u0131 Serisi 12 - StackExchange.Redis API \u0130le Redis Kullan\u0131m\u0131\" width=\"181\" height=\"66\" class=\"aligncenter size-full wp-image-15357\" \/><\/a><\/p>\n<h4 style=\"color:purple;\">Redis List<\/h4>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n            IDatabase database = _redisService.GetDb(1);\r\n            \/\/LPUSH\r\n            database.ListLeftPush(&quot;ogrenciler&quot;, &quot;furkan&quot;);\r\n            database.ListLeftPush(&quot;ogrenciler&quot;, &quot;hakan&quot;);\r\n            database.ListLeftPush(&quot;ogrenciler&quot;, &quot;berrak&quot;);\r\n            \/\/RPUSH\r\n            database.ListRightPush(&quot;ogrenciler&quot;, &quot;emine&quot;);\r\n            database.ListRightPush(&quot;ogrenciler&quot;, &quot;kevser&quot;);\r\n            database.ListRightPush(&quot;ogrenciler&quot;, &quot;murat&quot;);\r\n            \/\/LRANGE\r\n            RedisValue&#x5B;] values = database.ListRange(&quot;ogrenciler&quot;, 0, -1);\r\n            int count = 1;\r\n            values.ToList().ForEach(o =&gt; Console.WriteLine($&quot;\u00f6\u011frenci {count++} - {o}&quot;));\r\n            \/\/LPOP\r\n            string left_pop = database.ListLeftPop(&quot;ogrenciler&quot;);\r\n            Console.WriteLine($&quot;LPOP : {left_pop}&quot;);\r\n            \/\/RPOP\r\n            string right_pop = database.ListRightPop(&quot;ogrenciler&quot;);\r\n            Console.WriteLine($&quot;RPOP : {right_pop}&quot;);\r\n            \/\/LINDEX\r\n            string index_value = database.ListGetByIndex(&quot;ogrenciler&quot;, 2);\r\n            Console.WriteLine($&quot;LINDEX : {index_value}&quot;);\r\n<\/pre>\n<p><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/04\/Redis-Yaz\u0131-Serisi-12-StackExchange.Redis-API-\u0130le-Redis-Kullan\u0131m\u0131-1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/04\/Redis-Yaz\u0131-Serisi-12-StackExchange.Redis-API-\u0130le-Redis-Kullan\u0131m\u0131-1.jpg\" alt=\"Redis Yaz\u0131 Serisi 12 - StackExchange.Redis API \u0130le Redis Kullan\u0131m\u0131\" width=\"153\" height=\"146\" class=\"aligncenter size-full wp-image-15360\" \/><\/a><\/p>\n<h4 style=\"color:purple;\">Redis Set<\/h4>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n            IDatabase database = _redisService.GetDb(1);\r\n            \/\/SADD\r\n            database.SetAdd(&quot;color&quot;, &quot;blue&quot;);\r\n            database.SetAdd(&quot;color&quot;, &quot;red&quot;);\r\n            database.SetAdd(&quot;color&quot;, &quot;white&quot;);\r\n            database.SetAdd(&quot;color&quot;, &quot;black&quot;);\r\n            \/\/SETMEMBERS\r\n            RedisValue&#x5B;] values = database.SetMembers(&quot;color&quot;);\r\n            values.ToList().ForEach(c =&gt; Console.WriteLine(c));\r\n            \/\/SREM\r\n            database.SetRemove(&quot;color&quot;, &quot;white&quot;);\r\n            Console.WriteLine(&quot;****&quot;);\r\n            values = database.SetMembers(&quot;color&quot;);\r\n            values.ToList().ForEach(c =&gt; Console.WriteLine(c));\r\n<\/pre>\n<p><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/04\/Redis-Yaz\u0131-Serisi-12-StackExchange.Redis-API-\u0130le-Redis-Kullan\u0131m\u0131-2.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/04\/Redis-Yaz\u0131-Serisi-12-StackExchange.Redis-API-\u0130le-Redis-Kullan\u0131m\u0131-2.jpg\" alt=\"Redis Yaz\u0131 Serisi 12 - StackExchange.Redis API \u0130le Redis Kullan\u0131m\u0131\" width=\"50\" height=\"126\" class=\"aligncenter size-full wp-image-15362\" \/><\/a><\/p>\n<h4 style=\"color:purple;\">Redis Sorted Set<\/h4>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n            IDatabase database = _redisService.GetDb(1);\r\n            \/\/ZADD\r\n            database.SortedSetAdd(&quot;esya&quot;, &quot;kalem&quot;, 5);\r\n            database.SortedSetAdd(&quot;esya&quot;, &quot;silgi&quot;, 10);\r\n            database.SortedSetAdd(&quot;esya&quot;, &quot;defter&quot;, 15);\r\n            database.SortedSetAdd(&quot;esya&quot;, &quot;ka\u011f\u0131t&quot;, 2);\r\n            \/\/ZRANGE\r\n            RedisValue&#x5B;] values = database.SortedSetRangeByRank(&quot;esya&quot;, 0, -1);\r\n            values.ToList().ForEach(e =&gt; Console.WriteLine(e));\r\n            \/\/ZRANGE - WITHSCORES\r\n            SortedSetEntry&#x5B;] values2 = database.SortedSetRangeByRankWithScores(&quot;esya&quot;, 0, -1);\r\n            Console.WriteLine(&quot;****&quot;);\r\n            values2.ToList().ForEach(e =&gt; Console.WriteLine(e));\r\n            \/\/ZREM\r\n            database.SortedSetRemove(&quot;esya&quot;, &quot;kalem&quot;);\r\n            Console.WriteLine(&quot;****&quot;);\r\n            values = database.SortedSetRangeByRank(&quot;esya&quot;, 0, -1);\r\n            values.ToList().ForEach(e =&gt; Console.WriteLine(e));\r\n<\/pre>\n<p><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/04\/Redis-Yaz\u0131-Serisi-12-StackExchange.Redis-API-\u0130le-Redis-Kullan\u0131m\u0131-3.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/04\/Redis-Yaz\u0131-Serisi-12-StackExchange.Redis-API-\u0130le-Redis-Kullan\u0131m\u0131-3.jpg\" alt=\"Redis Yaz\u0131 Serisi 12 - StackExchange.Redis API \u0130le Redis Kullan\u0131m\u0131\" width=\"86\" height=\"206\" class=\"aligncenter size-full wp-image-15364\" \/><\/a><\/p>\n<h4 style=\"color:purple;\">Redis Hash<\/h4>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n            IDatabase database = _redisService.GetDb(1);\r\n            \/\/HMSET\r\n            database.HashSet(&quot;sozluk&quot;, &quot;pen&quot;, &quot;kalem&quot;);\r\n            database.HashSet(&quot;sozluk&quot;, &quot;mouse&quot;, &quot;fare&quot;);\r\n            database.HashSet(&quot;sozluk&quot;, &quot;dog&quot;, &quot;k\u00f6pek&quot;);\r\n            database.HashSet(&quot;sozluk&quot;, &quot;cat&quot;, &quot;kedi&quot;);\r\n            \/\/HMGET\r\n            string value_cat = database.HashGet(&quot;sozluk&quot;, &quot;cat&quot;);\r\n            Console.WriteLine(value_cat);\r\n            \/\/HDEL\r\n            database.HashDelete(&quot;sozluk&quot;, &quot;mouse&quot;);\r\n            \/\/HGETALL\r\n            HashEntry&#x5B;] values = database.HashGetAll(&quot;sozluk&quot;);\r\n            values.ToList().ForEach(h =&gt; Console.WriteLine(h));\r\n<\/pre>\n<p><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/04\/Redis-Yaz\u0131-Serisi-12-StackExchange.Redis-API-\u0130le-Redis-Kullan\u0131m\u0131-4.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/04\/Redis-Yaz\u0131-Serisi-12-StackExchange.Redis-API-\u0130le-Redis-Kullan\u0131m\u0131-4.jpg\" alt=\"Redis Yaz\u0131 Serisi 12 - StackExchange.Redis API \u0130le Redis Kullan\u0131m\u0131\" width=\"91\" height=\"66\" class=\"aligncenter size-full wp-image-15365\" \/><\/a><\/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 Redis yaz\u0131 serimizin bir \u00f6nceki Redis \u0130le DistributedCache Kullan\u0131m\u0131 ba\u015fl\u0131kla yay\u0131nlad\u0131\u011f\u0131m\u0131z makalemizde Redis ile DistributedCache&#8217;i y\u00fczeysel olarak nas\u0131l kulland\u0131\u011f\u0131m\u0131z\u0131 incelemi\u015ftik. Bu i\u00e7eri\u011fimizde ise StackExchange.Redis ile datalar\u0131m\u0131z\u0131 Redis t\u00fcrlerinde tutarak Redis&#8217;i daha hakim nas\u0131l&#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":15045,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3383],"tags":[3446,3384,3436,3432,3434,3435,3433,3444,3445,3447,3448],"class_list":["post-15350","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-redis","tag-connectionmultiplexer","tag-redis","tag-redis-hash","tag-redis-list","tag-redis-set","tag-redis-sorted-set","tag-redis-string","tag-stack-exchange-api","tag-stack-exchange-api-ile-redis-kullanimi","tag-stackexchange-redis","tag-stackexchange-redis-api"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/15350","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=15350"}],"version-history":[{"count":11,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/15350\/revisions"}],"predecessor-version":[{"id":15366,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/15350\/revisions\/15366"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/15045"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=15350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=15350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=15350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}