github.com/TeaOSLab/EdgeNode@v1.3.8/internal/caches/item.go (about)

     1  package caches
     2  
     3  import (
     4  	"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
     5  	"strings"
     6  )
     7  
     8  type ItemType = int
     9  
    10  const (
    11  	ItemTypeFile   ItemType = 1
    12  	ItemTypeMemory ItemType = 2
    13  )
    14  
    15  // 计算当前周
    16  // 不要用YW,因为需要计算两周是否临近
    17  func currentWeek() int32 {
    18  	return int32(fasttime.Now().Unix() / 86400)
    19  }
    20  
    21  type Item struct {
    22  	Type       ItemType `json:"-"`
    23  	Key        string   `json:"1,omitempty"`
    24  	ExpiresAt  int64    `json:"2,omitempty"`
    25  	StaleAt    int64    `json:"3,omitempty"`
    26  	HeaderSize int64    `json:"-"`
    27  	BodySize   int64    `json:"4,omitempty"`
    28  	MetaSize   int64    `json:"-"`
    29  	Host       string   `json:"-"`           // 主机名
    30  	ServerId   int64    `json:"5,omitempty"` // 服务ID
    31  	Week       int32    `json:"-"`
    32  	CreatedAt  int64    `json:"6,omitempty"`
    33  }
    34  
    35  func (this *Item) IsExpired() bool {
    36  	return this.ExpiresAt < fasttime.Now().Unix()
    37  }
    38  
    39  func (this *Item) TotalSize() int64 {
    40  	return this.Size() + this.MetaSize + int64(len(this.Key)) + int64(len(this.Host))
    41  }
    42  
    43  func (this *Item) Size() int64 {
    44  	return this.HeaderSize + this.BodySize
    45  }
    46  
    47  func (this *Item) RequestURI() string {
    48  	var schemeIndex = strings.Index(this.Key, "://")
    49  	if schemeIndex <= 0 {
    50  		return ""
    51  	}
    52  
    53  	var firstSlashIndex = strings.Index(this.Key[schemeIndex+3:], "/")
    54  	if firstSlashIndex <= 0 {
    55  		return ""
    56  	}
    57  
    58  	return this.Key[schemeIndex+3+firstSlashIndex:]
    59  }