cache

package
v0.246.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2024 License: Apache-2.0 Imports: 16 Imported by: 2

README

Package cache

The cache package will supply you with caching solutions for your crud port-based resources.

You can swap cache.Cache with the resource you wish to cache, and just set your original resource as the Cache.Source. It is advised that your domain logic will be unaware of the caching value type.

package main

import (
	"context"
	"domainpkg"
	"adapters/slowsolution"
	"adapters/fastsolution"
)

func main() {
	var repo domainpkg.XYRepository
	repo = slowsolution.NewXYRepository()
	repo = cache.New(repo, fastsolution.NewCacheRepository())
	bl := domainpkg.NewBusinessLogic(repo)
	_ = bl.Do(context.Background())
}

Documentation

Overview

Package cache will supply caching solutions for your crud port compatible resources.

Index

Constants

View Source
const ErrNotImplementedBySource errorkit.Error = "the method is not implemented by the cache source"

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[ENT any, ID comparable] struct {
	// Source is the location of the original data
	Source Source[ENT, ID]
	// Repository is the resource that keeps the cached data.
	Repository Repository[ENT, ID]
	// IDA [optional] is the ENT's ID accessor.
	//
	// default: extid Lookup/Set
	IDA extid.Accessor[ENT, ID]

	CachedQueryInvalidators []CachedQueryInvalidator[ENT, ID]
}

Cache supplies Read/Write-Through caching to CRUD resources.

func New

func New[ENT any, ID comparable](
	source Source[ENT, ID],
	cacheRepo Repository[ENT, ID],
) *Cache[ENT, ID]

func (*Cache[ENT, ID]) CachedQueryMany

func (m *Cache[ENT, ID]) CachedQueryMany(
	ctx context.Context,
	hitID HitID,
	query QueryManyFunc[ENT],
) (iterators.Iterator[ENT], error)

func (*Cache[ENT, ID]) CachedQueryOne

func (m *Cache[ENT, ID]) CachedQueryOne(
	ctx context.Context,
	hitID HitID,
	query QueryOneFunc[ENT],
) (_ent ENT, _found bool, _err error)

func (*Cache[ENT, ID]) Create

func (m *Cache[ENT, ID]) Create(ctx context.Context, ptr *ENT) error

func (*Cache[ENT, ID]) DeleteAll

func (m *Cache[ENT, ID]) DeleteAll(ctx context.Context) error

func (*Cache[ENT, ID]) DeleteByID

func (m *Cache[ENT, ID]) DeleteByID(ctx context.Context, id ID) (rErr error)

func (*Cache[ENT, ID]) DropCachedValues

func (m *Cache[ENT, ID]) DropCachedValues(ctx context.Context) error

func (*Cache[ENT, ID]) FindAll

func (m *Cache[ENT, ID]) FindAll(ctx context.Context) (iterators.Iterator[ENT], error)

func (*Cache[ENT, ID]) FindByID

func (m *Cache[ENT, ID]) FindByID(ctx context.Context, id ID) (ENT, bool, error)

func (*Cache[ENT, ID]) InvalidateByID

func (m *Cache[ENT, ID]) InvalidateByID(ctx context.Context, id ID) (rErr error)

InvalidateByID will as the name suggest, invalidate an entity from the cache.

If you have CachedQueryMany and CachedQueryOne usage, then you must use InvalidateCachedQuery instead of this. This is requires because if absence of an entity is cached in the HitRepository, then it is impossible to determine how to invalidate those queries using an ENT ID.

func (*Cache[ENT, ID]) InvalidateCachedQuery

func (m *Cache[ENT, ID]) InvalidateCachedQuery(ctx context.Context, hitID HitID) (rErr error)

func (*Cache[ENT, ID]) Save added in v0.244.0

func (m *Cache[ENT, ID]) Save(ctx context.Context, ptr *ENT) error

func (*Cache[ENT, ID]) Update

func (m *Cache[ENT, ID]) Update(ctx context.Context, ptr *ENT) error

type CachedQueryInvalidator

type CachedQueryInvalidator[ENT, ID any] struct {
	// CheckEntity checks an entity which is being invalidated, and using its properties,
	// you can construct the entity values
	CheckEntity func(ent ENT) []HitID
	// CheckHit meant to check a Hit to decide if it needs to be invalidated.
	// If CheckHit returns with true, then the hit will be invalidated.
	CheckHit func(hit Hit[ID]) bool
}

type EntityRepository

type EntityRepository[ENT, ID any] interface {
	crud.Creator[ENT]
	crud.Updater[ENT]
	crud.Finder[ENT, ID]
	crud.Deleter[ID]
	crud.ByIDsFinder[ENT, ID]
	crud.Saver[ENT]
}

type Hit

type Hit[ID any] struct {
	ID        HitID `ext:"id"`
	EntityIDs []ID
	Timestamp time.Time
}

type HitID

type HitID string

type HitRepository

type HitRepository[EntID any] interface {
	crud.Saver[Hit[EntID]]
	crud.Finder[Hit[EntID], HitID]
	crud.Deleter[HitID]
}

HitRepository is the query hit result repository.

type Interface

type Interface[ENT, ID any] interface {
	CachedQueryOne(ctx context.Context, hid HitID, query QueryOneFunc[ENT]) (_ent ENT, _found bool, _err error)
	CachedQueryMany(ctx context.Context, hid HitID, query QueryManyFunc[ENT]) (iterators.Iterator[ENT], error)
	InvalidateCachedQuery(ctx context.Context, hid HitID) error
	InvalidateByID(ctx context.Context, id ID) (rErr error)
	DropCachedValues(ctx context.Context) error
}

type Query added in v0.244.0

type Query struct {
	// Name is the name of the repository's query operation.
	// A method name or any unique deterministic name is sufficient.
	Name constant.String
	// ARGS contain parameters to the query that can affect the query result.
	// Supplying the ARGS ensures that a query call with different arguments cached individually.
	// Request lifetime related values are not expected to be part of ARGS.
	// ARGS should contain values that are serializable.
	ARGS QueryARGS
	// Version can help supporting multiple version of the same cached operation,
	// so if the application rolls out with a new version, that has different behaviour or fifferend signature
	// these values can be distringuesed
	Version int
}

Query is a helper that allows you to create a cache.HitID

func (Query) HitID added in v0.244.0

func (q Query) HitID() HitID

func (Query) String added in v0.244.0

func (q Query) String() string

String will encode the QueryKey into a string format

type QueryARGS added in v0.244.0

type QueryARGS map[string]any

QueryARGS is the argument name of the

type QueryManyFunc

type QueryManyFunc[ENT any] func() (iterators.Iterator[ENT], error)

type QueryOneFunc

type QueryOneFunc[ENT any] func() (_ ENT, found bool, _ error)

type Repository

type Repository[ENT, ID any] interface {
	comproto.OnePhaseCommitProtocol
	Entities() EntityRepository[ENT, ID]
	Hits() HitRepository[ID]
}

type Source

type Source[ENT, ID any] interface {
	crud.ByIDFinder[ENT, ID]
}

Source is the minimum expected interface that is expected from a Source resources that needs caching. On top of this, cache.Cache also supports Updater, CreatorPublisher, UpdaterPublisher and DeleterPublisher.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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