Change: use bbolt as cache db

This commit is contained in:
Dreamacro 2021-10-04 19:20:11 +08:00
parent ced9749104
commit 537b672fcf
4 changed files with 83 additions and 46 deletions

View File

@ -10,45 +10,40 @@ import (
"github.com/Dreamacro/clash/component/profile" "github.com/Dreamacro/clash/component/profile"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log" "github.com/Dreamacro/clash/log"
bolt "go.etcd.io/bbolt"
) )
var ( var (
initOnce sync.Once initOnce sync.Once
fileMode os.FileMode = 0666 fileMode os.FileMode = 0666
defaultCache *CacheFile defaultCache *CacheFile
)
type cache struct { bucketSelected = []byte("selected")
Selected map[string]string )
}
// CacheFile store and update the cache file // CacheFile store and update the cache file
type CacheFile struct { type CacheFile struct {
path string db *bolt.DB
model *cache
buf *bytes.Buffer
mux sync.Mutex
} }
func (c *CacheFile) SetSelected(group, selected string) { func (c *CacheFile) SetSelected(group, selected string) {
if !profile.StoreSelected.Load() { if !profile.StoreSelected.Load() {
return return
} } else if c.db == nil {
c.mux.Lock()
defer c.mux.Unlock()
model := c.element()
model.Selected[group] = selected
c.buf.Reset()
if err := gob.NewEncoder(c.buf).Encode(model); err != nil {
log.Warnln("[CacheFile] encode gob failed: %s", err.Error())
return return
} }
if err := ioutil.WriteFile(c.path, c.buf.Bytes(), fileMode); err != nil { err := c.db.Batch(func(t *bolt.Tx) error {
log.Warnln("[CacheFile] write cache to %s failed: %s", c.path, err.Error()) bucket, err := t.CreateBucketIfNotExists(bucketSelected)
if err != nil {
return err
}
return bucket.Put([]byte(group), []byte(selected))
})
if err != nil {
log.Warnln("[CacheFile] write cache to %s failed: %s", c.db.Path(), err.Error())
return return
} }
} }
@ -56,46 +51,80 @@ func (c *CacheFile) SetSelected(group, selected string) {
func (c *CacheFile) SelectedMap() map[string]string { func (c *CacheFile) SelectedMap() map[string]string {
if !profile.StoreSelected.Load() { if !profile.StoreSelected.Load() {
return nil return nil
} else if c.db == nil {
return nil
} }
c.mux.Lock()
defer c.mux.Unlock()
model := c.element()
mapping := map[string]string{} mapping := map[string]string{}
for k, v := range model.Selected { c.db.View(func(t *bolt.Tx) error {
mapping[k] = v bucket := t.Bucket(bucketSelected)
} if bucket == nil {
return nil
}
c := bucket.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
mapping[string(k)] = string(v)
}
return nil
})
return mapping return mapping
} }
func (c *CacheFile) element() *cache { func (c *CacheFile) Close() error {
if c.model != nil { return c.db.Close()
return c.model }
}
func migrateCache() {
defer func() {
db, err := bolt.Open(C.Path.Cache(), fileMode, nil)
if err != nil {
log.Warnln("[CacheFile] can't open cache file: %s", err.Error())
}
defaultCache = &CacheFile{
db: db,
}
}()
buf, err := ioutil.ReadFile(C.Path.OldCache())
if err != nil {
return
}
defer os.Remove(C.Path.OldCache())
// read old cache file
type cache struct {
Selected map[string]string
}
model := &cache{ model := &cache{
Selected: map[string]string{}, Selected: map[string]string{},
} }
bufReader := bytes.NewBuffer(buf)
gob.NewDecoder(bufReader).Decode(model)
if buf, err := ioutil.ReadFile(c.path); err == nil { // write to new cache file
bufReader := bytes.NewBuffer(buf) db, err := bolt.Open(C.Path.Cache(), fileMode, nil)
gob.NewDecoder(bufReader).Decode(model) if err != nil {
return
} }
defer db.Close()
c.model = model db.Batch(func(t *bolt.Tx) error {
return c.model bucket, err := t.CreateBucketIfNotExists(bucketSelected)
if err != nil {
return err
}
for group, selected := range model.Selected {
if err := bucket.Put([]byte(group), []byte(selected)); err != nil {
return err
}
}
return nil
})
} }
// Cache return singleton of CacheFile // Cache return singleton of CacheFile
func Cache() *CacheFile { func Cache() *CacheFile {
initOnce.Do(func() { initOnce.Do(migrateCache)
defaultCache = &CacheFile{
path: C.Path.Cache(),
buf: &bytes.Buffer{},
}
})
return defaultCache return defaultCache
} }

View File

@ -55,6 +55,10 @@ func (p *path) MMDB() string {
return P.Join(p.homeDir, "Country.mmdb") return P.Join(p.homeDir, "Country.mmdb")
} }
func (p *path) Cache() string { func (p *path) OldCache() string {
return P.Join(p.homeDir, ".cache") return P.Join(p.homeDir, ".cache")
} }
func (p *path) Cache() string {
return P.Join(p.homeDir, "cache.db")
}

1
go.mod
View File

@ -14,6 +14,7 @@ require (
github.com/oschwald/geoip2-golang v1.5.0 github.com/oschwald/geoip2-golang v1.5.0
github.com/sirupsen/logrus v1.8.1 github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0 github.com/stretchr/testify v1.7.0
go.etcd.io/bbolt v1.3.6
go.uber.org/atomic v1.9.0 go.uber.org/atomic v1.9.0
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
golang.org/x/net v0.0.0-20210903162142-ad29c8ab022f golang.org/x/net v0.0.0-20210903162142-ad29c8ab022f

3
go.sum
View File

@ -55,6 +55,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/u-root/uio v0.0.0-20210528114334-82958018845c h1:BFvcl34IGnw8yvJi8hlqLFo9EshRInwWBs2M5fGWzQA= github.com/u-root/uio v0.0.0-20210528114334-82958018845c h1:BFvcl34IGnw8yvJi8hlqLFo9EshRInwWBs2M5fGWzQA=
github.com/u-root/uio v0.0.0-20210528114334-82958018845c/go.mod h1:LpEX5FO/cB+WF4TYGY1V5qktpaZLkKkSegbr0V4eYXA= github.com/u-root/uio v0.0.0-20210528114334-82958018845c/go.mod h1:LpEX5FO/cB+WF4TYGY1V5qktpaZLkKkSegbr0V4eYXA=
go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU=
go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@ -87,6 +89,7 @@ golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191224085550-c709ea063b76/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191224085550-c709ea063b76/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201101102859-da207088b7d1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201101102859-da207088b7d1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=