{"id":4428,"date":"2016-11-06T19:26:05","date_gmt":"2016-11-06T19:26:05","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4428"},"modified":"2016-11-06T19:26:05","modified_gmt":"2016-11-06T19:26:05","slug":"redis-service-and-client","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/redis-service-and-client\/","title":{"rendered":"Redis service and client"},"content":{"rendered":"<p><a href=\"http:\/\/redis.io\/\" target=\"_blank\">Redis<\/a> is an in memory store\/cache, key\/value store. Luckily there&#8217;s a Docker image for this. <\/p>\n<p>Let&#8217;s run an instance of Redis via Docker on an Ubuntu server<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\ndocker run --name myredis -d -p 6379:6379 redis\r\n<\/pre>\n<p>Oh, how I love Docker (and ofcourse the community who create these images). This will run Redis and return immediately to your host&#8217;s command prompt (i.e. we do not go into the instance of Redis).<\/p>\n<p>To run the redis client we&#8217;ll need to switch to the instance of the Docker container running Redis and then run the redis command line interface, thus<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\ndocker exec -it myredis bash\r\nredis-cli\r\n<\/pre>\n<p>We&#8217;ll use this CLI later to view data in the cache.<\/p>\n<p><strong>C# client<\/strong><\/p>\n<p>There are several Redis client libraries available for .NET\/C#, I&#8217;m going to go with <a href=\"https:\/\/github.com\/ServiceStack\/ServiceStack.Redis\" target=\"_blank\">ServiceStack.Redis<\/a>, mainly because I&#8217;ve been using ServiceStack recently. So create a Console application, add the nuget packages for ServiceStack.Redis and now add the following code<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class Person\r\n{\r\n   public string FirstName { get; set; }\r\n   public string LastName { get; set; }\r\n}\r\n\r\nclass Program\r\n{\r\n   static void Main(string&#x5B;] args)\r\n   {\r\n      var client = new RedisClient(&quot;redis:\/\/xxx.xxx.xxx.xxx:6379&quot;);\r\n      client.Add(&quot;1234&quot;, new Person {FirstName = &quot;Scooby&quot;, LastName = &quot;Doo&quot;});\r\n      client.Save();\r\n   }\r\n}\r\n<\/pre>\n<p>Obviously change xxx.xxx.xxx.xxx to your server ip address.<\/p>\n<p>This code will simply write the Person object to the store against the key 1234. If you have the redis-cli running then you can type<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nget 1234\r\n<\/pre>\n<p>this should result in the following result<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&quot;{\\&quot;FirstName\\&quot;:\\&quot;Scooby\\&quot;,\\&quot;LastName\\&quot;:\\&quot;Doo\\&quot;}&quot;\r\n<\/pre>\n<p>Ofcourse, we now need to use the ServiceStack.Redis client to read our data back. Just use this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar p = client.Get&lt;Person&gt;(&quot;1234&quot;);\r\n<\/pre>\n<p><strong>Security<\/strong><\/p>\n<p>By default Redis has no security set up, hence we didn&#8217;t need to specify a user name and password. Obviously in a production environment we&#8217;d need to implement such security (or if using Redis via a cloud provider such as Azure). <\/p>\n<p>For our instance we can secure Redis as a whole using the command <a href=\"http:\/\/redis.io\/commands\/auth\" target=\"_blank\">AUTH<\/a>. So from redis-cli run<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nCONFIG SET requirepass &quot;password&quot;\r\nAUTH &quot;password&quot;\r\n<\/pre>\n<p>If you run AUTH &#8220;password&#8221; and get <em>Err Client sent AUTH, but no password is set<\/em> you&#8217;ll need the CONFIG line, otherwise the AUTH line should work fine. Our client application will need the following changes to the URL<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar client = new RedisClient(&quot;redis:\/\/password@xxx.xxx.xxx.xxx:6379&quot;);\r\n<\/pre>\n<p>To remove the password (if you need to) simple type the following from the redis-cli<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nCONFIG SET requirepass &quot;&quot;\r\n<\/pre>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"https:\/\/github.com\/ServiceStack\/ServiceStack.Redis\" target=\"_blank\">https:\/\/github.com\/ServiceStack\/ServiceStack.Redis<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis is an in memory store\/cache, key\/value store. Luckily there&#8217;s a Docker image for this. Let&#8217;s run an instance of Redis via Docker on an Ubuntu server docker run &#8211;name myredis -d -p 6379:6379 redis Oh, how I love Docker (and ofcourse the community who create these images). This will run Redis and return immediately [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[102,142,140],"tags":[],"class_list":["post-4428","post","type-post","status-publish","format-standard","hentry","category-docker","category-redis","category-ubuntu"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=4428"}],"version-history":[{"count":6,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4428\/revisions"}],"predecessor-version":[{"id":11723,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4428\/revisions\/11723"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}