parsecache

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package parsecache provides a structure which caches parsed files and directory entries on top of a filesystem.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JsonParser

func JsonParser[T any](f io.Reader) (T, error)

JsonParser[T] is a value of type Parser[T] which parses a file as JSON.

Types

type CachedDir

type CachedDir struct {
	// contains filtered or unexported fields
}

CachedDir stores a cache entry for a directory.

func (*CachedDir) Cached

func (f *CachedDir) Cached() ([]fs.DirEntry, time.Time, bool)

Cached returns the cached entries, the time it was cached, and a boolean, which is true only if the cache entry has been loaded.

func (*CachedDir) Get

func (f *CachedDir) Get(open func() (fs.File, error), maxAge time.Duration) ([]fs.DirEntry, error)

Get the directory entries, the results may be cached upto the specified `maxAge`.

`open` should open the underlying file is required, this will be once or not at all.

type CachedFile

type CachedFile[T any] struct {
	// contains filtered or unexported fields
}

CachedFile stores a cache entry for a file.

func (*CachedFile[T]) Cached

func (f *CachedFile[T]) Cached() (T, time.Time, bool)

Cached returns the cached content, the time it was cached, and a boolean, which is true only if the cache entry has been loaded.

func (*CachedFile[T]) Get

func (f *CachedFile[T]) Get(open func() (fs.File, error), parser Parser[T], maxAge time.Duration) (T, error)

Get the parsed file content, the results may be cached upto the specified `maxAge`.

`open` should open the underlying file is required, this will be once or not at all.

type ConcurrentCachedDir

type ConcurrentCachedDir struct {
	// contains filtered or unexported fields
}

ConcurrentCachedDir is a concurrency-safe wrapper around a `CachedDir`.

func (*ConcurrentCachedDir) Cached

func (f *ConcurrentCachedDir) Cached() ([]fs.DirEntry, time.Time, bool)

Cached returns the cached entries, the time it was cached, and a boolean, which is true only if the cache entry has been loaded.

func (*ConcurrentCachedDir) Get

func (f *ConcurrentCachedDir) Get(open func() (fs.File, error), maxAge time.Duration) ([]fs.DirEntry, error)

Get the directory entries, the results may be cached upto the specified `maxAge`.

`open` should open the underlying file is required, this will be once or not at all.

type ConcurrentCachedFile

type ConcurrentCachedFile[T any] struct {
	// contains filtered or unexported fields
}

ConcurrentCachedFile is a concurrency-safe wrapper around a `CachedFile`.

func (*ConcurrentCachedFile[T]) Cached

func (f *ConcurrentCachedFile[T]) Cached() (T, time.Time, bool)

Cached returns the cached content, the time it was cached, and a boolean, which is true only if the cache entry has been loaded.

func (*ConcurrentCachedFile[T]) Get

func (f *ConcurrentCachedFile[T]) Get(open func() (fs.File, error), parser Parser[T], maxAge time.Duration) (T, error)

Get the parsed file content, the results may be cached upto the specified `maxAge`.

`open` should open the underlying file is required, this will be once or not at all.

type ConcurrentFsCache

type ConcurrentFsCache[T any] struct {
	// contains filtered or unexported fields
}

FsCache is a concurrency safe cache on top of a generic filesystem.

It can cache directory listings and parsed file content.

func NewConcurrentFsCache

func NewConcurrentFsCache[T any](fs fs.FS, parser Parser[T], maxAge time.Duration) *ConcurrentFsCache[T]

NewConcurrentFsCache returns a new cache on top of the `fs` filesystem, safe for concurrent access, using `parser` to parse the content of files and sets the maximum age of cache entries to `maxAge`.

`fs` must be safe for concurrent use.

func (*ConcurrentFsCache[T]) Clear

func (cache *ConcurrentFsCache[T]) Clear()

Clear the cache.

func (*ConcurrentFsCache[T]) ClearDirs

func (cache *ConcurrentFsCache[T]) ClearDirs()

ClearDirs from the cache.

func (*ConcurrentFsCache[T]) ClearFiles

func (cache *ConcurrentFsCache[T]) ClearFiles()

ClearFiles from the cache.

func (*ConcurrentFsCache[T]) GetDir

func (cache *ConcurrentFsCache[T]) GetDir(dir string) ([]fs.DirEntry, error)

GetDir gets the entries of a directory, which may be cached.

func (*ConcurrentFsCache[T]) GetDirEntry

func (cache *ConcurrentFsCache[T]) GetDirEntry(path string) (entry *ConcurrentCachedDir, ok bool)

GetDirEntry gets the `ConcurrentCachedDir` for the path if one exists.

func (*ConcurrentFsCache[T]) GetDirWithMaxAge

func (cache *ConcurrentFsCache[T]) GetDirWithMaxAge(dir string, maxAge time.Duration) ([]fs.DirEntry, error)

GetDirWithMaxAge gets the entries of a directory, with the specified maximum age.

func (*ConcurrentFsCache[T]) GetFile

func (cache *ConcurrentFsCache[T]) GetFile(file string) (T, error)

GetFile returns the parsed content of a file, which may be cached.

func (*ConcurrentFsCache[T]) GetFileEntry

func (cache *ConcurrentFsCache[T]) GetFileEntry(path string) (entry *ConcurrentCachedFile[T], ok bool)

GetFileEntry gets the `CachedFile` for the path if one exists.

func (*ConcurrentFsCache[T]) GetFileWithMaxAge

func (cache *ConcurrentFsCache[T]) GetFileWithMaxAge(file string, maxAge time.Duration) (T, error)

GetFileWithMaxAge returns the parsed content of a file, which may be cached.

func (*ConcurrentFsCache[T]) SetMaxAge

func (cache *ConcurrentFsCache[T]) SetMaxAge(maxAge time.Duration)

type FsCache

type FsCache[T any] struct {

	// MaxAge is the maximum allowed age of a cache entry.
	MaxAge time.Duration
	// contains filtered or unexported fields
}

FsCache is a cache on top of a generic filesystem. It's not safe for concurrent use, use `ConcurrentFsCache` for a thread-safe version.

It can cache directory listings and parsed file content.

func NewFsCache

func NewFsCache[T any](fs fs.FS, parser Parser[T], maxAge time.Duration) FsCache[T]

NewFsCache creates a new cache on top of the `fs` filesystem, using `parser` to parse the content of files and sets the maximum age of cache entries to `maxAge`.

func (*FsCache[T]) Clear

func (cache *FsCache[T]) Clear()

Clear the cache.

func (*FsCache[T]) ClearDirs

func (cache *FsCache[T]) ClearDirs()

ClearDirs from the cache.

func (*FsCache[T]) ClearFiles

func (cache *FsCache[T]) ClearFiles()

ClearFile from the cache.

func (*FsCache[T]) GetDir

func (cache *FsCache[T]) GetDir(dir string) ([]fs.DirEntry, error)

GetDir gets the entries of a directory, which may be cached.

func (*FsCache[T]) GetDirEntry

func (cache *FsCache[T]) GetDirEntry(path string) (entry *CachedDir, ok bool)

GetDirEntry gets the `CachedDir` for the path if one exists.

func (*FsCache[T]) GetDirWithMaxAge

func (cache *FsCache[T]) GetDirWithMaxAge(dir string, maxAge time.Duration) ([]fs.DirEntry, error)

GetDirWithMaxAge gets the entries of a directory, with the specified maximum age.

func (*FsCache[T]) GetFile

func (cache *FsCache[T]) GetFile(file string) (T, error)

GetFile returns the parsed content of a file, which may be cached.

func (*FsCache[T]) GetFileEntry

func (cache *FsCache[T]) GetFileEntry(path string) (entry *CachedFile[T], ok bool)

GetFileEntry gets the `CachedFile` for the path if one exists.

func (*FsCache[T]) GetFileWithMaxAge

func (cache *FsCache[T]) GetFileWithMaxAge(file string, maxAge time.Duration) (T, error)

GetFileWithMaxAge returns the parsed content of a file, with the specified maximum age.

type Parser

type Parser[T any] func(io.Reader) (T, error)

Parser parses content into the type `T`.

Jump to

Keyboard shortcuts

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