cache

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2022 License: BSD-3-Clause Imports: 20 Imported by: 0

Documentation

Overview

TODO: readme, etc

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Disc

type Disc[K fmt.Stringer, V any, PV interface {
	encoding.BinaryMarshaler
	encoding.BinaryUnmarshaler
	*V
}] struct {
	// contains filtered or unexported fields
}

Disc is a cache which uses the host filesystem. It is safe for concurrent use WITHIN the same program, but not ACROSS programs. there's still some bugs to work out re: locking the host filesystem, especially across platforms. use at your own risk.

func NewDisc

func NewDisc[K fmt.Stringer, V any, PV interface {
	encoding.BinaryMarshaler
	encoding.BinaryUnmarshaler
	*V
}](
	f func(context.Context, K) (V, error),
	dir string,
	expiration time.Duration,
) (*Disc[K, V, PV], error)

func (*Disc[K, V, PV]) Check

func (d *Disc[K, V, PV]) Check(ctx context.Context, k K) (v V, fresh bool, err error)

func (*Disc[K, V, PV]) Get

func (d *Disc[K, V, PV]) Get(ctx context.Context, k K) (v V, err error)

func (Disc[K, V, PV]) Invalidate

func (d Disc[K, V, PV]) Invalidate(ctx context.Context, k K) error

func (*Disc[K, V, PV]) Refresh

func (d *Disc[K, V, PV]) Refresh(ctx context.Context, k K) (v V, err error)

type Float32

type Float32 float32

Float32 and Float64 are aliases for the primitives that implement fmt.Stringer, encoding.TextMarshaler (on *T), and encoding.Unmarshaler (on *T).

type Float64

type Float64 float64

Float32 and Float64 are aliases for the primitives that implement fmt.Stringer, encoding.TextMarshaler (on *T), and encoding.Unmarshaler (on *T).

type InMem

type InMem[K, V any] struct {
	// contains filtered or unexported fields
}

InMem is an expiring cache for a function where values are stored in memory. This is safe for concurrent use among any number of goroutines.

func NewInMem

func NewInMem[K, V any](
	f func(context.Context, K) (V, error),
	invalidateAfter, cleanupEvery time.Duration,
) *InMem[K, V]

NewInMem creates an expiring cache for a function where values are stored in memory.

func (*InMem[K, V]) Cancel

func (im *InMem[K, V]) Cancel()

Cancel the cache, freeing up it's resources. It is undefined behavior to use the cache after cancellation.

func (*InMem[K, V]) Check

func (im *InMem[K, V]) Check(ctx context.Context, k K) (v V, fresh bool, err error)

Check the cache for the item without refreshing it.

func (*InMem[K, V]) Expiration

func (im *InMem[K, V]) Expiration() time.Duration

func (*InMem[K, V]) Get

func (im *InMem[K, V]) Get(ctx context.Context, k K) (v V, err error)

Get the item by loading a fresh cache value, Refresh()-ing the cache if necessary.

func (*InMem[K, V]) Invalidate

func (im *InMem[K, V]) Invalidate(_ context.Context, k K) error

Invalidate the cached result for k. Never returns an error.

func (*InMem[K, V]) Purge

func (im *InMem[K, V]) Purge()

Purge the cache as completely as possible. Because this uses sync.Map.Range, this does not represent any consistent snapshot of the underlying map.

func (*InMem[K, V]) Refresh

func (im *InMem[K, V]) Refresh(ctx context.Context, k K) (V, error)

Refresh the value for key K by calling the underyling function and returning it.

type Int

type Int int

Int8..=Int64 are aliases for the primitives that implement fmt.Stringer, encoding.TextMarshaler, and encoding.Unmarshaler.

func (*Int) MarshalBinary

func (u *Int) MarshalBinary() ([]byte, error)

MarshalBinary marshals the integer type as 8 bytes, little-endian, regardless of source size.

func (*Int) MarshalText

func (u *Int) MarshalText() ([]byte, error)

MarshalText calls String().

func (Int) String

func (n Int) String() string

String formats w/ strconv.FormatInt(int64(n), 10)

func (*Int) UnmarshalBinary

func (u *Int) UnmarshalBinary(b []byte) error

Unmarshal.Binary unmarshals the integer from the first eight bytes, little-endian. THERE IS NO ERROR CHECKING WHATSOEVER.

func (*Int) UnmarshalText

func (u *Int) UnmarshalText(b []byte) error

UnmarshalText via strconv.ParseUint.

type Int16

type Int16 int16

Int8..=Int64 are aliases for the primitives that implement fmt.Stringer, encoding.TextMarshaler, and encoding.Unmarshaler.

func (*Int16) MarshalBinary

func (u *Int16) MarshalBinary() ([]byte, error)

MarshalBinary marshals the integer type as 8 bytes, little-endian, regardless of source size.

func (*Int16) MarshalText

func (u *Int16) MarshalText() ([]byte, error)

MarshalText calls String().

func (Int16) String

func (n Int16) String() string

String formats w/ strconv.FormatInt(int64(n), 10)

func (*Int16) UnmarshalBinary

func (u *Int16) UnmarshalBinary(b []byte) error

Unmarshal.Binary unmarshals the integer from the first eight bytes, little-endian. THERE IS NO ERROR CHECKING WHATSOEVER.

func (*Int16) UnmarshalText

func (u *Int16) UnmarshalText(b []byte) error

type Int32

type Int32 int32

Int8..=Int64 are aliases for the primitives that implement fmt.Stringer, encoding.TextMarshaler, and encoding.Unmarshaler.

func (*Int32) MarshalBinary

func (u *Int32) MarshalBinary() ([]byte, error)

MarshalBinary marshals the integer type as 8 bytes, little-endian, regardless of source size.

func (*Int32) MarshalText

func (u *Int32) MarshalText() ([]byte, error)

MarshalText calls String().

func (Int32) String

func (n Int32) String() string

String formats w/ strconv.FormatIint(int64(n), 10)

func (*Int32) UnmarshalBinary

func (u *Int32) UnmarshalBinary(b []byte) error

Unmarshal.Binary unmarshals the integer from the first eight bytes, little-endian. THERE IS NO ERROR CHECKING WHATSOEVER.

func (*Int32) UnmarshalText

func (u *Int32) UnmarshalText(b []byte) (err error)

UnmarshalText via strconv.ParseInt.

type Int64

type Int64 int64

Int8..=Int64 are aliases for the primitives that implement fmt.Stringer, encoding.TextMarshaler, and encoding.Unmarshaler.

func (*Int64) MarshalBinary

func (u *Int64) MarshalBinary() ([]byte, error)

MarshalBinary marshals the integer type as 8 bytes, little-endian, regardless of source size.

func (*Int64) MarshalText

func (u *Int64) MarshalText() ([]byte, error)

MarshalText calls String().

func (Int64) String

func (n Int64) String() string

String formats w/ strconv.FormatInt(int64(n), 10)

func (*Int64) UnmarshalBinary

func (u *Int64) UnmarshalBinary(b []byte) error

Unmarshal.Binary unmarshals the integer from the first eight bytes, little-endian. THERE IS NO ERROR CHECKING WHATSOEVER.

func (*Int64) UnmarshalText

func (u *Int64) UnmarshalText(b []byte) error

UnmarshalText via strconv.ParseInt.

type Int8

type Int8 int8

Int8..=Int64 are aliases for the primitives that implement fmt.Stringer, encoding.TextMarshaler, and encoding.Unmarshaler.

func (*Int8) MarshalBinary

func (u *Int8) MarshalBinary() ([]byte, error)

MarshalBinary marshals the integer type as 8 bytes, little-endian, regardless of source size.

func (*Int8) MarshalText

func (u *Int8) MarshalText() ([]byte, error)

MarshalText calls String().

func (Int8) String

func (n Int8) String() string

String formats w/ strconv.FormatInt(int64(n), 10)

func (*Int8) UnmarshalBinary

func (u *Int8) UnmarshalBinary(b []byte) error

Unmarshal.Binary unmarshals the integer from the first eight bytes, little-endian. THERE IS NO ERROR CHECKING WHATSOEVER.

func (*Int8) UnmarshalText

func (u *Int8) UnmarshalText(b []byte) error

UnmarshalText via strconv.ParseUint.

type Interface

type Interface[K, V any] interface {
	// Invalidate the cached result for K.
	Invalidate(context.Context, K) error
	// Check the cache for a result without refreshing it.
	Check(context.Context, K) (v V, fresh bool, err error)
	// Get the cached result for K, refreshing the cache if necessary.
	Get(context.Context, K) (V, error)
	// Refresh gets a fresh result and refreshes the cache.
	Refresh(context.Context, K) (V, error)
}

Interface represents a cache of an underlying function. It's satisfied by InMem, Disk, and Redis.

type Redis

type Redis[K fmt.Stringer, V any, PV interface {
	encoding.TextMarshaler
	encoding.TextUnmarshaler
	*V
}] struct {
	// contains filtered or unexported fields
}

Redis is an expiring cache for a function where values are stored in a redis instance. This is safe for concurrent use among multiple programs & goroutines.

func NewRedis

func NewRedis[K fmt.Stringer, V any, PV interface {
	encoding.TextMarshaler
	encoding.TextUnmarshaler
	*V
}](
	redis *redis.Client,
	expiresAfter time.Duration,
	f func(context.Context, K) (V, error),
) Redis[K, V, PV]

NewRedis builds a cache that

func (*Redis[K, V, PV]) Check

func (r *Redis[K, V, PV]) Check(ctx context.Context, k K) (v V, found bool, err error)

func (*Redis[K, V, PV]) Get

func (r *Redis[K, V, PV]) Get(ctx context.Context, k K) (v V, err error)

func (*Redis[K, V, PV]) Invalidate

func (r *Redis[K, V, PV]) Invalidate(ctx context.Context, k K) error

func (*Redis[K, V, PV]) Refresh

func (r *Redis[K, V, PV]) Refresh(ctx context.Context, k K) (v V, err error)

type String

type String string

String is an alias for the primitive string that implements fmt.Stringer.

func (String) String

func (s String) String() string

String returns the underlyign string.

type Uint

type Uint uint

Unt8..=UInt64 are aliases for the primitives that implement fmt.Stringer, encoding.TextMarshaler, and encoding.Unmarshaler.

func (*Uint) MarshalBinary

func (u *Uint) MarshalBinary() ([]byte, error)

MarshalBinary marshals the integer type as 8 bytes, little-endian, regardless of source size.

func (Uint) String

func (u Uint) String() string

String formats w/ strconv.FormatInt(int64(n), 10)

func (*Uint) UnmarshalBinary

func (u *Uint) UnmarshalBinary(b []byte) error

Unmarshal.Binary unmarshals the integer from the first eight bytes, little-endian. THERE IS NO ERROR CHECKING WHATSOEVER.

type Uint16

type Uint16 uint16

Unt8..=UInt64 are aliases for the primitives that implement fmt.Stringer, encoding.TextMarshaler, and encoding.Unmarshaler.

func (*Uint16) MarshalBinary

func (u *Uint16) MarshalBinary() ([]byte, error)

MarshalBinary marshals the integer type as 8 bytes, little-endian, regardless of source size.

func (Uint16) String

func (u Uint16) String() string

String formats w/ strconv.FormatUint(uint64(n), 10)

func (*Uint16) UnmarshalBinary

func (u *Uint16) UnmarshalBinary(b []byte) error

Unmarshal.Binary unmarshals the integer from the first eight bytes, little-endian. THERE IS NO ERROR CHECKING WHATSOEVER.

func (*Uint16) UnmarshalText

func (u *Uint16) UnmarshalText(b []byte) error

UnmarshalText via strconv.ParseUint.

type Uint32

type Uint32 uint32

Unt8..=UInt64 are aliases for the primitives that implement fmt.Stringer, encoding.TextMarshaler, and encoding.Unmarshaler.

func (*Uint32) MarshalBinary

func (u *Uint32) MarshalBinary() ([]byte, error)

MarshalBinary marshals the integer type as 8 bytes, little-endian, regardless of source size.

func (Uint32) String

func (u Uint32) String() string

String formats w/ strconv.FormatUint(uint64(n), 10)

func (*Uint32) UnmarshalBinary

func (u *Uint32) UnmarshalBinary(b []byte) error

Unmarshal.Binary unmarshals the integer from the first eight bytes, little-endian. THERE IS NO ERROR CHECKING WHATSOEVER.

func (*Uint32) UnmarshalText

func (u *Uint32) UnmarshalText(b []byte) error

UnmarshalText via strconv.ParseUint.

type Uint64

type Uint64 uint64

Unt8..=UInt64 are aliases for the primitives that implement fmt.Stringer, encoding.TextMarshaler, and encoding.Unmarshaler.

func (*Uint64) MarshalBinary

func (u *Uint64) MarshalBinary() ([]byte, error)

MarshalBinary marshals the integer type as 8 bytes, little-endian, regardless of source size.

func (Uint64) String

func (u Uint64) String() string

String formats w/ strconv.FormatUint(uint64(n), 10)

func (*Uint64) UnmarshalBinary

func (u *Uint64) UnmarshalBinary(b []byte) error

Unmarshal.Binary unmarshals the integer from the first eight bytes, little-endian. THERE IS NO ERROR CHECKING WHATSOEVER.

func (*Uint64) UnmarshalText

func (u *Uint64) UnmarshalText(b []byte) error

UnmarshalText via strconv.ParseUint.

type Uint8

type Uint8 uint8

Unt8..=UInt64 are aliases for the primitives that implement fmt.Stringer, encoding.TextMarshaler, and encoding.Unmarshaler.

func (*Uint8) MarshalBinary

func (u *Uint8) MarshalBinary() ([]byte, error)

MarshalBinary marshals the integer type as 8 bytes, little-endian, regardless of source size.

func (Uint8) String

func (u Uint8) String() string

String formats w/ strconv.FormatUint(uint64(n), 10)

func (*Uint8) UnmarshalBinary

func (u *Uint8) UnmarshalBinary(b []byte) error

Unmarshal.Binary unmarshals the integer from the first eight bytes, little-endian. THERE IS NO ERROR CHECKING WHATSOEVER.

func (*Uint8) UnmarshalText

func (u *Uint8) UnmarshalText(b []byte) error

UnmarshalText via strconv.ParseUint.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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