Files
EdgeNode/internal/caches/item.go

77 lines
1.6 KiB
Go
Raw Normal View History

2020-10-04 14:30:42 +08:00
package caches
2021-06-11 14:53:51 +08:00
import (
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
"strings"
"time"
2021-06-11 14:53:51 +08:00
)
2020-10-04 14:30:42 +08:00
type ItemType = int
const (
ItemTypeFile ItemType = 1
ItemTypeMemory ItemType = 2
)
// 计算当前周
// 不要用YW因为需要计算两周是否临近
func currentWeek() int32 {
2021-11-15 09:15:23 +08:00
return int32(time.Now().Unix() / 86400)
}
2020-10-04 14:30:42 +08:00
type Item struct {
2021-05-19 12:07:35 +08:00
Type ItemType `json:"type"`
Key string `json:"key"`
ExpiredAt int64 `json:"expiredAt"`
2021-12-16 17:27:21 +08:00
StaleAt int64 `json:"staleAt"`
2021-05-19 12:07:35 +08:00
HeaderSize int64 `json:"headerSize"`
BodySize int64 `json:"bodySize"`
MetaSize int64 `json:"metaSize"`
2021-06-13 17:37:57 +08:00
Host string `json:"host"` // 主机名
ServerId int64 `json:"serverId"` // 服务ID
Week1Hits int64 `json:"week1Hits"`
Week2Hits int64 `json:"week2Hits"`
Week int32 `json:"week"`
2020-10-04 14:30:42 +08:00
}
func (this *Item) IsExpired() bool {
return this.ExpiredAt < fasttime.Now().Unix()
2020-10-04 14:30:42 +08:00
}
2021-01-13 12:02:50 +08:00
func (this *Item) TotalSize() int64 {
return this.Size() + this.MetaSize + int64(len(this.Key)) + int64(len(this.Host))
2021-01-13 12:02:50 +08:00
}
func (this *Item) Size() int64 {
return this.HeaderSize + this.BodySize
}
func (this *Item) IncreaseHit(week int32) {
if this.Week == week {
this.Week2Hits++
} else {
if week-this.Week == 1 {
this.Week1Hits = this.Week2Hits
} else {
this.Week1Hits = 0
}
this.Week2Hits = 1
this.Week = week
}
}
func (this *Item) RequestURI() string {
var schemeIndex = strings.Index(this.Key, "://")
if schemeIndex <= 0 {
return ""
}
var firstSlashIndex = strings.Index(this.Key[schemeIndex+3:], "/")
if firstSlashIndex <= 0 {
return ""
}
return this.Key[schemeIndex+3+firstSlashIndex:]
}