Skip to content

Commit

Permalink
Merge pull request #5 from kotalco/update-resp-dependencies
Browse files Browse the repository at this point in the history
feat:update redis dependencies
  • Loading branch information
mohamed-abdelrhman authored Mar 11, 2024
2 parents 9806a1e + af4c277 commit d4b984c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 116 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/kotalco/crossover-cache

go 1.21

require github.com/kotalco/resp v0.0.0-20240310155233-e8ffa70a7031
require github.com/kotalco/resp v0.0.0-20240311131414-34be1adf83d2
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/kotalco/resp v0.0.0-20240310155233-e8ffa70a7031 h1:F1VO30pQBmIo3Vmw2LtcjM6qd8ocxatXiUc8SFxAv2Y=
github.com/kotalco/resp v0.0.0-20240310155233-e8ffa70a7031/go.mod h1:+7+oV/yGtCk7K98vCqoFkkhG0gzoyfp/zNMtuSyxn08=
github.com/kotalco/resp v0.0.0-20240311131414-34be1adf83d2 h1:w1zVyA8V5eB3ndBdfrCFTtzh2ViuHz/BQa3djmS/ek0=
github.com/kotalco/resp v0.0.0-20240311131414-34be1adf83d2/go.mod h1:+7+oV/yGtCk7K98vCqoFkkhG0gzoyfp/zNMtuSyxn08=
54 changes: 25 additions & 29 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
)

const (
DefaultRedisPoolSize = 10
DefaultCacheExpiry = 15
DefaultCacheExpiry = 15
)

type CachedResponse struct {
Expand All @@ -21,10 +20,9 @@ type CachedResponse struct {
}

type Config struct {
RedisAddress string
RedisAuth string
RedisPoolSize int
CacheExpiry int //in seconds
RedisAddress string
RedisAuth string
CacheExpiry int //in seconds
}

// CreateConfig creates the default plugin configuration.
Expand All @@ -33,45 +31,43 @@ func CreateConfig() *Config {
}

type Cache struct {
next http.Handler
name string
resp resp.IClient
redisAuth string
redisAddress string
redisPoolSize int
cacheExpiry int
next http.Handler
name string
redisAuth string
redisAddress string
cacheExpiry int
}

func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
if config.RedisPoolSize == 0 {
config.RedisPoolSize = DefaultRedisPoolSize
}
if config.CacheExpiry == 0 {
config.CacheExpiry = DefaultCacheExpiry
}
gob.Register(CachedResponse{})

//ignore error check to run the plugin with Yaegi
respClient, _ := resp.NewRedisClient(config.RedisAddress, config.RedisPoolSize, config.RedisAuth)

handler := &Cache{
next: next,
name: name,
resp: respClient,
redisAddress: config.RedisAddress,
redisAuth: config.RedisAuth,
redisPoolSize: config.RedisPoolSize,
cacheExpiry: config.CacheExpiry,
next: next,
name: name,
redisAddress: config.RedisAddress,
redisAuth: config.RedisAuth,
cacheExpiry: config.CacheExpiry,
}
return handler, nil
}

func (c *Cache) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
respClient, err := resp.NewRedisClient(c.redisAddress, c.redisAuth)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
log.Printf("Failed to create Redis Connection %s", err.Error())
rw.Write([]byte("something went wrong"))
return
}
defer respClient.Close()
// cache key based on the request
cacheKey := req.URL.Path

// retrieve the cached response
cachedData, err := c.resp.Get(req.Context(), cacheKey)
cachedData, err := respClient.Get(req.Context(), cacheKey)
if err == nil && cachedData != "" {
// Cache hit - parse the cached response and write it to the original ResponseWriter
var cachedResponse CachedResponse
Expand All @@ -88,7 +84,7 @@ func (c *Cache) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
return
}
log.Printf("Failed to serialize response for caching: %s", err.Error())
_ = c.resp.Delete(req.Context(), cacheKey)
_ = respClient.Delete(req.Context(), cacheKey)
}

// Cache miss - record the response
Expand All @@ -108,7 +104,7 @@ func (c *Cache) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}

// Store the serialized response in Redis as a string with an expiration time
if err := c.resp.SetWithTTL(req.Context(), cacheKey, buffer.String(), c.cacheExpiry); err != nil {
if err := respClient.SetWithTTL(req.Context(), cacheKey, buffer.String(), c.cacheExpiry); err != nil {
log.Println("Failed to cache response in Redis:", err)
}

Expand Down
102 changes: 19 additions & 83 deletions vendor/github.com/kotalco/resp/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# github.com/kotalco/resp v0.0.0-20240310155233-e8ffa70a7031
# github.com/kotalco/resp v0.0.0-20240311131414-34be1adf83d2
## explicit; go 1.21
github.com/kotalco/resp

0 comments on commit d4b984c

Please sign in to comment.