Skip to content

Commit 67bc012

Browse files
committed
caching is workingggggggg
1 parent 45766e3 commit 67bc012

File tree

16 files changed

+945
-391
lines changed

16 files changed

+945
-391
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ __debug*
22
.DS_Store
33
*.log
44
.vscode
5-
*.log.json
5+
*.log.json
6+
*.cache.json

cache/cache.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package cache
22

3-
import "encoding/json"
4-
53
type Cache interface {
64
Set(key string, value interface{}) error
75
Get(key string) (interface{}, error)
@@ -10,40 +8,3 @@ type Cache interface {
108
Map() (map[string]interface{}, error)
119
JSON() ([]byte, error)
1210
}
13-
14-
type InMemoryCache struct {
15-
cache map[string]interface{}
16-
}
17-
18-
func NewInMemoryCache() *InMemoryCache {
19-
return &InMemoryCache{
20-
cache: make(map[string]interface{}),
21-
}
22-
}
23-
24-
func (c *InMemoryCache) Set(key string, value interface{}) error {
25-
c.cache[key] = value
26-
return nil
27-
}
28-
29-
func (c *InMemoryCache) Get(key string) (interface{}, error) {
30-
return c.cache[key], nil
31-
}
32-
33-
func (c *InMemoryCache) Del(key string) error {
34-
delete(c.cache, key)
35-
return nil
36-
}
37-
38-
func (c *InMemoryCache) Exists(key string) (bool, error) {
39-
_, exists := c.cache[key]
40-
return exists, nil
41-
}
42-
43-
func (c *InMemoryCache) Map() (map[string]interface{}, error) {
44-
return c.cache, nil
45-
}
46-
47-
func (c *InMemoryCache) JSON() ([]byte, error) {
48-
return json.Marshal(c.cache)
49-
}

cache/cache_response.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package cache

cache/inmemory_cache.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cache
2+
3+
import (
4+
"encoding/json"
5+
"graphql_cache/utils/file_utils"
6+
)
7+
8+
type InMemoryCache struct {
9+
cache map[string]interface{}
10+
}
11+
12+
func NewInMemoryCache() *InMemoryCache {
13+
return &InMemoryCache{
14+
cache: make(map[string]interface{}),
15+
}
16+
}
17+
18+
func (c *InMemoryCache) Set(key string, value interface{}) error {
19+
c.cache[key] = value
20+
return nil
21+
}
22+
23+
func (c *InMemoryCache) Get(key string) (interface{}, error) {
24+
return c.cache[key], nil
25+
}
26+
27+
func (c *InMemoryCache) Del(key string) error {
28+
delete(c.cache, key)
29+
return nil
30+
}
31+
32+
func (c *InMemoryCache) Exists(key string) (bool, error) {
33+
_, exists := c.cache[key]
34+
return exists, nil
35+
}
36+
37+
func (c *InMemoryCache) Map() (map[string]interface{}, error) {
38+
return c.cache, nil
39+
}
40+
41+
func (c *InMemoryCache) JSON() ([]byte, error) {
42+
return json.Marshal(c.cache)
43+
}
44+
45+
func (c *InMemoryCache) Debug(identifier string) error {
46+
f := file_utils.NewFile(identifier + ".cache.json")
47+
defer f.Close()
48+
jsonContent, _ := c.JSON()
49+
f.Write(string(jsonContent))
50+
return nil
51+
}

cache/redis_cache.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package cache
2+
3+
import (
4+
"context"
5+
6+
"github.com/go-redis/redis/v8"
7+
)
8+
9+
var ctx = context.Background()
10+
11+
type RedisCache struct {
12+
cache *redis.Client
13+
}
14+
15+
// var client *redis.Client
16+
17+
// func init() {
18+
// client = redis.NewClient(&redis.Options{
19+
// Addr: "localhost:6379",
20+
// Password: "", // no password set
21+
// DB: 1, // use default DB
22+
// })
23+
// }
24+
25+
func NewRedisCache() *RedisCache {
26+
c := redis.NewClient(&redis.Options{
27+
Addr: "localhost:6379",
28+
Password: "", // no password set
29+
DB: 0, // use default DB
30+
})
31+
return &RedisCache{
32+
cache: c,
33+
}
34+
}
35+
36+
func (c *RedisCache) Set(key string, value interface{}) error {
37+
c.cache.Set(ctx, key, value, 0)
38+
return nil
39+
}
40+
41+
func (c *RedisCache) Get(key string) (interface{}, error) {
42+
val, err := c.cache.Get(ctx, key).Result()
43+
if err != nil {
44+
return nil, err
45+
}
46+
return val, nil
47+
}
48+
49+
func (c *RedisCache) Del(key string) error {
50+
c.cache.Del(ctx, key)
51+
return nil
52+
}
53+
54+
func (c *RedisCache) Exists(key string) (bool, error) {
55+
val, err := c.cache.Exists(ctx, key).Result()
56+
if err != nil {
57+
return false, err
58+
}
59+
return val == 1, nil
60+
}
61+
62+
func (c *RedisCache) Map() (map[string]interface{}, error) {
63+
return nil, nil
64+
}
65+
66+
func (c *RedisCache) JSON() ([]byte, error) {
67+
return nil, nil
68+
}
69+
70+
func (c *RedisCache) Debug(identifier string) error {
71+
return nil
72+
}

go.mod

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ module graphql_cache
33
go 1.20
44

55
require (
6+
github.com/go-redis/redis/v8 v8.11.5
67
github.com/vektah/gqlparser v1.3.1
7-
github.com/vektah/gqlparser/v2 v2.5.16
8+
)
9+
10+
require (
11+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
12+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
13+
github.com/sergi/go-diff v1.3.1 // indirect
14+
github.com/stretchr/testify v1.9.0 // indirect
815
)

go.sum

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,42 @@
11
github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM=
22
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
33
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
4+
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
5+
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
46
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
57
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
8+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
10+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
11+
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
12+
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
13+
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
14+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
15+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
16+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
17+
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
18+
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
19+
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
620
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
721
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
822
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
923
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
24+
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
1025
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1126
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
27+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
1228
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
29+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
1330
github.com/vektah/gqlparser v1.3.1 h1:8b0IcD3qZKWJQHSzynbDlrtP3IxVydZ2DZepCGofqfU=
1431
github.com/vektah/gqlparser v1.3.1/go.mod h1:bkVf0FX+Stjg/MHnm8mEyubuaArhNEqfQhF+OTiAL74=
15-
github.com/vektah/gqlparser/v2 v2.5.16 h1:1gcmLTvs3JLKXckwCwlUagVn/IlV2bwqle0vJ0vy5p8=
16-
github.com/vektah/gqlparser/v2 v2.5.16/go.mod h1:1lz1OeCqgQbQepsGxPVywrjdBHW2T08PUS3pJqepRww=
32+
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 h1:DzZ89McO9/gWPsQXS/FVKAlG02ZjaQ6AlZRBimEYOd0=
33+
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
34+
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
1735
golang.org/x/tools v0.0.0-20190125232054-d66bd3c5d5a6/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
1836
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
37+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
38+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
1939
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
2040
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
41+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
2142
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

0 commit comments

Comments
 (0)