Skip to content

Commit

Permalink
[patch] small refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: kpango <kpango@vdaas.org>
  • Loading branch information
kpango committed Mar 16, 2023
1 parent 0d00e17 commit edf5279
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
alias:
default: &default
docker:
- image: circleci/go:1.19
- image: cimg/go:1.20.2
environment:
GO111MODULE: "on"
REPO_NAME: "kpango"
Expand Down
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ GO_VERSION:=$(shell go version)

.PHONY: all clean bench bench-all profile lint test contributors update install


GOPATH := $(eval GOPATH := $(shell go env GOPATH))$(GOPATH)
GOLINES_MAX_WIDTH ?= 200

all: clean install lint test bench

clean:
Expand Down Expand Up @@ -54,3 +58,9 @@ contributors:

run:
go run example/main.go

format:
find ./ -type d -name .git -prune -o -type f -regex '.*[^\.pb]\.go' -print | xargs $(GOPATH)/bin/golines -w -m $(GOLINES_MAX_WIDTH)
find ./ -type d -name .git -prune -o -type f -regex '.*[^\.pb]\.go' -print | xargs $(GOPATH)/bin/gofumpt -w
find ./ -type d -name .git -prune -o -type f -regex '.*[^\.pb]\.go' -print | xargs $(GOPATH)/bin/strictgoimports -w
find ./ -type d -name .git -prune -o -type f -regex '.*\.go' -print | xargs $(GOPATH)/bin/goimports -w
3 changes: 2 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {
value3 = struct{}{}
)

gc := gache.New[any]().SetDefaultExpire(time.Second * 10)
gc := gache.New[any]().SetDefaultExpire(time.Second*10).StartExpired(context.Background(), time.Hour)

// store with expire setting
gc.SetWithExpire(key1, value1, time.Second*30)
Expand Down Expand Up @@ -52,6 +52,7 @@ func main() {
}
gc.Write(context.Background(), file)

gc.Stop()
file.Close()

gcn := gache.New[any]().SetDefaultExpire(time.Minute)
Expand Down
9 changes: 4 additions & 5 deletions gache.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type (
expFuncEnabled bool
expire int64
l uint64
cancel atomic.Value
cancel atomic.Pointer[context.CancelFunc]
expGroup singleflight.Group
expChan chan string
expFunc func(context.Context, string)
Expand Down Expand Up @@ -150,7 +150,7 @@ func (g *gache[V]) StartExpired(ctx context.Context, dur time.Duration) Gache[V]
go func() {
tick := time.NewTicker(dur)
ctx, cancel := context.WithCancel(ctx)
g.cancel.Store(cancel)
g.cancel.Store(&cancel)
for {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -350,9 +350,8 @@ func (g *gache[V]) Read(r io.Reader) error {
// Stop kills expire daemon
func (g *gache[V]) Stop() {
if c := g.cancel.Load(); c != nil {
if cancel, ok := c.(context.CancelFunc); ok && cancel != nil {
cancel()
}
cancel := *c
cancel()
}
}

Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module github.com/kpango/gache/v2

go 1.19
go 1.20

require (
github.com/cornelk/hashmap v1.0.8
github.com/kpango/fastime v1.1.4
github.com/kpango/glg v1.6.13
github.com/kpango/fastime v1.1.7
github.com/kpango/glg v1.6.14
github.com/zeebo/xxh3 v1.0.2
golang.org/x/sync v0.0.0-20220907140024-f12130a52804
golang.org/x/sync v0.1.0
)

require (
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk=
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/kpango/fastime v1.1.4 h1:pus9JgJBg/8Jie3ozayA4yNIV67BUPhbq0wMZY3CtYo=
github.com/kpango/fastime v1.1.4/go.mod h1:tTNDbIo5qL6D7g5vh2YbkyUbOVP2kD/we3rSjN22PMY=
github.com/kpango/glg v1.6.13 h1:QMhxOm/Oo1k8qraMtH4SQOYIgB/SI2RW2Hvrn1kgAZw=
github.com/kpango/glg v1.6.13/go.mod h1:fwP/c6NJTXe0vd9L3He6myDnO33lFVfgQGtGmlMnyws=
github.com/kpango/fastime v1.1.7 h1:xaWENHzTrfzAmqhLayoLVJFF0MBf1EGZL9FL6V/OyDo=
github.com/kpango/fastime v1.1.7/go.mod h1:vyD7FnUn08zxY4b/QFBZVG+9EWMYsNl+QF0uE46urD4=
github.com/kpango/glg v1.6.14 h1:Ss3ZvTQ23blUCDYizSAijiFTZsgGeYr/lanUGgQ10rY=
github.com/kpango/glg v1.6.14/go.mod h1:2djk7Zr4zKIYPHlORH8tJVlhCEh+XXW8W4K3qJyNXMI=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=
github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY=
golang.org/x/sync v0.0.0-20220907140024-f12130a52804 h1:0SH2R3f1b1VmIMG7BXbEZCBUu2dKmHschSmjqGUrW8A=
golang.org/x/sync v0.0.0-20220907140024-f12130a52804/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=

0 comments on commit edf5279

Please sign in to comment.