diff --git a/.circleci/config.yml b/.circleci/config.yml index df2b413..5053591 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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" diff --git a/Makefile b/Makefile index 88ba78b..cfadc31 100644 --- a/Makefile +++ b/Makefile @@ -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: @@ -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 diff --git a/example/main.go b/example/main.go index 0523762..e2a3582 100644 --- a/example/main.go +++ b/example/main.go @@ -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) @@ -52,6 +52,7 @@ func main() { } gc.Write(context.Background(), file) + gc.Stop() file.Close() gcn := gache.New[any]().SetDefaultExpire(time.Minute) diff --git a/gache.go b/gache.go index 4a0e8ae..e5d350a 100644 --- a/gache.go +++ b/gache.go @@ -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) @@ -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(): @@ -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() } } diff --git a/go.mod b/go.mod index 97a8caf..5f838c4 100644 --- a/go.mod +++ b/go.mod @@ -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 ( diff --git a/go.sum b/go.sum index 231b0cc..c3a91eb 100644 --- a/go.sum +++ b/go.sum @@ -4,10 +4,10 @@ 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= @@ -15,6 +15,6 @@ github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaD 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=