세션 해제 콜백 추가

This commit is contained in:
2023-12-25 22:06:57 +09:00
parent 08802176cb
commit 46f7d358ed
6 changed files with 110 additions and 27 deletions

View File

@ -4,6 +4,8 @@ import (
"context"
"sync"
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type cache_stage[T any] struct {
@ -19,11 +21,12 @@ func make_cache_stage[T any]() *cache_stage[T] {
}
type consumer_common[T any] struct {
lock sync.Mutex
ttl time.Duration
ctx context.Context
stages [2]*cache_stage[T]
startTime time.Time
lock sync.Mutex
ttl time.Duration
ctx context.Context
stages [2]*cache_stage[T]
startTime time.Time
onSessionInvalidated []func(primitive.ObjectID)
}
func (c *consumer_common[T]) add_internal(sk storagekey, si T) {
@ -33,18 +36,25 @@ func (c *consumer_common[T]) add_internal(sk storagekey, si T) {
delete(c.stages[1].deleted, sk)
}
func (c *consumer_common[T]) delete_internal(sk storagekey) {
delete(c.stages[0].cache, sk)
func (c *consumer_common[T]) delete_internal(sk storagekey) (old T) {
if v, ok := c.stages[0].cache[sk]; ok {
old = v
delete(c.stages[0].cache, sk)
delete(c.stages[1].cache, sk)
} else if v, ok = c.stages[1].cache[sk]; ok {
old = v
delete(c.stages[1].cache, sk)
}
c.stages[0].deleted[sk] = true
delete(c.stages[1].cache, sk)
c.stages[1].deleted[sk] = true
return
}
func (c *consumer_common[T]) delete(sk storagekey) {
func (c *consumer_common[T]) delete(sk storagekey) T {
c.lock.Lock()
defer c.lock.Unlock()
c.delete_internal(sk)
return c.delete_internal(sk)
}
func (c *consumer_common[T]) changeStage() {