Documentation ¶
Index ¶
- Variables
- func SafeKey(key string) string
- type Data
- func (d *Data[T]) Age() time.Duration
- func (d *Data[T]) Bytes() ([]byte, error)
- func (d *Data[T]) FromBytes(bytes []byte) error
- func (d *Data[T]) Get() T
- func (d *Data[T]) IsExpired(ttl time.Duration) bool
- func (d *Data[T]) IsUnset() bool
- func (d *Data[T]) Load(ctx context.Context) error
- func (d *Data[T]) ResetTTL()
- func (d *Data[T]) Set(ctx context.Context, a T) error
- type FireStore
- type FsStore
- type MultiStore
- type RedisStore
- type Serializable
- type Store
Constants ¶
This section is empty.
Variables ¶
var ( // ErrExternalCache indicates there was an error reading or writing to an external cache // this error may mean the external cache has become out of date. However, even if this error is returned // the cache will be safe to use as it will fall back on and in-memory cache. ErrExternalCache = errors.New("could not read/write from the external cache") // ErrNotSerializable indicates there was an error serializing the underlying data into a storable format // this error may mean the external cache has become out of date. However, even if this error is returned // the cache will be safe to use as it will fall back on an in-memory cache. ErrNotSerializable = errors.New("return type could not be serialized/deserialized") // ErrFailedKey indicates there was an error converting an incoming parameter into a valid key // this error may mean the external cache has become out of date. However, even if this error is returned // the cache will be safe to use as it will fall back on an in-memory cache. ErrFailedKey = errors.New("failed to convert input into valid key") )
var Forever = time.Duration(0)
Forever tells cache data to persist forever and never expire
Functions ¶
Types ¶
type Data ¶
type Data[T any] struct { // contains filtered or unexported fields }
Data wraps a value in a persistent data type. Once created, Load can be called to restore the value from a persistent data store. the Get() and Set() methods can be used to read and update the value and will attempt to keep the external data store in sync. Even if the external data store goes out of sync, Data is safe to use, however, future calls to Load may retrieve old data.
func NewData ¶
NewData wraps the initial in a Data type. If the provided store is non-nil, Data will sync it's internal value to the external store
func (*Data[T]) Bytes ¶
Bytes converts the value int a slice of bytes, so it can be stored. If the underlying type implements the Serializable interface that will be used. Otherwise, the type is JSON marshalled
func (*Data[T]) FromBytes ¶
FromBytes takes a slice of bytes and hydrates Data. It can fail if the by format is incorrect. If the underlying type implements the Serializable interface that will be used. Otherwise, the type is JSON marshalled
func (*Data[T]) IsExpired ¶
IsExpired can be used to determine if a Data value is expired in relation to the provided expiration
func (*Data[T]) Load ¶
Load will load the initial data from the external store. If the store is nil or the Data has already been set Load is a no-op. Load can safely be called multiple times.
func (*Data[T]) Set ¶
Set will set the Data's internal value, it will always succeed at setting the in memory value. However, setting the store value may fail. If this happens, Data is still safe to use, and it's value will still reflect the update. however, the data in the external store will not be updated and may be out of date the next time the backed value is created.
type FireStore ¶
type FireStore struct {
// contains filtered or unexported fields
}
FireStore is a Store that uses a firestore collection to store cache data
func NewFireStore ¶
NewFireStore creates a new FireStore, the client is used to interact with the store.
type FsStore ¶
type FsStore struct {
// contains filtered or unexported fields
}
FsStore is a Store that uses the filesystem to store cache data
func NewFsStore ¶
NewFsStore creates a new FsStore, dir is the rood directory where all cached files will be stored
type MultiStore ¶
type MultiStore struct {
// contains filtered or unexported fields
}
type RedisStore ¶
type RedisStore struct {
// contains filtered or unexported fields
}
RedisStore is a Store that uses redis to store cache data
func NewRedisStore ¶
func NewRedisStore(client *redis.Client) *RedisStore
NewRedisStore creates a new RedisStore.
type Serializable ¶
Serializable is an optional interface that can be used to customize the way a Data struct serializes its data if this interface is not provided, jsonMarshall and jsonUnmarshal will be used instead.