mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-02-26 16:57:08 +00:00
chore: save etag in bbolt by msgpack
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
package cachefile
|
||||
|
||||
import (
|
||||
"math"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/metacubex/mihomo/common/utils"
|
||||
"github.com/metacubex/mihomo/component/profile"
|
||||
C "github.com/metacubex/mihomo/constant"
|
||||
"github.com/metacubex/mihomo/log"
|
||||
@@ -72,58 +70,6 @@ func (c *CacheFile) SelectedMap() map[string]string {
|
||||
return mapping
|
||||
}
|
||||
|
||||
func (c *CacheFile) SetETagWithHash(url string, hash utils.HashType, etag string) {
|
||||
if c.DB == nil {
|
||||
return
|
||||
}
|
||||
|
||||
lenHash := hash.Len()
|
||||
if lenHash > math.MaxUint8 {
|
||||
return // maybe panic is better
|
||||
}
|
||||
|
||||
data := make([]byte, 1, 1+lenHash+len(etag))
|
||||
data[0] = uint8(lenHash)
|
||||
data = append(data, hash.Bytes()...)
|
||||
data = append(data, etag...)
|
||||
|
||||
err := c.DB.Batch(func(t *bbolt.Tx) error {
|
||||
bucket, err := t.CreateBucketIfNotExists(bucketETag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return bucket.Put([]byte(url), data)
|
||||
})
|
||||
if err != nil {
|
||||
log.Warnln("[CacheFile] write cache to %s failed: %s", c.DB.Path(), err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
func (c *CacheFile) GetETagWithHash(key string) (hash utils.HashType, etag string) {
|
||||
if c.DB == nil {
|
||||
return
|
||||
}
|
||||
c.DB.View(func(t *bbolt.Tx) error {
|
||||
if bucket := t.Bucket(bucketETag); bucket != nil {
|
||||
if v := bucket.Get([]byte(key)); v != nil {
|
||||
if len(v) == 0 {
|
||||
return nil
|
||||
}
|
||||
lenHash := int(v[0])
|
||||
if len(v) < 1+lenHash {
|
||||
return nil
|
||||
}
|
||||
hash = utils.MakeHashFromBytes(v[1 : 1+lenHash])
|
||||
etag = string(v[1+lenHash:])
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (c *CacheFile) Close() error {
|
||||
return c.DB.Close()
|
||||
}
|
||||
|
||||
58
component/profile/cachefile/etag.go
Normal file
58
component/profile/cachefile/etag.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package cachefile
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/metacubex/mihomo/common/utils"
|
||||
"github.com/metacubex/mihomo/log"
|
||||
|
||||
"github.com/metacubex/bbolt"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
)
|
||||
|
||||
type EtagWithHash struct {
|
||||
Hash utils.HashType
|
||||
ETag string
|
||||
Time time.Time
|
||||
}
|
||||
|
||||
func (c *CacheFile) SetETagWithHash(url string, etagWithHash EtagWithHash) {
|
||||
if c.DB == nil {
|
||||
return
|
||||
}
|
||||
|
||||
data, err := msgpack.Marshal(etagWithHash)
|
||||
if err != nil {
|
||||
return // maybe panic is better
|
||||
}
|
||||
|
||||
err = c.DB.Batch(func(t *bbolt.Tx) error {
|
||||
bucket, err := t.CreateBucketIfNotExists(bucketETag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return bucket.Put([]byte(url), data)
|
||||
})
|
||||
if err != nil {
|
||||
log.Warnln("[CacheFile] write cache to %s failed: %s", c.DB.Path(), err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
func (c *CacheFile) GetETagWithHash(key string) (etagWithHash EtagWithHash) {
|
||||
if c.DB == nil {
|
||||
return
|
||||
}
|
||||
c.DB.View(func(t *bbolt.Tx) error {
|
||||
if bucket := t.Bucket(bucketETag); bucket != nil {
|
||||
if v := bucket.Get([]byte(key)); v != nil {
|
||||
if err := msgpack.Unmarshal(v, &etagWithHash); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user