Skip to content

Commit

Permalink
Redis implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagozs committed Nov 25, 2021
1 parent b46fd8a commit e26ac11
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Simple cache implementation **key**, **value** system.
The cache you can choose between drivers for different storage.

### Implementation
* [x] BuntDB
* [ ] Redis
* [x] **BuntDB**
* [x] **Redis**

## How to use the cache

Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions examples/redis/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: '3'

services:
redis:
image: redis
ports:
- "6379:6379"
43 changes: 43 additions & 0 deletions examples/redis/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"fmt"

"github.com/thiagozs/go-cache/v1/cache"
"github.com/thiagozs/go-cache/v1/cache/drivers"
"github.com/thiagozs/go-cache/v1/cache/options"
)

func main() {
opts := []options.Options{
options.OptTTL(3000),
options.OptLogDebug(false),
options.OptLogDisable(false),
options.OptHost("localhost"),
options.OptPort(6379),
}

cache, err := cache.New(drivers.REDIS, opts...)
if err != nil {
fmt.Println("Error:", err)
return
}

cache.WriteKeyVal("key1", "value1")
cache.WriteKeyVal("key2", "value2")

v1, err := cache.GetVal("key1")
if err != nil {
fmt.Println("Error:", err)
return
}

v2, err := cache.GetVal("key2")
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("v1:", v1)
fmt.Println("v2:", v2)

}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
)

require (
github.com/go-redis/redis/v7 v7.4.1
github.com/tidwall/btree v0.6.1 // indirect
github.com/tidwall/buntdb v1.2.7
github.com/tidwall/gjson v1.11.0 // indirect
Expand Down
33 changes: 33 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-redis/redis/v7 v7.4.1 h1:PASvf36gyUpr2zdOUS/9Zqc80GbM+9BDyiJSJDDOrTI=
github.com/go-redis/redis/v7 v7.4.1/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo=
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.26.0 h1:ORM4ibhEZeTeQlCojCK2kPz1ogAY4bGs4tD+SaAdGaE=
Expand Down Expand Up @@ -31,22 +46,40 @@ github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d h1:20cMwl2fHAzkJMEA+8J4JgqBQcQGzbisXo31MIeenXI=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
5 changes: 5 additions & 0 deletions v1/cache/drivers/buntdb/buntdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ func (d *buntdblayer) WriteKeyValAsJSON(key string, val interface{}) error {
}

func (d *buntdblayer) WriteKeyValAsJSONTTL(key string, val interface{}, ttlSeconds int) error {
if ttlSeconds == 0 {
d.log.Debug().Int("ttl_seconds", d.ttl).Msg("WriteKeyValTTL")
ttlSeconds = d.ttl
}

valueAsJSON, err := json.Marshal(val)
if err != nil {
d.log.Debug().Str("method", "json.Marshal").Err(err).Msg("WriteKeyValAsJSONTTL")
Expand Down
69 changes: 51 additions & 18 deletions v1/cache/drivers/redis/redis.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package redislayer

import (
"encoding/json"
"fmt"
"os"
"time"

"github.com/go-redis/redis/v7"
"github.com/rs/zerolog"
"github.com/thiagozs/go-cache/v1/cache/options"
)
Expand All @@ -23,6 +27,7 @@ type redisblayer struct {
port int
ttl int
log zerolog.Logger
rdb *redis.Client
}

func NewRedis(opts ...options.Options) (RedisLayerRepo, error) {
Expand Down Expand Up @@ -51,49 +56,77 @@ func newInstance(opt *options.OptionsCfg) (RedisLayerRepo, error) {
zerolog.SetGlobalLevel(zerolog.Disabled)
}

rdb := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", opt.GetHost(), opt.GetPort()),
Password: opt.GetPassword(), // no password set
DB: 0, // use default DB
})

_, err := rdb.Ping().Result()
if err != nil {
log.Error().Err(err).Msg("error ping")
return nil, err
}

return &redisblayer{
log: log,
host: opt.GetHost(),
password: opt.GetPassword(),
user: opt.GetUser(),
port: opt.GetPort(),
ttl: opt.GetTTL(),
rdb: rdb,
}, nil
}

func (r *redisblayer) Ping() (string, error) {
return r.rdb.Ping().Result()
}

func (d *redisblayer) GetVal(key string) (string, error) {
var value string
// TODO: implement
return value, nil
return d.rdb.Get(key).Result()
}

func (d *redisblayer) DeleteKey(key string) (string, error) {
var value string
// TODO: implement

return value, nil
val, err := d.rdb.Del(key).Result()
if err != nil {
d.log.Debug().Err(err).Msg("DeleteKey")
return "", err
}
return fmt.Sprintf("%d", val), nil
}

func (d *redisblayer) WriteKeyVal(key string, val string) error {
// TODO: implement

return nil
return d.rdb.Set(key, val, time.Duration(0)).Err()
}

func (d *redisblayer) WriteKeyValTTL(key string, val string, ttlSeconds int) error {
// TODO: implement

return nil
if ttlSeconds == 0 {
d.log.Debug().Int("ttl_seconds", d.ttl).Msg("WriteKeyValTTL")
ttlSeconds = d.ttl
}
return d.rdb.Set(key, val, time.Duration(ttlSeconds)).Err()
}

func (d *redisblayer) WriteKeyValAsJSON(key string, val interface{}) error {
// TODO: implement

return d.WriteKeyVal(key, string(""))
valueAsJSON, err := json.Marshal(val)
if err != nil {
d.log.Debug().Str("method", "json.Marshal").Err(err).Msg("WriteKeyValAsJSON")
return err
}
return d.WriteKeyVal(key, string(valueAsJSON))
}

func (d *redisblayer) WriteKeyValAsJSONTTL(key string, val interface{}, ttlSeconds int) error {
// TODO: implement
if ttlSeconds == 0 {
d.log.Debug().Int("ttl_seconds", d.ttl).Msg("WriteKeyValAsJSONTTL")
ttlSeconds = d.ttl
}
valueAsJSON, err := json.Marshal(val)
if err != nil {
d.log.Debug().Str("method", "json.Marshal").Err(err).Msg("WriteKeyValAsJSONTTL")
return err
}

return d.WriteKeyValTTL(key, string(""), ttlSeconds)
return d.WriteKeyValTTL(key, string(valueAsJSON), ttlSeconds)
}

0 comments on commit e26ac11

Please sign in to comment.