mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2026-01-04 22:55:48 +08:00
使用空struct{}代替bool节约内存
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
package expires
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/zero"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type ItemMap = map[int64]bool
|
||||
type ItemMap = map[int64]zero.Zero
|
||||
|
||||
type List struct {
|
||||
expireMap map[int64]ItemMap // expires timestamp => map[id]bool
|
||||
expireMap map[int64]ItemMap // expires timestamp => map[id]ItemMap
|
||||
itemsMap map[int64]int64 // itemId => timestamp
|
||||
|
||||
locker sync.Mutex
|
||||
@@ -38,10 +39,10 @@ func (this *List) Add(itemId int64, expiresAt int64) {
|
||||
|
||||
expireItemMap, ok := this.expireMap[expiresAt]
|
||||
if ok {
|
||||
expireItemMap[itemId] = true
|
||||
expireItemMap[itemId] = zero.New()
|
||||
} else {
|
||||
expireItemMap = ItemMap{
|
||||
itemId: true,
|
||||
itemId: zero.New(),
|
||||
}
|
||||
this.expireMap[expiresAt] = expireItemMap
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ package expires
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/zero"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -11,14 +12,14 @@ import (
|
||||
var SharedManager = NewManager()
|
||||
|
||||
type Manager struct {
|
||||
listMap map[*List]bool
|
||||
listMap map[*List]zero.Zero
|
||||
locker sync.Mutex
|
||||
ticker *time.Ticker
|
||||
}
|
||||
|
||||
func NewManager() *Manager {
|
||||
var manager = &Manager{
|
||||
listMap: map[*List]bool{},
|
||||
listMap: map[*List]zero.Zero{},
|
||||
ticker: time.NewTicker(1 * time.Second),
|
||||
}
|
||||
goman.New(func() {
|
||||
@@ -60,7 +61,7 @@ func (this *Manager) init() {
|
||||
|
||||
func (this *Manager) Add(list *List) {
|
||||
this.locker.Lock()
|
||||
this.listMap[list] = true
|
||||
this.listMap[list] = zero.New()
|
||||
this.locker.Unlock()
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/zero"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -8,7 +9,7 @@ import (
|
||||
// Ticker 类似于time.Ticker,但能够真正地停止
|
||||
type Ticker struct {
|
||||
raw *time.Ticker
|
||||
done chan bool
|
||||
done chan zero.Zero
|
||||
once sync.Once
|
||||
|
||||
C <-chan time.Time
|
||||
@@ -20,7 +21,7 @@ func NewTicker(duration time.Duration) *Ticker {
|
||||
return &Ticker{
|
||||
raw: raw,
|
||||
C: raw.C,
|
||||
done: make(chan bool, 1),
|
||||
done: make(chan zero.Zero, 1),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +39,6 @@ func (this *Ticker) Next() bool {
|
||||
func (this *Ticker) Stop() {
|
||||
this.once.Do(func() {
|
||||
this.raw.Stop()
|
||||
this.done <- true
|
||||
this.done <- zero.New()
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user