Files
gocommon/session/consumer_common.go

57 lines
1.1 KiB
Go

package session
import (
"context"
"sync"
"time"
)
type cache_stage[T any] struct {
cache map[storagekey]T
deleted map[storagekey]bool
}
func make_cache_stage[T any]() *cache_stage[T] {
return &cache_stage[T]{
cache: make(map[storagekey]T),
deleted: make(map[storagekey]bool),
}
}
type consumer_common[T any] struct {
lock sync.Mutex
ttl time.Duration
ctx context.Context
stages [2]*cache_stage[T]
startTime time.Time
}
func (c *consumer_common[T]) add_internal(sk storagekey, si T) {
c.stages[0].cache[sk] = si
delete(c.stages[0].deleted, sk)
c.stages[1].cache[sk] = si
delete(c.stages[1].deleted, sk)
}
func (c *consumer_common[T]) delete_internal(sk storagekey) {
delete(c.stages[0].cache, sk)
c.stages[0].deleted[sk] = true
delete(c.stages[1].cache, sk)
c.stages[1].deleted[sk] = true
}
func (c *consumer_common[T]) delete(sk storagekey) {
c.lock.Lock()
defer c.lock.Unlock()
c.delete_internal(sk)
}
func (c *consumer_common[T]) changeStage() {
c.lock.Lock()
defer c.lock.Unlock()
c.stages[1] = c.stages[0]
c.stages[0] = make_cache_stage[T]()
}