Files
EdgeAPI/internal/utils/cache_map.go

42 lines
681 B
Go
Raw Normal View History

2024-05-17 18:27:26 +08:00
// Copyright 2021 GoEdge CDN goedge.cdn@gmail.com. All rights reserved.
2021-11-11 14:16:42 +08:00
package utils
import (
"sync"
2024-07-27 14:15:25 +08:00
"github.com/iwind/TeaGo/maps"
2021-11-11 14:16:42 +08:00
)
type CacheMap struct {
locker sync.Mutex
m maps.Map
}
func NewCacheMap() *CacheMap {
return &CacheMap{m: maps.Map{}}
}
func (this *CacheMap) Get(key string) (value interface{}, ok bool) {
this.locker.Lock()
value, ok = this.m[key]
this.locker.Unlock()
return
}
func (this *CacheMap) Put(key string, value interface{}) {
if value == nil {
return
}
this.locker.Lock()
this.m[key] = value
this.locker.Unlock()
}
func (this *CacheMap) Len() int {
this.locker.Lock()
var l = len(this.m)
this.locker.Unlock()
return l
}