mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-16 21:20:08 +00:00
Some checks are pending
CI / Linux (python=3.14 cc=clang sanitize=1) (push) Waiting to run
CI / Linux (python=3.12 cc=gcc sanitize=0) (push) Waiting to run
CI / Linux (python=3.13 cc=gcc sanitize=1) (push) Waiting to run
CI / Linux package (push) Waiting to run
CI / Bundle test (macos-latest) (push) Waiting to run
CI / Bundle test (ubuntu-latest) (push) Waiting to run
CI / macOS Brew (push) Waiting to run
CI / Test ./dev.sh and benchmark (push) Waiting to run
CodeQL / CodeQL-Build (actions, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, macos-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (go, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (python, ubuntu-latest) (push) Waiting to run
Depscan / Scan dependencies for vulnerabilities (push) Waiting to run
Replace exported get_dir files atomically so paths returned by Get/Add never briefly vanish while another worker re-exports the same key. Don't panic in Get for a key folder missing from entries.json. Propagate errors from unlocking, document the lock()/unlock() contract, and fix the NewDiskCache lock error test which checked the wrong cache
102 lines
2.1 KiB
Go
102 lines
2.1 KiB
Go
package disk_cache
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
var _ = fmt.Print
|
|
|
|
type Entry struct {
|
|
Key string
|
|
Size int64
|
|
LastUsed time.Time
|
|
}
|
|
|
|
type Metadata struct {
|
|
TotalSize int64
|
|
PathMap map[string]string
|
|
SortedEntries []*Entry
|
|
}
|
|
|
|
type DiskCache struct {
|
|
Path string
|
|
MaxSize int64
|
|
|
|
lock_file *os.File
|
|
lock_mutex sync.Mutex
|
|
entries Metadata
|
|
entry_map map[string]*Entry
|
|
entries_last_read_state *file_state
|
|
entries_dirty bool
|
|
get_dir string
|
|
read_count int
|
|
}
|
|
|
|
func NewDiskCache(path string, max_size int64) (dc *DiskCache, err error) {
|
|
return new_disk_cache(path, max_size)
|
|
}
|
|
|
|
func (dc *DiskCache) ResultsDir() string {
|
|
return dc.get_dir
|
|
}
|
|
|
|
func (dc *DiskCache) unlock_and_set_err(err *error) {
|
|
if ue := dc.unlock(); ue != nil && *err == nil {
|
|
*err = ue
|
|
}
|
|
}
|
|
|
|
func (dc *DiskCache) Get(key string, items ...string) (ans map[string]string, err error) {
|
|
if err = dc.lock(); err != nil {
|
|
return
|
|
}
|
|
defer dc.unlock_and_set_err(&err)
|
|
return dc.get(key, items)
|
|
}
|
|
|
|
func (dc *DiskCache) GetPath(path string, items ...string) (key string, ans map[string]string, err error) {
|
|
if err = dc.lock(); err != nil {
|
|
return
|
|
}
|
|
defer dc.unlock_and_set_err(&err)
|
|
if key, err = key_for_path(path); err != nil {
|
|
return "", nil, err
|
|
}
|
|
ans, err = dc.get(key, items)
|
|
return
|
|
}
|
|
|
|
func (dc *DiskCache) Remove(key string) (err error) {
|
|
if err = dc.lock(); err != nil {
|
|
return
|
|
}
|
|
defer dc.unlock_and_set_err(&err)
|
|
return dc.remove(key)
|
|
}
|
|
|
|
func (dc *DiskCache) Clear() (err error) {
|
|
if err = dc.lock(); err != nil {
|
|
return
|
|
}
|
|
defer dc.unlock_and_set_err(&err)
|
|
return dc.clear()
|
|
}
|
|
|
|
func (dc *DiskCache) AddPath(path, key string, items map[string][]byte) (ans map[string]string, err error) {
|
|
if err = dc.lock(); err != nil {
|
|
return
|
|
}
|
|
defer dc.unlock_and_set_err(&err)
|
|
return dc.add_path(path, key, items)
|
|
}
|
|
|
|
func (dc *DiskCache) Add(key string, items map[string][]byte) (ans map[string]string, err error) {
|
|
if err = dc.lock(); err != nil {
|
|
return
|
|
}
|
|
defer dc.unlock_and_set_err(&err)
|
|
return dc.add(key, items)
|
|
}
|