1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 03:37:37 -05:00
v2fly/common/collect/validity_map.go

73 lines
1.2 KiB
Go
Raw Normal View History

2015-12-14 08:14:10 -05:00
package collect
import (
"sync"
2016-02-01 17:21:44 -05:00
"sync/atomic"
2015-12-14 08:14:10 -05:00
"github.com/v2ray/v2ray-core/common/serial"
)
type Validity interface {
IsValid() bool
}
type entry struct {
key string
value Validity
}
type ValidityMap struct {
sync.RWMutex
2016-02-01 17:21:44 -05:00
cache map[string]Validity
opCount int32
2015-12-14 08:14:10 -05:00
}
func NewValidityMap(cleanupIntervalSec int) *ValidityMap {
instance := &ValidityMap{
2016-02-01 17:21:44 -05:00
cache: make(map[string]Validity),
2015-12-14 08:14:10 -05:00
}
return instance
}
func (this *ValidityMap) cleanup() {
2016-02-01 17:21:44 -05:00
entry2Remove := make([]entry, 0, 128)
this.RLock()
for key, value := range this.cache {
if !value.IsValid() {
entry2Remove = append(entry2Remove, entry{
key: key,
value: value,
})
2015-12-14 08:14:10 -05:00
}
2016-02-01 17:21:44 -05:00
}
this.RUnlock()
2015-12-14 08:14:10 -05:00
2016-02-01 17:21:44 -05:00
for _, entry := range entry2Remove {
if !entry.value.IsValid() {
this.Lock()
delete(this.cache, entry.key)
this.Unlock()
2015-12-14 08:14:10 -05:00
}
}
}
func (this *ValidityMap) Set(key serial.String, value Validity) {
this.Lock()
this.cache[key.String()] = value
this.Unlock()
2016-02-01 17:21:44 -05:00
opCount := atomic.AddInt32(&this.opCount, 1)
if opCount > 1000 {
atomic.StoreInt32(&this.opCount, 0)
go this.cleanup()
}
2015-12-14 08:14:10 -05:00
}
func (this *ValidityMap) Get(key serial.String) Validity {
this.RLock()
defer this.RUnlock()
if value, found := this.cache[key.String()]; found {
return value
}
return nil
}