Something went wrong. Try again.
A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
Something went wrong. Try again.
Go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206// SiYuan - Refactor your thinking// Copyright (c) 2020-present, b3log.org//// This program is free software: you can redistribute it and/or modify// it under the terms of the GNU Affero General Public License as published by// the Free Software Foundation, either version 3 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU Affero General Public License for more details.//// You should have received a copy of the GNU Affero General Public License// along with this program. If not, see <https://www.gnu.org/licenses/>.
package cache
import ( "io/fs" "path/filepath" "strings" "sync" "time"
"github.com/siyuan-note/filelock" "github.com/siyuan-note/logging" "github.com/siyuan-note/siyuan/kernel/util")
type AssetHash struct { Hash string `json:"hash"` Path string `json:"path"`}
var ( assetHashCache = map[string]*AssetHash{} assetPathHashCache = map[string]*AssetHash{} assetHashLock = sync.Mutex{})
func RemoveAssetHash(hash string) { assetHashLock.Lock() defer assetHashLock.Unlock()
asset := assetHashCache[hash] if nil != asset { delete(assetHashCache, hash) delete(assetPathHashCache, asset.Path) }}
func SetAssetHash(hash, path string) { assetHashLock.Lock() defer assetHashLock.Unlock()
if !strings.HasPrefix(path, "assets/") { logging.LogErrorf("invalid asset path [%s]", path) return }
assetHashCache[hash] = &AssetHash{Hash: hash, Path: path} assetPathHashCache[path] = &AssetHash{Hash: hash, Path: path}}
func GetAssetHashByPath(path string) *AssetHash { assetHashLock.Lock() defer assetHashLock.Unlock()
asset, exists := assetPathHashCache[path] if exists { if filelock.IsExist(filepath.Join(util.DataDir, asset.Path)) { return asset }
delete(assetHashCache, asset.Hash) delete(assetPathHashCache, path) return nil } return nil}
func GetAssetHash(hash string) *AssetHash { assetHashLock.Lock() defer assetHashLock.Unlock()
// 直接使用 hash 作为 key 进行查找 asset, exists := assetHashCache[hash] if !exists { return nil }
// 验证文件是否存在 if !filelock.IsExist(filepath.Join(util.DataDir, asset.Path)) { // 文件不存在,清理缓存 delete(assetHashCache, hash) delete(assetPathHashCache, asset.Path) return nil } return asset}
type Asset struct { HName string `json:"hName"` Path string `json:"path"` Updated int64 `json:"updated"`}
var ( assetsCache = map[string]*Asset{} assetsLock = sync.Mutex{})
// IterateAssets 遍历所有资源,适合只读场景func IterateAssets(fn func(path string, asset *Asset) bool) { assetsLock.Lock() defer assetsLock.Unlock()
for path, asset := range assetsCache { if !fn(path, asset) { break } }}
// FilterAssets 根据过滤函数返回符合条件的资源func FilterAssets(filter func(path string, asset *Asset) bool) (ret map[string]*Asset) { assetsLock.Lock() defer assetsLock.Unlock()
ret = map[string]*Asset{} for path, asset := range assetsCache { if filter(path, asset) { ret[path] = asset } } return}
// GetAssetByPath 根据路径获取资源func GetAssetByPath(path string) *Asset { assetsLock.Lock() defer assetsLock.Unlock()
return assetsCache[path]}
func RemoveAsset(path string) { assetsLock.Lock() defer assetsLock.Unlock()
delete(assetsCache, path)}
func ExistAsset(path string) (ret bool) { assetsLock.Lock() defer assetsLock.Unlock()
_, ret = assetsCache[path] return}
func LoadAssets() { defer logging.Recover()
start := time.Now() assetsLock.Lock() defer assetsLock.Unlock()
assetsCache = map[string]*Asset{} assets := util.GetDataAssetsAbsPath() filelock.Walk(assets, func(path string, d fs.DirEntry, err error) error { if nil != err || nil == d { return err } if d.IsDir() { if strings.HasPrefix(d.Name(), ".") { return filepath.SkipDir } return nil } if strings.HasSuffix(d.Name(), ".sya") || strings.HasPrefix(d.Name(), ".") || filelock.IsHidden(path) { return nil }
info, err := d.Info() if nil != err { logging.LogErrorf("load assets failed: %s", err) return nil }
hName := util.RemoveID(d.Name()) path = "assets" + filepath.ToSlash(strings.TrimPrefix(path, assets)) assetsCache[path] = &Asset{ HName: hName, Path: path, Updated: info.ModTime().Unix(), } return nil }) elapsed := time.Since(start) if 2000 < elapsed.Milliseconds() { logging.LogInfof("loaded assets [%.2fs]", elapsed.Seconds()) }}