Skip to content

Commit

Permalink
Support humanize size units (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
mstmdev authored Jan 22, 2024
1 parent b673473 commit b9a4da7
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ Current support following cache drivers
| BuntDB | `github.com/no-src/nscache/buntdb` | `buntdb://:memory:` or `buntdb://buntdb.db` |
| Etcd | `github.com/no-src/nscache/etcd` | `etcd://127.0.0.1:2379?dial_timeout=5s` |
| BoltDB | `github.com/no-src/nscache/boltdb` | `boltdb://boltdb.db` |
| FreeCache | `github.com/no-src/nscache/freecache` | `freecache://?cache_size=10000000` |
| FreeCache | `github.com/no-src/nscache/freecache` | `freecache://?cache_size=50mib` |
| BigCache | `github.com/no-src/nscache/bigcache` | `bigcache://?eviction=10m` |
| FastCache | `github.com/no-src/nscache/fastcache` | `fastcache://?max_bytes=10000000` |
| FastCache | `github.com/no-src/nscache/fastcache` | `fastcache://?max_bytes=50mib` |

For example, initial a memory cache and write, read and remove data.

Expand Down
6 changes: 3 additions & 3 deletions etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"context"
"errors"
"net/url"
"strconv"
"sync"
"time"

"github.com/no-src/nscache"
"github.com/no-src/nscache/encoding"
"github.com/no-src/nscache/extension"
"github.com/no-src/nsgo/unit"
clientv3 "go.etcd.io/etcd/client/v3"
)

Expand Down Expand Up @@ -104,15 +104,15 @@ func parseEtcdConnection(u *url.URL) (c clientv3.Config, err error) {
}
maxCallSendMsgSizeValue := u.Query().Get("max_call_send_msg_size")
if len(maxCallSendMsgSizeValue) > 0 {
maxCallSendMsgSize, err := strconv.Atoi(maxCallSendMsgSizeValue)
maxCallSendMsgSize, err := unit.ParseBytesInt(maxCallSendMsgSizeValue)
if err != nil {
return c, err
}
c.MaxCallSendMsgSize = maxCallSendMsgSize
}
maxCallRecvMsgSizeValue := u.Query().Get("max_call_recv_msg_size")
if len(maxCallRecvMsgSizeValue) > 0 {
maxCallRecvMsgSize, err := strconv.Atoi(maxCallRecvMsgSizeValue)
maxCallRecvMsgSize, err := unit.ParseBytesInt(maxCallRecvMsgSizeValue)
if err != nil {
return c, err
}
Expand Down
4 changes: 2 additions & 2 deletions fastcache/fastcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package fastcache
import (
"errors"
"net/url"
"strconv"

"github.com/VictoriaMetrics/fastcache"
"github.com/no-src/nscache"
"github.com/no-src/nscache/cache"
"github.com/no-src/nscache/encoding"
"github.com/no-src/nsgo/unit"
)

const (
Expand All @@ -29,7 +29,7 @@ func parseConnection(u *url.URL) (maxBytes int, err error) {
return maxBytes, errors.New("invalid fastcache connection string")
}
maxBytesStr := u.Query().Get("max_bytes")
maxBytes, err = strconv.Atoi(maxBytesStr)
maxBytes, err = unit.ParseBytesInt(maxBytesStr)
if err != nil {
return maxBytes, errors.New("invalid max_bytes parameter in the fastcache connection string")
}
Expand Down
4 changes: 2 additions & 2 deletions freecache/freecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package freecache
import (
"errors"
"net/url"
"strconv"
"time"

"github.com/coocood/freecache"
"github.com/no-src/nscache"
"github.com/no-src/nscache/encoding"
"github.com/no-src/nscache/extension"
"github.com/no-src/nsgo/unit"
)

const (
Expand Down Expand Up @@ -45,7 +45,7 @@ func parseConnection(u *url.URL) (cacheSize int, err error) {
return cacheSize, errors.New("invalid freecache connection string")
}
sizeStr := u.Query().Get("cache_size")
cacheSize, err = strconv.Atoi(sizeStr)
cacheSize, err = unit.ParseBytesInt(sizeStr)
if err != nil {
return cacheSize, errors.New("invalid cache_size parameter in the freecache connection string")
}
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/allegro/bigcache/v3 v3.1.0
github.com/coocood/freecache v1.2.4
github.com/no-src/log v0.3.1
github.com/no-src/nsgo v0.0.3
github.com/redis/go-redis/v9 v9.4.0
github.com/tidwall/buntdb v1.3.0
go.etcd.io/bbolt v1.3.8
Expand All @@ -18,6 +19,7 @@ require (
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
Expand All @@ -34,8 +36,8 @@ require (
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
Expand Down
11 changes: 8 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
Expand All @@ -34,6 +36,8 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/no-src/log v0.3.1 h1:HvozFJQwRicaCThaRztVNOACR01jMiK8Upn4eU7lyps=
github.com/no-src/log v0.3.1/go.mod h1:AG2AiKsnJpZQHmJ24waj6Bi0xHaBEPXiFl1yYpHzLJQ=
github.com/no-src/nsgo v0.0.3 h1:YB8g7cecF2QGHE1EwzyPy97kxEn0gTruTrngZzL7EOM=
github.com/no-src/nsgo v0.0.3/go.mod h1:KvBQ8jFxDROIW1pMIrJ3G2ovE+oQP/upeMf1S/5jmZA=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down Expand Up @@ -96,12 +100,13 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
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-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
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.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
Expand Down
4 changes: 2 additions & 2 deletions internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ const (
// BoltDBConnectionString a boltdb cache driver test connection string
BoltDBConnectionString = "boltdb://boltdb.db"
// FreeCacheConnectionString a freecache driver test connection string
FreeCacheConnectionString = "freecache://?cache_size=10000000"
FreeCacheConnectionString = "freecache://?cache_size=50mib"
// BigCacheConnectionString a bigcache driver test connection string
BigCacheConnectionString = "bigcache://?eviction=10m"
// FastCacheConnectionString a fastcache driver test connection string
FastCacheConnectionString = "fastcache://?max_bytes=10000000"
FastCacheConnectionString = "fastcache://?max_bytes=50mib"
// DefaultExpiration the default expiration time for cache driver tests
DefaultExpiration = time.Second * 3
// NoExpiration means never expire
Expand Down

0 comments on commit b9a4da7

Please sign in to comment.