{"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\nTest 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\nOutput: <\/strong> <\/p>\n\n\n\nName: 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":"\n
Golang: Redis client example - Welcome To Golang By Example<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n