fcache

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2023 License: MIT Imports: 12 Imported by: 0

README

Tests GoDoc

fcache

fcache is a simple and fast SQLite based cache that is designed for CLIs and other tools that need to temporarily cache and retrieve data.

Documentation

Overview

The fcache package provides a simple and fast SQLite based cache that is designed for CLIs and other tools that need to temporarily cache and retrieve data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

A Cache is a fast SQLite based on-disk cache.

func Open added in v0.0.3

func Open(filename string, opts ...Option) (*Cache, error)

Open returns a new Cache with filename as the SQLite3 database path. If filename does not exist, it will be created (parent directories are not created).

It is allowed, but not always recommended to use ":memory:" as the filename, which will create an in-memory cache.

func OpenUserCache added in v0.0.3

func OpenUserCache(name string, opts ...Option) (*Cache, error)

OpenUserCache returns a new cache using the default cache directory for the current OS. The $XDG_CACHE_HOME environment variable is respected on all systems (not just Linux).

Name is the name of the directory to create under the user cache directory and the database name is always "cache.sqlite3".

On Linux this might be "$HOME/.cache/{NAME}/cache.sqlite3".

On macOS this might be "$HOME/Library/Caches/{NAME}/cache.sqlite3".

func (*Cache) Close

func (c *Cache) Close() error

Close closes the Cache and the underlying database.

func (*Cache) Contains added in v0.0.2

func (c *Cache) Contains(key string) (bool, error)

Contains returns true if an entry exists for key. It does not check if the entry is expired.

func (*Cache) ContainsContext added in v0.0.2

func (c *Cache) ContainsContext(ctx context.Context, key string) (bool, error)

ContainsContext returns true if an entry exists for key. It does not check if the entry is expired.

func (*Cache) Count added in v0.0.2

func (c *Cache) Count() (int64, error)

CountContext returns the number of entries in the cache.

func (*Cache) CountContext added in v0.0.2

func (c *Cache) CountContext(ctx context.Context) (int64, error)

CountContext returns the number of entries in the cache.

func (*Cache) Database added in v0.0.2

func (c *Cache) Database() string

Database returns the file path of the Cache's database.

func (*Cache) Delete added in v0.0.2

func (c *Cache) Delete(key string) (bool, error)

Delete deletes the entry with key from the cache and returns true if an entry with key was deleted. If no entry with key exists false is returned.

func (*Cache) DeleteContext added in v0.0.2

func (c *Cache) DeleteContext(ctx context.Context, key string) (bool, error)

DeleteContext deletes the entry with key from the cache and returns true if an entry with key was deleted. If no entry with key exists false is returned.

func (*Cache) Entries

func (c *Cache) Entries() ([]Entry, error)

Entries returns a slice of all the entries in the cache sorted by key.

func (*Cache) EntriesContext

func (c *Cache) EntriesContext(ctx context.Context) ([]Entry, error)

EntriesContext returns a slice of all the entries in the cache.

func (*Cache) Entry added in v0.0.2

func (c *Cache) Entry(key string) (*Entry, error)

Entry returns the Entry for key or ErrNoRows if it does not exist.

func (*Cache) EntryContext added in v0.0.2

func (c *Cache) EntryContext(ctx context.Context, key string) (*Entry, error)

EntryContext returns the Entry for key or ErrNoRows if it does not exist.

func (*Cache) ExpiredCount added in v0.0.2

func (c *Cache) ExpiredCount() (int64, error)

ExpiredCount return the number of expired entries in the cache.

func (*Cache) ExpiredCountContext added in v0.0.2

func (c *Cache) ExpiredCountContext(ctx context.Context) (int64, error)

ExpiredCountContext return the number of expired entries in the cache.

func (*Cache) Keys added in v0.0.2

func (c *Cache) Keys() ([]string, error)

Keys returns a sorted slice of all the keys in the cache.

func (*Cache) KeysContext added in v0.0.2

func (c *Cache) KeysContext(ctx context.Context) ([]string, error)

KeysContext returns a sorted slice of all the keys in the cache.

func (*Cache) Load

func (c *Cache) Load(key string, dst any) (bool, error)

Load unmarshals the JSON encoded value stored in the cache for key into dst and returns true if an entry for key existed and is not expired.

An error is returned if there was an error querying the cache database or if there was an error unmarshaling the JSON value into dst.

If the DisallowUnknownFields option is enabled an error is returned when the destination is a struct and the input contains object keys which do not match any non-ignored, exported fields in the destination.

func (*Cache) LoadContext

func (c *Cache) LoadContext(ctx context.Context, key string, dst any) (bool, error)

TODO: consider returning sql.ErrNoRows if an entry is not found. That way users can distinguish between an expired entry and a missing entry

LoadContext unmarshals the JSON encoded value stored in the cache for key into dst and returns true if an entry for key existed and is not expired.

An error is returned if there was an error querying the cache database or if there was an error unmarshaling the JSON value into dst.

If the DisallowUnknownFields option is enabled an error is returned when the destination is a struct and the input contains object keys which do not match any non-ignored, exported fields in the destination.

func (*Cache) Prune

func (c *Cache) Prune() error

Prune prunes any expired entries from the cache and performs a VACUUM if any entries were removed.

func (*Cache) PruneContext

func (c *Cache) PruneContext(ctx context.Context) error

PruneContext prunes any expired entries from the cache and performs a VACUUM if any entries were removed.

func (*Cache) Store

func (c *Cache) Store(key string, v any, ttl time.Duration) error

Store encodes val to JSON and sets it as the value for key, replacing any existing value. If the value cannot be JSON encoded the returned error will unwrap to: json.UnsupportedTypeError.

The TTL controls when an entry expires and has millisecond resolution. A negative TTL never expires.

func (*Cache) StoreContext

func (c *Cache) StoreContext(ctx context.Context, key string, val any, ttl time.Duration) error

StoreContext encodes val to JSON and sets it as the value for key, replacing any existing value. If the value cannot be JSON encoded the returned error will unwrap to: json.UnsupportedTypeError.

The TTL controls when an entry expires and has millisecond resolution. A negative TTL never expires.

type Entry

type Entry struct {
	Key       string    `json:"key" db:"key"`
	CreatedAt time.Time `json:"created_at" db:"created_at_unix_ms"`
	// ExpiresAt is zero if the entry was created without a TTL.
	ExpiresAt time.Time       `json:"expires_at" db:"expires_at_unix_ms"`
	Data      json.RawMessage `json:"data" db:"data"`
}

An Entry is a cache entry.

func (*Entry) Expired

func (e *Entry) Expired() bool

Expired returns if the Entry is expired.

func (*Entry) HasTTL added in v0.0.2

func (e *Entry) HasTTL() bool

HasTTL returns true if the Entry was created with a TTL.

func (*Entry) TTL

func (e *Entry) TTL() time.Duration

TTL returns the TTL the Entry was created with or -1 if the Entry does not have a TTL.

func (*Entry) Unmarshal added in v0.0.2

func (e *Entry) Unmarshal(v any) error

Unmarshal is a helper to unmarshal the data stored in the Entry into v.

type Option added in v0.0.2

type Option interface {
	// contains filtered or unexported methods
}

An Option configures a Cache.

func BusyTimeout added in v0.0.2

func BusyTimeout(d time.Duration) Option

WithBusyTimeout sets the busy timeout of the Cache.

In addition to SQLite3's busy timeout logic the Cache will also retry queries that fail with SQLITE_BUSY until the busy timeout expires. This is to handle the busy timeout setting of SQlite3 not always working as expected.

https://www.sqlite.org/pragma.html#pragma_busy_timeout

func DisallowUnknownFields added in v0.0.2

func DisallowUnknownFields() Option

DisallowUnknownFields causes the Cache's Decoder to return an error when the destination is a struct and the input contains object keys which do not match any non-ignored, exported fields in the destination.

func ReadOnly added in v0.0.3

func ReadOnly() Option

ReadOnly makes the Cache read-only. If the database does not already exist, an error is returned.

Jump to

Keyboard shortcuts

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