Skip to content

Commit

Permalink
Add a hook to get the value (#141)
Browse files Browse the repository at this point in the history
* add: func SetExpiredHookWithValue

* fix: ExpiredHook default to passing Value

* fix: Order of return values of the Delete method

* fix: variable name

* fix: val non nil and loaded

---------

Co-authored-by: ly-2045544 <ly-2045544@yahoo-corp.jp>
  • Loading branch information
fum1h1to and ly-2045544 authored Oct 25, 2024
1 parent afc028b commit e25d067
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
4 changes: 2 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ func main() {

runtime.GC()
gch := gache.New[int64]().EnableExpiredHook().
SetExpiredHook(func(ctx context.Context, key string) {
glg.Debugf("key=%v expired", key)
SetExpiredHook(func(ctx context.Context, key string, v int64) {
glg.Debugf("key=%v value=%d expired", key, v)
}).
StartExpired(context.Background(), time.Second*10)
for i := 0; i < 10000; i++ {
Expand Down
42 changes: 26 additions & 16 deletions gache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type (
// Gache is base interface type
Gache[V any] interface {
Clear()
Delete(string) bool
Delete(string) (V, bool)
DeleteExpired(context.Context) uint64
DisableExpiredHook() Gache[V]
EnableExpiredHook() Gache[V]
Expand All @@ -28,7 +28,7 @@ type (
Read(io.Reader) error
Set(string, V)
SetDefaultExpire(time.Duration) Gache[V]
SetExpiredHook(f func(context.Context, string)) Gache[V]
SetExpiredHook(f func(context.Context, string, V)) Gache[V]
SetWithExpire(string, V, time.Duration)
StartExpired(context.Context, time.Duration) Gache[V]
Len() int
Expand Down Expand Up @@ -61,8 +61,8 @@ type (
gache[V any] struct {
shards [slen]*Map[string, *value[V]]
cancel atomic.Pointer[context.CancelFunc]
expChan chan string
expFunc func(context.Context, string)
expChan chan keyValue[V]
expFunc func(context.Context, string, V)
expFuncEnabled bool
expire int64
l uint64
Expand All @@ -72,6 +72,11 @@ type (
val V
expire int64
}

keyValue[V any] struct {
key string
value V
}
)

const (
Expand All @@ -97,7 +102,7 @@ func New[V any](opts ...Option[V]) Gache[V] {
opt(g)
}
g.Clear()
g.expChan = make(chan string, len(g.shards)*10)
g.expChan = make(chan keyValue[V], len(g.shards)*10)
return g
}

Expand Down Expand Up @@ -136,7 +141,7 @@ func (g *gache[V]) DisableExpiredHook() Gache[V] {
}

// SetExpiredHook set expire hooked function
func (g *gache[V]) SetExpiredHook(f func(context.Context, string)) Gache[V] {
func (g *gache[V]) SetExpiredHook(f func(context.Context, string, V)) Gache[V] {
g.expFunc = f
return g
}
Expand All @@ -153,8 +158,8 @@ func (g *gache[V]) StartExpired(ctx context.Context, dur time.Duration) Gache[V]
case <-ctx.Done():
tick.Stop()
return
case key := <-g.expChan:
go g.expFunc(ctx, key)
case kv := <-g.expChan:
go g.expFunc(ctx, kv.key, kv.value)
case <-tick.C:
go func() {
g.DeleteExpired(ctx)
Expand Down Expand Up @@ -250,18 +255,23 @@ func (g *gache[V]) Set(key string, val V) {
}

// Delete deletes value from Gache using key
func (g *gache[V]) Delete(key string) (loaded bool) {
_, loaded = g.shards[getShardID(key)].LoadAndDelete(key)
func (g *gache[V]) Delete(key string) (v V, loaded bool) {
var val *value[V]
val, loaded = g.shards[getShardID(key)].LoadAndDelete(key)
if loaded {
atomic.AddUint64(&g.l, ^uint64(0))
}
return
if val != nil && loaded {
return val.val, loaded
}
return v, loaded
}

func (g *gache[V]) expiration(key string) {
g.Delete(key)
if g.expFuncEnabled {
g.expChan <- key
v, loaded := g.Delete(key)

if loaded && g.expFuncEnabled {
g.expChan <- keyValue[V]{key: key, value: v}
}
}

Expand Down Expand Up @@ -326,8 +336,8 @@ func (g *gache[V]) Size() (size uintptr) {
size += unsafe.Sizeof(g.expire) // int64
size += unsafe.Sizeof(g.l) // uint64
size += unsafe.Sizeof(g.cancel) // atomic.Pointer[context.CancelFunc]
size += unsafe.Sizeof(g.expChan) // chan string
size += unsafe.Sizeof(g.expFunc) // func(context.Context, string)
size += unsafe.Sizeof(g.expChan) // chan keyValue[V]
size += unsafe.Sizeof(g.expFunc) // func(context.Context, string, V)
for _, shard := range g.shards {
size += shard.Size()
}
Expand Down
2 changes: 1 addition & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func WithDefaultExpiration[V any](dur time.Duration) Option[V] {
}
}

func WithExpiredHookFunc[V any](f func(ctx context.Context, key string)) Option[V] {
func WithExpiredHookFunc[V any](f func(ctx context.Context, key string, v V)) Option[V] {
return func(g *gache[V]) error {
if f != nil {
g.expFunc = f
Expand Down

0 comments on commit e25d067

Please sign in to comment.