mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-16 13:08:25 +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
Let preview renderers supply extra cache key data instead of checking for the cmd_renderer type. Include the command line in the key for custom previews and the video_preview settings in the key for video thumbnails, so that changing them no longer shows stale previews. GetPath and AddPath now take this extra key data, replacing GetPathWithAlias. Track the entry each path uses per extra key, so that previews of one file with different settings do not evict each other. Do not remove an entry while another path, such as another symlink to the same file, still uses it. Cache hits are now recorded so that such shared entries are known. Failing to remove an orphaned entry on a cache hit no longer turns the hit into an error. Fix ensure_entries() discarding unwritten changes when the entries file does not exist yet, which lost the first path recorded in a new cache. Also reset the path map when clearing the cache.
119 lines
3.1 KiB
Go
119 lines
3.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)
|
|
}
|
|
|
|
// GetPath returns the cache key for the file at path and the cached items for
|
|
// that key. The key depends on the resolved path of the file, its size and its
|
|
// modification time. Use extra_key to add anything else the cached data
|
|
// depends on, such as the command used to generate it. extra_key is used
|
|
// verbatim, it is not normalized in any way. Pass the same path and extra_key
|
|
// to AddPath() when adding the items for key.
|
|
func (dc *DiskCache) GetPath(path, extra_key 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, extra_key); err != nil {
|
|
return "", nil, err
|
|
}
|
|
if ans, err = dc.get(key, items); err == nil && len(ans) > 0 {
|
|
// Record which entry path now uses so that its previous entry is
|
|
// removed once no other path uses it.
|
|
if orphan := dc.set_key_for_path(path_map_key(path, extra_key), key); orphan != "" {
|
|
// Removing the orphan is only cleanup, failing to do so must not
|
|
// discard the valid result. The orphan is still tracked, so
|
|
// pruning removes it eventually.
|
|
_ = dc.remove(orphan)
|
|
}
|
|
err = dc.write_entries_if_dirty()
|
|
}
|
|
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()
|
|
}
|
|
|
|
// AddPath adds items for the key returned by GetPath(path, extra_key).
|
|
func (dc *DiskCache) AddPath(path, extra_key, 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_map_key(path, extra_key), 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)
|
|
}
|