-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* start with New(uri) constructor for #6 * add tests for url constructor * add short description
- Loading branch information
Showing
7 changed files
with
353 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,23 @@ | ||
module github.com/go-pkgz/lcw | ||
|
||
require ( | ||
github.com/alicebob/gopher-json v0.0.0-20180125190556-5a6b3ba71ee6 // indirect | ||
github.com/alicebob/miniredis v2.5.0+incompatible | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/go-redis/redis/v7 v7.0.0-beta.4 | ||
github.com/hashicorp/golang-lru v0.5.0 | ||
github.com/golang/protobuf v1.3.2 // indirect | ||
github.com/gomodule/redigo v2.0.0+incompatible // indirect | ||
github.com/hashicorp/go-multierror v1.0.0 | ||
github.com/hashicorp/golang-lru v0.5.3 | ||
github.com/kr/pretty v0.1.0 // indirect | ||
github.com/patrickmn/go-cache v2.1.0+incompatible | ||
github.com/pkg/errors v0.8.1 | ||
github.com/stretchr/testify v1.3.0 | ||
github.com/yuin/gopher-lua v0.0.0-20190514113301-1cd887cd7036 // indirect | ||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 // indirect | ||
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0 // indirect | ||
golang.org/x/text v0.3.2 // indirect | ||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect | ||
) | ||
|
||
go 1.13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package lcw | ||
|
||
import ( | ||
"net/url" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/go-redis/redis/v7" | ||
"github.com/hashicorp/go-multierror" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// New parses uri and makes any of supported caches | ||
// supported URIs: | ||
// - redis://<ip>:<port>?db=123&max_keys=10 | ||
// - mem://lru?max_keys=10&max_cache_size=1024 | ||
// - mem://expirable?ttl=30s&max_val_size=100 | ||
// - nop:// | ||
func New(uri string) (LoadingCache, error) { | ||
u, err := url.Parse(uri) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "parse cache uri %s", uri) | ||
} | ||
|
||
query := u.Query() | ||
opts, err := optionsFromQuery(query) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "parse uri options %s", uri) | ||
} | ||
|
||
switch u.Scheme { | ||
case "redis": | ||
redisOpts, err := redisOptionsFromURL(u) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res, err := NewRedisCache(redis.NewClient(redisOpts), opts...) | ||
return res, errors.Wrapf(err, "make redis for %s", uri) | ||
case "mem": | ||
switch u.Hostname() { | ||
case "lru": | ||
return NewLruCache(opts...) | ||
case "expirable": | ||
return NewExpirableCache(opts...) | ||
default: | ||
return nil, errors.Errorf("unsupported mem cache type %s", u.Hostname()) | ||
} | ||
case "nop": | ||
return NewNopCache(), nil | ||
} | ||
return nil, errors.Errorf("unsupported cache type %s", u.Scheme) | ||
} | ||
|
||
func optionsFromQuery(q url.Values) (opts []Option, err error) { | ||
|
||
errs := new(multierror.Error) | ||
|
||
if v := q.Get("max_val_size"); v != "" { | ||
vv, e := strconv.Atoi(v) | ||
if e != nil { | ||
errs = multierror.Append(errs, errors.Wrapf(e, "max_val_size query param %s", v)) | ||
} else { | ||
opts = append(opts, MaxValSize(vv)) | ||
} | ||
} | ||
|
||
if v := q.Get("max_key_size"); v != "" { | ||
vv, e := strconv.Atoi(v) | ||
if e != nil { | ||
errs = multierror.Append(errs, errors.Wrapf(e, "max_key_size query param %s", v)) | ||
} else { | ||
opts = append(opts, MaxKeySize(vv)) | ||
} | ||
} | ||
|
||
if v := q.Get("max_keys"); v != "" { | ||
vv, e := strconv.Atoi(v) | ||
if e != nil { | ||
errs = multierror.Append(errs, errors.Wrapf(e, "max_keys query param %s", v)) | ||
} else { | ||
opts = append(opts, MaxKeys(vv)) | ||
} | ||
} | ||
|
||
if v := q.Get("max_cache_size"); v != "" { | ||
vv, e := strconv.ParseInt(v, 10, 64) | ||
if e != nil { | ||
errs = multierror.Append(errs, errors.Wrapf(e, "max_cache_size query param %s", v)) | ||
} else { | ||
opts = append(opts, MaxCacheSize(vv)) | ||
} | ||
} | ||
|
||
if v := q.Get("ttl"); v != "" { | ||
vv, e := time.ParseDuration(v) | ||
if e != nil { | ||
errs = multierror.Append(errs, errors.Wrapf(e, "ttl query param %s", v)) | ||
} else { | ||
opts = append(opts, TTL(vv)) | ||
} | ||
} | ||
|
||
return opts, errs.ErrorOrNil() | ||
} | ||
|
||
func redisOptionsFromURL(u *url.URL) (*redis.Options, error) { | ||
query := u.Query() | ||
|
||
db, err := strconv.Atoi(query.Get("db")) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "db from %s", u) | ||
} | ||
|
||
res := &redis.Options{ | ||
Addr: u.Hostname() + ":" + u.Port(), | ||
DB: db, | ||
Password: query.Get("password"), | ||
Network: query.Get("network"), | ||
} | ||
|
||
if dialTimeout, err := time.ParseDuration(query.Get("dial_timeout")); err == nil { | ||
res.DialTimeout = dialTimeout | ||
} | ||
|
||
if readTimeout, err := time.ParseDuration(query.Get("read_timeout")); err == nil { | ||
res.ReadTimeout = readTimeout | ||
} | ||
|
||
if writeTimeout, err := time.ParseDuration(query.Get("write_timeout")); err == nil { | ||
res.WriteTimeout = writeTimeout | ||
} | ||
|
||
return res, nil | ||
} |
Oops, something went wrong.