Documentation
¶
Overview ¶
Package pubcache implements a cache of selected publications from a remote Smart Core publication server. This can be used to ensure that publications (such as configuration data) remain available even when communication with the publication server is unavailable, and to allow multiple subsystems to share a publication without retrieving it multiple times.
The server is the authoritative source of publication versions. The cache stores only the most recent version of each cached publication.
Index ¶
Constants ¶
Variables ¶
var ( ErrPublicationNotFound = errors.New("publication not found in storage") ErrPublicationInvalid = errors.New("invalid publication cannot be stored") )
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
A Cache will keep a local copy of a Publication in sync with a publication server. Updates are one-directional - it is not possible to apply updates locally to the cache. Once Pull is called, the Cache will continue updating its local mirror of the publication in the background, until ctx is cancelled. If Storage is provided, then the Cache will load its initial value from the Storage if present, and store all received publication updates into the Cache. Do not copy a Cache or modify its fields once any methods have been called.
func New ¶
func New(ctx context.Context, source traits.PublicationApiClient, device string, pubID string, opts ...CacheOption) *Cache
New constructs a Cache. Background tasks are not started immediately, they will begin once Pull is called for the first time. The Context ctx can be used to stop the Cache's background tasks. Storage is optional; if nil, storage won't be used.
func (*Cache) Pull ¶
func (c *Cache) Pull(ctx context.Context) <-chan *traits.Publication
Pull will return the current cached value of the publication, followed by all updates. When ctx ends, the returned channel will be closed. If no current value is available (for example, because the server is offline and the publication is not present in Storage), then the initial value may be delayed indefinitely. The returned channel does not apply backpressure, so if the consumer is slow, some intermediate values of the publication may be missed.
type CacheOption ¶
type CacheOption func(cache *Cache)
func WithLogger ¶
func WithLogger(logger *zap.Logger) CacheOption
func WithStorage ¶
func WithStorage(storage Storage) CacheOption
type Storage ¶
type Storage interface {
// LoadPublication retrieves a cached publication from storage.
// If a publication with the given ID is not cached, returns ErrPublicationNotFound
LoadPublication(ctx context.Context, pubID string) (*traits.Publication, error)
// StorePublication will cache a publication in storage, silently replacing any existing publication with the same ID.
// If pub cannot be stored because it is invalid, returns ErrPublicationInvalid
StorePublication(ctx context.Context, pub *traits.Publication) error
// ListPublications will list the publication IDs of all publications currently stored in the cache.
ListPublications(ctx context.Context) (pubIDs []string, err error)
// DeletePublication will remove the publication with the given ID from the cache storage.
// If the publication was not in the storage, no error is returned but return value present will be false.
DeletePublication(ctx context.Context, pubID string) (present bool, err error)
}
A Storage implementation is used by the cache to persist a single version for each publication it caches.
func NewBoltStorage ¶
NewBoltStorage create a Storage using a Bolt database as the backend. The passed database must have been opened read-write. This implementation is not context aware; contexts passed to methods will be ignored.
func NewFileStorage ¶
NewFileStorage returns a Storage that persists publications in a directory on the filesystem. The directory must already exist, and we must have the necessary permissions to create, list and delete files in the directory. This implementation doesn't use the contexts passed to methods.
func NewMemoryStorage ¶
func NewMemoryStorage() Storage
NewMemoryStorage returns a non-persistent Storage implementation, which stores all publications in memory. Publications are copied when inserted or retrieved, so cached publications cannot be accidentally modified. The returned Storage is empty.