2020-10-04 14:30:42 +08:00
|
|
|
|
package caches
|
|
|
|
|
|
|
2021-06-11 14:53:51 +08:00
|
|
|
|
import (
|
2023-04-08 12:47:04 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
2022-11-26 11:05:46 +08:00
|
|
|
|
"strings"
|
2021-06-11 14:53:51 +08:00
|
|
|
|
)
|
2020-10-04 14:30:42 +08:00
|
|
|
|
|
2021-03-02 19:43:05 +08:00
|
|
|
|
type ItemType = int
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
ItemTypeFile ItemType = 1
|
|
|
|
|
|
ItemTypeMemory ItemType = 2
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2021-11-13 21:30:24 +08:00
|
|
|
|
// 计算当前周
|
|
|
|
|
|
// 不要用YW,因为需要计算两周是否临近
|
|
|
|
|
|
func currentWeek() int32 {
|
2023-09-14 18:30:11 +08:00
|
|
|
|
return int32(fasttime.Now().Unix() / 86400)
|
2021-11-13 21:30:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-04 14:30:42 +08:00
|
|
|
|
type Item struct {
|
2024-03-22 08:23:22 +08:00
|
|
|
|
Type ItemType `json:"-"`
|
|
|
|
|
|
Key string `json:"1,omitempty"`
|
|
|
|
|
|
ExpiresAt int64 `json:"2,omitempty"`
|
|
|
|
|
|
StaleAt int64 `json:"-"`
|
|
|
|
|
|
HeaderSize int64 `json:"-"`
|
|
|
|
|
|
BodySize int64 `json:"-"`
|
|
|
|
|
|
MetaSize int64 `json:"-"`
|
|
|
|
|
|
Host string `json:"-"` // 主机名
|
|
|
|
|
|
ServerId int64 `json:"3,omitempty"` // 服务ID
|
|
|
|
|
|
Week int32 `json:"-"`
|
2020-10-04 14:30:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *Item) IsExpired() bool {
|
2024-03-22 08:23:22 +08:00
|
|
|
|
return this.ExpiresAt < fasttime.Now().Unix()
|
2020-10-04 14:30:42 +08:00
|
|
|
|
}
|
2021-01-13 12:02:50 +08:00
|
|
|
|
|
|
|
|
|
|
func (this *Item) TotalSize() int64 {
|
2021-11-13 21:30:24 +08:00
|
|
|
|
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
|
|
|
|
|
|
}
|
2021-11-13 21:30:24 +08:00
|
|
|
|
|
2022-11-26 11:05:46 +08:00
|
|
|
|
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:]
|
|
|
|
|
|
}
|