代码拉取完成,页面将自动刷新
package gguf_parser
import (
"errors"
"fmt"
"os"
"path/filepath"
"time"
"github.com/gpustack/gguf-parser-go/util/json"
"github.com/gpustack/gguf-parser-go/util/osx"
"github.com/gpustack/gguf-parser-go/util/stringx"
)
var (
ErrGGUFFileCacheDisabled = errors.New("GGUF file cache disabled")
ErrGGUFFileCacheMissed = errors.New("GGUF file cache missed")
ErrGGUFFileCacheCorrupted = errors.New("GGUF file cache corrupted")
)
type GGUFFileCache string
func (c GGUFFileCache) getKeyPath(key string) string {
k := stringx.SumByFNV64a(key)
p := filepath.Join(string(c), k[:1], k)
return p
}
func (c GGUFFileCache) Get(key string, exp time.Duration) (*GGUFFile, error) {
if c == "" {
return nil, ErrGGUFFileCacheDisabled
}
if key == "" {
return nil, ErrGGUFFileCacheMissed
}
p := c.getKeyPath(key)
if !osx.Exists(p, func(stat os.FileInfo) bool {
if !stat.Mode().IsRegular() {
return false
}
return exp == 0 || time.Since(stat.ModTime()) < exp
}) {
return nil, ErrGGUFFileCacheMissed
}
var gf GGUFFile
{
bs, err := os.ReadFile(p)
if err != nil {
return nil, fmt.Errorf("GGUF file cache get: %w", err)
}
if err = json.Unmarshal(bs, &gf); err != nil {
return nil, fmt.Errorf("GGUF file cache get: %w", err)
}
}
if len(gf.TensorInfos) == 0 {
_ = os.Remove(p)
return nil, ErrGGUFFileCacheCorrupted
}
return &gf, nil
}
func (c GGUFFileCache) Put(key string, gf *GGUFFile) error {
if c == "" {
return ErrGGUFFileCacheDisabled
}
if key == "" || gf == nil {
return nil
}
bs, err := json.Marshal(gf)
if err != nil {
return fmt.Errorf("GGUF file cache put: %w", err)
}
p := c.getKeyPath(key)
if err = osx.WriteFile(p, bs, 0o600); err != nil {
return fmt.Errorf("GGUF file cache put: %w", err)
}
return nil
}
func (c GGUFFileCache) Delete(key string) error {
if c == "" {
return ErrGGUFFileCacheDisabled
}
if key == "" {
return ErrGGUFFileCacheMissed
}
p := c.getKeyPath(key)
if !osx.ExistsFile(p) {
return ErrGGUFFileCacheMissed
}
if err := os.Remove(p); err != nil {
return fmt.Errorf("GGUF file cache delete: %w", err)
}
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。