Documentation
¶
Index ¶
- func IsNilValue(data interface{}) bool
- func Key(method string, params ...any) string
- type DataCache
- func (c *DataCache) Invalidate(prefix string) error
- func (c *DataCache) Read(name string, data interface{}) bool
- func (c *DataCache) WithHandlers(handlers ...HandlerInterface) *DataCache
- func (c *DataCache) WithNamespace(namespace string) *DataCache
- func (c *DataCache) Write(name string, data interface{}) error
- type HandlerInterface
- type Hashable
- type InvalidatorInterface
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsNilValue ¶ added in v0.10.1
func IsNilValue(data interface{}) bool
IsNilValue reports whether data is a nil pointer or a nil interface, i.e. a value that must not be cached.
gob.Encoder.Encode *panics* on a top-level nil pointer ("gob: cannot encode nil pointer of type ...") instead of returning an error, so a repository method that legitimately answers "no such thing" with (nil, nil) — an AWS lookup that matched nothing — would take the whole process down as soon as the generated cached wrapper stored its result. Writes of such values are skipped: nothing is stored and the next call refetches.
Handler implementations serializing with gob must apply the same check.
func Key ¶ added in v0.5.0
Key builds a deterministic cache key from a method name and its parameters.
Parameters are rendered by value: pointers are dereferenced, map entries sorted and unexported fields included, so equal arguments always produce the same key and different arguments never share one. Formatting parameters with %v instead would embed pointer addresses for every nested pointer field — which is how AWS SDK input structs are built — yielding a key that changes on every call and can collide once the allocator reuses an address.
The method name is kept as a readable prefix; the parameters are folded into an FNV-64a suffix. The result is safe to use as a file name.
Types ¶
type DataCache ¶
type DataCache struct {
// contains filtered or unexported fields
}
func NewDataCache ¶
func NewDataCache() *DataCache
func (*DataCache) Invalidate ¶ added in v0.14.0
Invalidate evicts every entry in this namespace whose method-name prefix starts with prefix, from every handler that can evict.
Cache keys are "<namespace>:<method>-<hash of params>", and the hash is one way, so there is no evicting "the entries for resource type X" without knowing every parameter set that was ever cached. Callers that write to a resource therefore evict by method-name prefix — wider than strictly needed, within one namespace — because a stale list that still shows a deleted resource is worse than a refetch of a neighbouring type.
func (*DataCache) WithHandlers ¶
func (c *DataCache) WithHandlers(handlers ...HandlerInterface) *DataCache
func (*DataCache) WithNamespace ¶
type HandlerInterface ¶
type Hashable ¶ added in v0.5.0
type Hashable interface {
Hash() string
}
Hashable lets a type supply its own cache-key fragment instead of being walked structurally. Types whose state lives behind a builder API (unexported fields wrapping an SDK input) should implement it.
type InvalidatorInterface ¶ added in v0.14.0
type InvalidatorInterface interface {
// DeletePrefix removes every entry whose key starts with prefix. A prefix
// matching nothing is not an error.
DeletePrefix(prefix string) error
}
InvalidatorInterface is the optional eviction side of a cache handler.
It is a separate interface rather than a method on HandlerInterface so that adding it did not break external handler implementations. DataCache.Invalidate skips handlers that do not implement it, with a warning, because a handler that cannot forget an entry keeps serving it until its TTL lapses.