Skip to content

Commit 496ee6c

Browse files
committed
优化代码
1 parent 437914a commit 496ee6c

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

internal/caches/item.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (this *Item) IsExpired() bool {
2323
}
2424

2525
func (this *Item) TotalSize() int64 {
26-
return this.Size() + this.MetaSize + int64(len(this.Key))
26+
return this.Size() + this.MetaSize + int64(len(this.Key)) + 64
2727
}
2828

2929
func (this *Item) Size() int64 {

internal/caches/storage_memory.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ func NewMemoryStorage(policy *serverconfigs.HTTPCachePolicy) *MemoryStorage {
4242
// Init 初始化
4343
func (this *MemoryStorage) Init() error {
4444
this.list.OnAdd(func(item *Item) {
45-
atomic.AddInt64(&this.totalSize, item.Size())
45+
atomic.AddInt64(&this.totalSize, item.TotalSize())
4646
})
4747
this.list.OnRemove(func(item *Item) {
48-
atomic.AddInt64(&this.totalSize, -item.Size())
48+
atomic.AddInt64(&this.totalSize, -item.TotalSize())
4949
})
5050

5151
if this.purgeDuration <= 0 {
@@ -118,7 +118,7 @@ func (this *MemoryStorage) Delete(key string) error {
118118
hash := this.hash(key)
119119
this.locker.Lock()
120120
delete(this.valuesMap, hash)
121-
this.list.Remove(fmt.Sprintf("%d", hash))
121+
_ = this.list.Remove(fmt.Sprintf("%d", hash))
122122
this.locker.Unlock()
123123
return nil
124124
}
@@ -137,7 +137,7 @@ func (this *MemoryStorage) Stat() (*Stat, error) {
137137
func (this *MemoryStorage) CleanAll() error {
138138
this.locker.Lock()
139139
this.valuesMap = map[uint64]*MemoryItem{}
140-
this.list.Reset()
140+
_ = this.list.Reset()
141141
atomic.StoreInt64(&this.totalSize, 0)
142142
this.locker.Unlock()
143143
return nil
@@ -173,7 +173,7 @@ func (this *MemoryStorage) Stop() {
173173
defer this.locker.Unlock()
174174

175175
this.valuesMap = map[uint64]*MemoryItem{}
176-
this.list.Reset()
176+
_ = this.list.Reset()
177177
if this.ticker != nil {
178178
this.ticker.Stop()
179179
}
@@ -188,7 +188,7 @@ func (this *MemoryStorage) Policy() *serverconfigs.HTTPCachePolicy {
188188
func (this *MemoryStorage) AddToList(item *Item) {
189189
item.MetaSize = int64(len(item.Key)) + 32 /** 32是我们评估的数据结构的长度 **/
190190
hash := fmt.Sprintf("%d", this.hash(item.Key))
191-
this.list.Add(hash, item)
191+
_ = this.list.Add(hash, item)
192192
}
193193

194194
// TotalDiskSize 消耗的磁盘尺寸

0 commit comments

Comments
 (0)