{"id":87,"date":"2019-04-04T02:36:41","date_gmt":"2019-04-04T02:36:41","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=87"},"modified":"2019-11-12T18:51:00","modified_gmt":"2019-11-12T18:51:00","slug":"golang-redis-client-example","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/golang-redis-client-example\/","title":{"rendered":"Golang: Redis client example"},"content":{"rendered":"\n

First, we will write the Redis layer that will have a function to initialize the Redis client. The Redis client will only be created once and used throughout. In the below code, initialize function will initialize the Redis.<\/p>\n\n\n\n

package main\n\nimport (\n\t\"encoding\/json\"\n\t\"time\"\n\t\"github.com\/go-redis\/redis\"\n)\n\nvar (\n\tclient = &redisClient{}\n)\n\ntype redisClient struct {\n\tc *redis.Client\n}\n\n\/\/GetClient get the redis client\nfunc initialize() *redisClient {\n\tc := redis.NewClient(&redis.Options{\n\t\tAddr: \"127.0.0.1:6379\",\n\t})\n\n\tif err := c.Ping().Err(); err != nil {\n\t\tpanic(\"Unable to connect to redis \" + err.Error())\n\t}\n\tclient.c = c\n\treturn client\n}\n\n\/\/GetKey get key\nfunc (client *redisClient) getKey(key string, src interface{}) error {\n\tval, err := client.c.Get(key).Result()\n\tif err == redis.Nil || err != nil {\n\t\treturn err\n\t}\n\terr = json.Unmarshal([]byte(val), &src)\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn nil\n}\n\n\/\/SetKey set key\nfunc (client *redisClient) setKey(key string, value interface{}, expiration time.Duration) error {\n\tcacheEntry, err := json.Marshal(value)\n\tif err != nil {\n\t\treturn err\n\t}\n\terr = client.c.Set(key, cacheEntry, expiration).Err()\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn nil\n}<\/code><\/pre>\n\n\n\n

Test the Redis client layer<\/p>\n\n\n\n

package main\n\nimport (\n\t\"log\"\n\t\"time\"\n)\n\ntype valueEx struct {\n\tName  string\n\tEmail string\n}\n\nfunc main() {\n\tredisClient := initialize()\n\tkey1 := \"sampleKey\"\n\tvalue1 := &valueEx{Name: \"someName\", Email: \"someemail@abc.com\"}\n\terr := redisClient.setKey(key1, value1, time.Minute*1)\n\tif err != nil {\n\t\tlog.Fatalf(\"Error: %v\", err.Error())\n\t}\n\n\tvalue2 := &valueEx{}\n\terr = redisClient.getKey(key1, value2)\n\tif err != nil {\n\t\tlog.Fatalf(\"Error: %v\", err.Error())\n\t}\n\n\tlog.Printf(\"Name: %s\", value2.Name)\n\tlog.Printf(\"Email: %s\", value2.Email)\n}\n<\/code><\/pre>\n\n\n\n

Output: <\/strong> <\/p>\n\n\n\n

Name: someName\nEmail: someemail@abc.com   <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

First, we will write the Redis layer that will have a function to initialize the Redis client. The Redis client will only be created once and used throughout. In the below code,…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"class_list":["post-87","post","type-post","status-publish","format-standard","hentry","category-tech"],"yoast_head":"\nGolang: Redis client example - Welcome To Golang By Example<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/golangbyexamples.com\/golang-redis-client-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Golang: Redis client example - Welcome To Golang By Example\" \/>\n<meta property=\"og:description\" content=\"First, we will write the Redis layer that will have a function to initialize the Redis client. The Redis client will only be created once and used throughout. In the below code,...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/golangbyexamples.com\/golang-redis-client-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Welcome To Golang By Example\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-04T02:36:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-11-12T18:51:00+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/golangbyexamples.com\/golang-redis-client-example\/\",\"url\":\"https:\/\/golangbyexamples.com\/golang-redis-client-example\/\",\"name\":\"Golang: Redis client example - Welcome To Golang By Example\",\"isPartOf\":{\"@id\":\"https:\/\/golangbyexamples.com\/#website\"},\"datePublished\":\"2019-04-04T02:36:41+00:00\",\"dateModified\":\"2019-11-12T18:51:00+00:00\",\"author\":{\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f\"},\"breadcrumb\":{\"@id\":\"https:\/\/golangbyexamples.com\/golang-redis-client-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/golangbyexamples.com\/golang-redis-client-example\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/golangbyexamples.com\/golang-redis-client-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/golangbyexamples.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Golang: Redis client example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/golangbyexamples.com\/#website\",\"url\":\"https:\/\/golangbyexamples.com\/\",\"name\":\"Welcome To Golang By Example\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/golangbyexamples.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\/\/golangbyexamples.com\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Golang: Redis client example - Welcome To Golang By Example","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/golangbyexamples.com\/golang-redis-client-example\/","og_locale":"en_US","og_type":"article","og_title":"Golang: Redis client example - Welcome To Golang By Example","og_description":"First, we will write the Redis layer that will have a function to initialize the Redis client. The Redis client will only be created once and used throughout. In the below code,...","og_url":"https:\/\/golangbyexamples.com\/golang-redis-client-example\/","og_site_name":"Welcome To Golang By Example","article_published_time":"2019-04-04T02:36:41+00:00","article_modified_time":"2019-11-12T18:51:00+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/golangbyexamples.com\/golang-redis-client-example\/","url":"https:\/\/golangbyexamples.com\/golang-redis-client-example\/","name":"Golang: Redis client example - Welcome To Golang By Example","isPartOf":{"@id":"https:\/\/golangbyexamples.com\/#website"},"datePublished":"2019-04-04T02:36:41+00:00","dateModified":"2019-11-12T18:51:00+00:00","author":{"@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f"},"breadcrumb":{"@id":"https:\/\/golangbyexamples.com\/golang-redis-client-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/golangbyexamples.com\/golang-redis-client-example\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/golangbyexamples.com\/golang-redis-client-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/golangbyexamples.com\/"},{"@type":"ListItem","position":2,"name":"Golang: Redis client example"}]},{"@type":"WebSite","@id":"https:\/\/golangbyexamples.com\/#website","url":"https:\/\/golangbyexamples.com\/","name":"Welcome To Golang By Example","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/golangbyexamples.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/golangbyexamples.com\/author\/admin\/"}]}},"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paOs1b-1p","amp_validity":null,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/87","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/comments?post=87"}],"version-history":[{"count":8,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/87\/revisions"}],"predecessor-version":[{"id":478,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/87\/revisions\/478"}],"wp:attachment":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/media?parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/categories?post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/tags?post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}