cache

package
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2023 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Overview

Package cache provides a cached interface to the xkcd server.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMiss means that value we are looking for wasn't in the cache.
	ErrMiss = errors.New("cache miss")
	// ErrLocalFailure means that there was an error while trying to access the
	// local cache.
	ErrLocalFailure = errors.New("error accessing local xkcd cache")
	// ErrOffline means that there was an error trying to access the xkcd
	// server.
	ErrOffline = errors.New("error accessing xkcd server")
	// ErrNoComicsFound is returned when a function can not find any comics.
	ErrNoComicsFound = errors.New("no comics found")
)

Functions

func CheckForNewestComicInfo

func CheckForNewestComicInfo(freshnessThreshold time.Duration) (*xkcd.Comic, error)

CheckForNewestComicInfo returns the latest xkcd.Comic. May query xkcd API if latest comic in the cache has not been updated since freshnessThreshold. The returned error can be safely ignored. Should not be called directly in the UI event loop.

func Close

func Close() error

Close closes the comic cache.

func ComicImagePath

func ComicImagePath(n int) string

ComicImagePath returns the path to the specified comic image within the cache. The file at the returned path may or may not exist. If it does not exist, call DownloadComicImage to fetch the file from the internet.

func ComicInfo

func ComicInfo(n int) (*xkcd.Comic, error)

ComicInfo always returns a valid *xkcd.Comic that can be used, and err will be set if any errors were encountered, however these errors can be ignored safely.

func DownloadAllComicImages added in v1.7.0

func DownloadAllComicImages(cacheWindow ViewRefreshWitherGetter)

DownloadAllComicImages tries to add all comic images to our local cache. If successful, the images can be found at the path returned by ComicImagePath.

func DownloadAllComicMetadata added in v1.7.0

func DownloadAllComicMetadata(cacheWindow ViewRefreshWitherGetter)

DownloadAllComicMetadata asynchronously fills the comic metadata cache and search index via the internet. Status can be checked with Stat(). Should not be called directly in the UI event loop.

func DownloadComicImage

func DownloadComicImage(n int, cacheWindow ViewRefresherGetter) error

DownloadComicImage tries to add a comic image to our local cache. If successful, the image can be found at the path returned by ComicImagePath.

func Init

func Init(index func(comic *xkcd.Comic) error) error

Init initializes the comic cache. Function index is called each time a comic is inserted into the comic cache.

func NewestComicInfoFromCache

func NewestComicInfoFromCache() (*xkcd.Comic, error)

NewestComicInfoFromCache returns the newest comic info available in the cache. The function will not use the internet. The returned error can be safely ignored. Can be used on UI event loop.

Types

type Stat added in v1.7.0

type Stat struct {
	LatestComicNumber int
	CachedCount       int
}

func StatImages added in v1.7.0

func StatImages() (Stat, error)

func StatMetadata added in v1.7.0

func StatMetadata() (Stat, error)

func (Stat) Complete added in v1.7.0

func (s Stat) Complete() bool

func (Stat) Fraction added in v1.7.0

func (s Stat) Fraction() (float64, error)

func (Stat) String added in v1.7.0

func (s Stat) String() string

type ViewRefreshWither added in v1.7.0

type ViewRefreshWither interface {
	RefreshMetadataWith(Stat)
	RefreshImagesWith(Stat)
}

ViewRefreshWither provides methods for refreshing a view of cache statistics with the provided stat. All methods must silently accept a nil receiver.

type ViewRefreshWitherGetter added in v1.7.0

type ViewRefreshWitherGetter func() ViewRefreshWither

ViewRefreshWitherGetter returns a ViewRefreshWither. Useful for lazily passing the ViewRefreshWither as an argument.

type ViewRefresher added in v1.7.0

type ViewRefresher interface {
	ViewRefreshWither
	// RefreshMetadata queries StatMetadata then calls RefreshMetadataWith.
	RefreshMetadata()
	// RefreshImages queries StatImages then calls RefreshImagesWith.
	RefreshImages()
}

ViewRefresher provides methods for refreshing a view of cache statistics. All methods must silently accept a nil receiver.

type ViewRefresherGetter added in v1.7.0

type ViewRefresherGetter func() ViewRefresher

ViewRefresherGetter returns a ViewRefresher. Useful for lazily passing the ViewRefresher as an argument.

Jump to

Keyboard shortcuts

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