diskcache

package module
v0.0.0-...-bdc38f5 Latest Latest
Warning

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

Go to latest
Published: May 28, 2024 License: MIT Imports: 11 Imported by: 0

README

DiskCache

A simple disk cache for caching arbitrary data on disk.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SortByExpiry

func SortByExpiry(entries []Data)

SortByExpiry is a sort function to sort cache entries by expiry time.

func SortByKey

func SortByKey(entries []Data)

SortByKey is a sort function to sort cache entries by key.

func SortByValue

func SortByValue(entries []Data)

SortByValue is a sort function to sort cache entries by value.

Types

type Cache

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

Cache is a disk cache. It stores entries in a directory on disk.

func New

func New(dir string) (Cache, error)

New creates a new disk cache in the given directory.

func (Cache) Clean

func (c Cache) Clean() error

Clean deletes expired cache entries from disk.

func (Cache) Delete

func (c Cache) Delete() error

Delete removes the cache directory and all its contents.

func (Cache) Dir

func (c Cache) Dir() string

Dir returns the directory path of the cache.

func (Cache) Expiry

func (c Cache) Expiry(key string) time.Time

Expiry returns the expiry time of a cache entry.

func (Cache) Filename

func (c Cache) Filename(key string) string

Filename returns the filename of a cache entry. TODO: Remove Filename from the public API?

func (Cache) Filepath

func (c Cache) Filepath(key string) string

Filepath returns the full path of a cache entry. TODO: Remove Filepath from the public API?

func (Cache) Flush

func (c Cache) Flush() error

Flush deletes all cache entries from disk.

func (Cache) Get

func (c Cache) Get(key string) ([]byte, error)

Get gets a cache entry from disk and returns the value only. It returns an error if the entry is expired.

func (Cache) IsExpired

func (c Cache) IsExpired(key string) bool

IsExpired returns true if a cache entry is expired.

func (Cache) List

func (c Cache) List(options ...func([]Data)) ([]Data, error)

List returns a list of cache entry data. It accepts sorting options.

func (Cache) Read

func (c Cache) Read(key string) (Data, error)

Read reads a cache entry from disk and returns all its data. It does not check if the entry is expired.

func (Cache) Remove

func (c Cache) Remove(key string) error

Remove deletes a cache entry from disk.

func (Cache) Set

func (c Cache) Set(key string, value []byte, duration time.Duration) error

Set saves a cache entry with a key, value, and duration.

type Data

type Data struct {
	Expiry time.Time
	Key    string
	Value  []byte
}

Data is a cache entry. It contains a key, a value, and an expiry time. Because the disk cache hashes the key for a filename, the key is stored in the entry. The hash ensures that the filename is valid and unique.

Directories

Path Synopsis
cmd
dc

Jump to

Keyboard shortcuts

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