Skip to content

Commit be3e5fb

Browse files
author
Akshay Ram
committed
Add redis client for caching
1 parent 397c6a9 commit be3e5fb

File tree

7 files changed

+132
-12
lines changed

7 files changed

+132
-12
lines changed

.env.example

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
GITHUB_TOKEN=<GITHUB_TOKEN>
22
GITHUB_USER=akshay5995
33
SITE_NAME=Akshay Ram Vignesh
4-
DEV_BLOG_SITE=https://akshayramvignesh.dev
4+
DEV_BLOG_SITE=https://akshayramvignesh.dev
5+
REDIS_HOST=redis

k8s-deployment.yml deployments/go-deployment.yml

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ spec:
1919
imagePullPolicy: Always
2020
ports:
2121
- containerPort: 4001 # Should match the port number that the Go application listens on
22+
env: # Environment variables passed to the container
23+
- name: REDIS_HOST
24+
value: redis-master
25+
- name: REDIS_PORT
26+
value: "6379"
2227
livenessProbe: # To check the health of the Pod
2328
httpGet:
2429
path: /ping

deployments/redis-master.yml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
apiVersion: apps/v1 # API version
3+
kind: Deployment
4+
metadata:
5+
name: redis-master # Unique name for the deployment
6+
labels:
7+
app: redis # Labels to be applied to this deployment
8+
spec:
9+
selector:
10+
matchLabels: # This deployment applies to the Pods matching these labels
11+
app: redis
12+
role: master
13+
tier: backend
14+
replicas: 1 # Run a single pod in the deployment
15+
template: # Template for the pods that will be created by this deployment
16+
metadata:
17+
labels: # Labels to be applied to the Pods in this deployment
18+
app: redis
19+
role: master
20+
tier: backend
21+
spec: # Spec for the container which will be run inside the Pod.
22+
containers:
23+
- name: master
24+
image: redis
25+
resources:
26+
requests:
27+
cpu: 100m
28+
memory: 100Mi
29+
ports:
30+
- containerPort: 6379
31+
---
32+
apiVersion: v1
33+
kind: Service # Type of Kubernetes resource
34+
metadata:
35+
name: redis-master # Name of the Kubernetes resource
36+
labels: # Labels that will be applied to this resource
37+
app: redis
38+
role: master
39+
tier: backend
40+
spec:
41+
ports:
42+
- port: 6379 # Map incoming connections on port 6379 to the target port 6379 of the Pod
43+
targetPort: 6379
44+
selector: # Map any Pod with the specified labels to this service
45+
app: redis
46+
role: master
47+
tier: backend

docker-compose.yml

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,14 @@ services:
33
web:
44
build: .
55
ports:
6-
- "4001:4001"
6+
- "4001:4001"
7+
depends_on:
8+
- redis
9+
redis:
10+
image: redis:3.2-alpine
11+
volumes:
12+
- redis_data:/data
13+
ports:
14+
- "6379:6379"
15+
volumes:
16+
redis_data:

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/gin-contrib/sse v0.1.0 // indirect
1111
github.com/gin-contrib/static v0.0.0-20190913125243-df30d4057ba1
1212
github.com/gin-gonic/gin v1.4.0
13+
github.com/go-redis/redis/v7 v7.0.0-beta.4
1314
github.com/golang/protobuf v1.3.2 // indirect
1415
github.com/google/go-github/v28 v28.1.1
1516
github.com/joho/godotenv v1.3.0

go.sum

+14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
1212
github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
1313
github.com/foolin/goview v0.2.0 h1:ovIVJmi2Lui+0obJ+NUhf2TN9mmV4hx5FA99oX1yH6U=
1414
github.com/foolin/goview v0.2.0/go.mod h1:hmQ++dSxBx8Uod3ZIpH+g2l1NSg/1Yc3kfNuLfY0EoI=
15+
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
1516
github.com/gin-contrib/cors v1.3.0 h1:PolezCc89peu+NgkIWt9OB01Kbzt6IP0J/JvkG6xxlg=
1617
github.com/gin-contrib/cors v1.3.0/go.mod h1:artPvLlhkF7oG06nK8v3U8TNz6IeX+w1uzCSEId5/Vc=
1718
github.com/gin-contrib/logger v0.0.2 h1:qT6qOR9/mp9hrHAgTEVxpjvS3anQtiUTtzJhf+NlBQM=
@@ -24,6 +25,9 @@ github.com/gin-contrib/static v0.0.0-20190913125243-df30d4057ba1 h1:20tgJcQcFETY
2425
github.com/gin-contrib/static v0.0.0-20190913125243-df30d4057ba1/go.mod h1:3pvUTQOgFP8/8nZgiWidsZ7piF6wCF0OVZlb5IyTD1Y=
2526
github.com/gin-gonic/gin v1.4.0 h1:3tMoCCfM7ppqsR0ptz/wi1impNpT7/9wQtMZ8lr1mCQ=
2627
github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM=
28+
github.com/go-redis/redis v6.15.6+incompatible h1:H9evprGPLI8+ci7fxQx6WNZHJSb7be8FqJQRhdQZ5Sg=
29+
github.com/go-redis/redis/v7 v7.0.0-beta.4 h1:p6z7Pde69EGRWvlC++y8aFcaWegyrKHzOBGo0zUACTQ=
30+
github.com/go-redis/redis/v7 v7.0.0-beta.4/go.mod h1:xhhSbUMTsleRPur+Vgx9sUHtyN33bdjxY+9/0n9Ig8s=
2731
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
2832
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
2933
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -35,6 +39,7 @@ github.com/google/go-github/v28 v28.1.1/go.mod h1:bsqJWQX05omyWVmc00nEUql9mhQyv3
3539
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
3640
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
3741
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
42+
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
3843
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
3944
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
4045
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
@@ -61,6 +66,9 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
6166
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
6267
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
6368
github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229/go.mod h1:0aYXnNPJ8l7uZxf45rWW1a/uME32OF0rhiYGNQ2oF2E=
69+
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
70+
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
71+
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
6472
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
6573
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6674
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -86,6 +94,7 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90Pveol
8694
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
8795
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734 h1:p/H982KKEjUnLJkM3tt/LemDnOc1GiZL5FCVlORJ5zo=
8896
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
97+
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
8998
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
9099
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
91100
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c h1:uOCk1iQW6Vc18bnC13MfzScl+wdKBmM9Y9kU7Z83/lw=
@@ -94,8 +103,10 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwL
94103
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
95104
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=
96105
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
106+
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
97107
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
98108
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
109+
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
99110
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
100111
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
101112
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@@ -113,9 +124,12 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
113124
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
114125
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
115126
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
127+
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
116128
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
117129
gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ=
118130
gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=
131+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
132+
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
119133
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
120134
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
121135
gopkg.in/yaml.v2 v2.2.5 h1:ymVxjfMaHvXD8RqPRmzHHsB3VvucivSkIAvJFDI5O3c=

main.go

+52-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import (
2323

2424
"github.com/google/go-github/v28/github"
2525

26-
"fmt"
26+
"github.com/go-redis/redis/v7"
27+
28+
"encoding/json"
2729

2830
model "go-svelte/models"
2931

@@ -35,8 +37,6 @@ import (
3537
)
3638

3739
func initGithubClient(accessToken string) (context.Context, *github.Client) {
38-
fmt.Printf("Initilizing Github Client")
39-
4040
ctx := context.Background()
4141

4242
ts := oauth2.StaticTokenSource(
@@ -49,6 +49,18 @@ func initGithubClient(accessToken string) (context.Context, *github.Client) {
4949
return ctx, client
5050
}
5151

52+
func createNewRedisClient() *redis.Client {
53+
redisHost := os.Getenv("REDIS_HOST")
54+
55+
client := redis.NewClient(&redis.Options{
56+
Addr: redisHost + ":6379",
57+
Password: "", // no password set
58+
DB: 0, // use default DB
59+
})
60+
61+
return client
62+
}
63+
5264
func main() {
5365
err := godotenv.Load(".env")
5466

@@ -62,6 +74,14 @@ func main() {
6274
zerolog.SetGlobalLevel(zerolog.DebugLevel)
6375
}
6476

77+
redisClient := createNewRedisClient()
78+
79+
_, redisErr := redisClient.Ping().Result()
80+
81+
if redisErr != nil {
82+
log.Fatal("Error connecting to redis")
83+
}
84+
6585
// Custom logger
6686
subLog := zerolog.New(os.Stdout).With().
6787
Logger()
@@ -72,9 +92,9 @@ func main() {
7292
log.Fatal("Error connecting to github")
7393
}
7494

75-
var githubToken = os.Getenv("GITHUB_TOKEN")
95+
githubToken := os.Getenv("GITHUB_TOKEN")
7696

77-
var githubUser = os.Getenv("GITHUB_USER")
97+
githubUser := os.Getenv("GITHUB_USER")
7898

7999
r := gin.Default()
80100

@@ -91,7 +111,7 @@ func main() {
91111

92112
r.Use(cors.New(config))
93113

94-
ctx, github := initGithubClient(githubToken)
114+
ctx, githubClient := initGithubClient(githubToken)
95115

96116
// Route for JS assets
97117
r.Use(static.Serve("/build", static.LocalFile("public/build", true)))
@@ -101,8 +121,6 @@ func main() {
101121

102122
// Route for index.html and config object
103123
r.GET("/", func(c *gin.Context) {
104-
fmt.Printf("/ route called")
105-
106124
var siteName = os.Getenv("SITE_NAME")
107125

108126
var devBlogSite = os.Getenv("DEV_BLOG_SITE")
@@ -116,13 +134,37 @@ func main() {
116134
})
117135

118136
r.GET("/user", func(c *gin.Context) {
119-
user, _, _ := github.Users.Get(ctx, githubUser)
137+
key := "user:" + githubUser
138+
139+
s, redisErr := redisClient.Get(key).Result()
140+
141+
user := github.User{}
142+
err = json.Unmarshal([]byte(s), &user)
143+
144+
if redisErr != nil {
145+
user, _, _ := githubClient.Users.Get(ctx, githubUser)
146+
json, err := json.Marshal(user)
147+
148+
if err != nil {
149+
c.JSON(400, gin.H{"error": err.Error()})
150+
return
151+
}
152+
153+
redisErr := redisClient.Set(key, json, 0).Err()
154+
155+
if redisErr != nil {
156+
c.JSON(400, gin.H{"error": redisErr.Error()})
157+
return
158+
}
159+
160+
c.JSON(200, user)
161+
}
120162

121163
c.JSON(200, user)
122164
})
123165

124166
r.GET("/repos", func(c *gin.Context) {
125-
repos, _, _ := github.Repositories.List(ctx, "", nil)
167+
repos, _, _ := githubClient.Repositories.List(ctx, "", nil)
126168

127169
queryCount := c.Request.URL.Query().Get("count")
128170

0 commit comments

Comments
 (0)