filecache

package
v0.0.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FilecacheRootDirname = "filecache"
)

Variables

View Source
var ErrFatal = errors.New("fatal filecache error")

ErrFatal can be used to signal an unrecoverable error.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	Fs afero.Fs
	// contains filtered or unexported fields
}

Cache caches a set of files in a directory. This is usually a file on disk, but since this is backed by an Afero file system, it can be anything.

func NewCache

func NewCache(fs afero.Fs, maxAge time.Duration, pruneAllRootDir string) *Cache

NewCache creates a new file cache with the given filesystem and max age.

func (*Cache) Get

func (c *Cache) Get(id string) (ItemInfo, io.ReadCloser, error)

Get gets the file with the given id from the cache, nil if none found.

func (*Cache) GetBytes

func (c *Cache) GetBytes(id string) (ItemInfo, []byte, error)

GetBytes gets the file content with the given id from the cache, nil if none found.

func (*Cache) GetOrCreate

func (c *Cache) GetOrCreate(id string, create func() (io.ReadCloser, error)) (ItemInfo, io.ReadCloser, error)

GetOrCreate tries to get the file with the given id from cache. If not found or expired, create will be invoked and the result cached. This method is protected by a named lock using the given id as identifier.

func (*Cache) GetOrCreateBytes

func (c *Cache) GetOrCreateBytes(id string, create func() ([]byte, error)) (ItemInfo, []byte, error)

GetOrCreateBytes is the same as GetOrCreate, but produces a byte slice.

func (*Cache) GetString

func (c *Cache) GetString(id string) string

For testing

func (*Cache) ReadOrCreate

func (c *Cache) ReadOrCreate(id string,
	read func(info ItemInfo, r io.ReadSeeker) error,
	create func(info ItemInfo, w io.WriteCloser) error,
) (info ItemInfo, err error)

ReadOrCreate tries to lookup the file in cache. If found, it is passed to read and then closed. If not found a new file is created and passed to create, which should close it when done.

func (*Cache) WriteCloser

func (c *Cache) WriteCloser(id string) (ItemInfo, io.WriteCloser, error)

WriteCloser returns a transactional writer into the cache. It's important that it's closed when done.

type Caches

type Caches map[string]*Cache

Caches is a named set of caches.

func (Caches) AssetsCache

func (f Caches) AssetsCache() *Cache

AssetsCache gets the file cache for assets (processed resources, SCSS etc.).

func (Caches) Get

func (f Caches) Get(name string) *Cache

Get gets a named cache, nil if none found.

func (Caches) GetResourceCache

func (f Caches) GetResourceCache() *Cache

GetResourceCache gets the file cache for remote resources.

func (Caches) ImageCache

func (f Caches) ImageCache() *Cache

type ItemInfo

type ItemInfo struct {
	// This is the file's name relative to the cache's filesystem.
	Name string
}

ItemInfo contains info about a cached file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL