ttlcache支持泛型

This commit is contained in:
刘祥超
2023-10-05 08:28:16 +08:00
parent 8561ff3e2d
commit d92f27c44b
10 changed files with 95 additions and 89 deletions

View File

@@ -11,17 +11,21 @@ import (
var SharedManager = NewManager()
type GCAble interface {
GC()
}
type Manager struct {
ticker *time.Ticker
locker sync.Mutex
cacheMap map[*Cache]zero.Zero
cacheMap map[GCAble]zero.Zero
}
func NewManager() *Manager {
var manager = &Manager{
ticker: time.NewTicker(2 * time.Second),
cacheMap: map[*Cache]zero.Zero{},
cacheMap: map[GCAble]zero.Zero{},
}
goman.New(func() {
@@ -41,13 +45,13 @@ func (this *Manager) init() {
}
}
func (this *Manager) Add(cache *Cache) {
func (this *Manager) Add(cache GCAble) {
this.locker.Lock()
this.cacheMap[cache] = zero.New()
this.locker.Unlock()
}
func (this *Manager) Remove(cache *Cache) {
func (this *Manager) Remove(cache GCAble) {
this.locker.Lock()
delete(this.cacheMap, cache)
this.locker.Unlock()