Documentation ¶
Index ¶
- Variables
- func GRPCServerOpts(opts ...grpc.ServerOption) func(c *Cache)
- func GRPCTransportCredentials(opts credentials.TransportCredentials) func(c *Cache)
- func HashFuncOpt(hashFunc hashring.HashFunc) func(c *Cache)
- func LFU(threshold int) inmem.Storage[string, []byte]
- func LRU(threshold int) inmem.Storage[string, []byte]
- func TimeoutOpt(timeout time.Duration) func(c *Cache)
- func VirtualNodeOpt(nodes int) func(c *Cache)
- type BatchEvictionErrs
- type Cache
- type CacheOpt
- type Codec
- type GobCodec
- type JsonCodec
- type Member
- type Metrics
- type Procedure
- type StringCodec
- type Table
- func (t *Table[T]) Call(ctx context.Context, key, function string, args []byte) (T, error)
- func (t *Table[T]) Evict(ctx context.Context, key string) error
- func (t *Table[T]) EvictAll(ctx context.Context, keys []string) error
- func (t *Table[T]) Get(ctx context.Context, key string) (T, error)
- func (t *Table[T]) GetHot(key string) (T, error)
- func (t *Table[T]) GetMetrics() (Metrics, error)
- func (t *Table[T]) Put(ctx context.Context, key string, value T, ttl time.Duration) error
- type TableBuilder
- func (tb *TableBuilder[T]) Build(c *Cache) *Table[T]
- func (tb *TableBuilder[T]) WithCodec(codec Codec[T]) *TableBuilder[T]
- func (tb *TableBuilder[T]) WithGetter(fn inmem.Getter[string, T]) *TableBuilder[T]
- func (tb *TableBuilder[T]) WithHotCache(storage inmem.Storage[string, []byte]) *TableBuilder[T]
- func (tb *TableBuilder[T]) WithProcedure(name string, function Procedure[T]) *TableBuilder[T]
- func (tb *TableBuilder[T]) WithStorage(storage inmem.Storage[string, []byte]) *TableBuilder[T]
Constants ¶
This section is empty.
Variables ¶
var ( ErrDuplicatePeer = errors.New("duplicate peer detected") ErrTableNotFound = errors.New("table not found") ErrMissingMembers = errors.New("peers must contain at least one member") ErrCacheDestroyed = errors.New("can't use cache after tear down") )
var ( ErrRPCNotFound = errors.New("RPC not found") ErrKeyNotFound = errors.New("key not found") )
Functions ¶
func GRPCServerOpts ¶ added in v0.4.0
func GRPCServerOpts(opts ...grpc.ServerOption) func(c *Cache)
GRPCServerOpts sets the options when creating the gRPC service.
func GRPCTransportCredentials ¶ added in v0.4.0
func GRPCTransportCredentials(opts credentials.TransportCredentials) func(c *Cache)
GRPCTransportCredentials sets the credentials for the gRPC server
func HashFuncOpt ¶ added in v0.3.0
HashFuncOpt sets the hash function used to determine hashring keys Defaults to FNV-1 algorithm
func TimeoutOpt ¶ added in v0.3.0
TimeoutOpt sets the timeout for grpc clients Defaults to 3 seconds
func VirtualNodeOpt ¶ added in v0.3.0
VirtualNodeOpt sets the number of points/node on the hashring Defaults to 32
Types ¶
type BatchEvictionErrs ¶ added in v0.4.2
type BatchEvictionErrs []batchEvictionErr
func (BatchEvictionErrs) AffectedKeys ¶ added in v0.4.2
func (b BatchEvictionErrs) AffectedKeys() []string
AffectedKeys returns a list of keys owned by clients who returned an error.
As a result, the list may contain keys that were successfully evicted.
func (BatchEvictionErrs) Error ¶ added in v0.4.2
func (b BatchEvictionErrs) Error() string
type Cache ¶ added in v0.2.0
type Cache struct {
// contains filtered or unexported fields
}
Cache hold the nitecache instance. The zero value is not read for use.
Refer to NewCache for creating an instance.
func NewCache ¶ added in v0.2.0
NewCache Creates a new Cache instance
This should only be called once for a same set of peers, so that connections can be reused.
Create a new Table using NewTable if you need to store different values.
Members must have a unique ID and Addr.
Ex.:
func() { self := nitecache.Member{ID: "1", Addr: "localhost:8000"} c, err := nitecache.NewCache(self, nitecache.Member{self}) ... }
func (*Cache) GetMetrics ¶ added in v0.2.0
GetMetrics Returns a copy of the current cache Metrics. For Metrics specific to a Table, refer to Table.GetMetrics.
func (*Cache) HealthCheckPeers ¶ added in v0.4.0
HealthCheckPeers checks the status of the cache's grpc clients
func (*Cache) ListenAndServe ¶ added in v0.4.0
ListenAndServe starts the cache grpc server
type Codec ¶ added in v0.2.1
type Codec[T any] interface { Encode(value T) ([]byte, error) Decode(bytes []byte, value *T) error }
Codec defines the interface to implement custom marshalling/unmarshalling
If a generic type of string or []byte is supplied to the table, nitecache will automatically use StringCodec Otherwise nitecache will default to JsonCodec.
Example:
type ( Coord struct { x, y float64 } CoordCodec struct{} ) func (c CoordCodec) Decode(b []byte, v *Coord) error { buf := bytes.NewBuffer(b) _, err := fmt.Fscanln(buf, &v.x, &v.y) return err } func (c CoordCodec) Encode(v Coord) ([]byte, error) { var buf bytes.Buffer if _, err := fmt.Fprintln(&buf, v.x, v.y); err != nil { return nil, err } return buf.Bytes(), nil }
type GobCodec ¶ added in v0.4.0
type GobCodec[T any] struct{}
GobCodec implements Codec using encoding/gob
type JsonCodec ¶ added in v0.4.0
type JsonCodec[T any] struct{}
JsonCodec implements Codec using encoding/json
This is the default codec for all type except []byte and string.
type Metrics ¶
Metrics are limited to the scope of the owner nodes.
For example, if node-1 queries node-2, metrics will be registered on node-2 only.
type Procedure ¶ added in v0.4.0
Procedure defines the type used for registering RPCs through TableBuilder.WithProcedure.
type StringCodec ¶ added in v0.4.0
StringCodec implements Codec by preventing unnecessary marshalling/unmarshalling for string-like tables.
This codec will automatically be used for string/[]byte tables. However, nitecache won't use it for string/[]byte alias, so you'll have to supply the codec yourself.
type Table ¶
type Table[T any] struct { // contains filtered or unexported fields }
func (*Table[T]) Call ¶ added in v0.4.0
Call calls an RPC previously registered through TableBuilder.WithProcedure on the owner node to update the value for the given key.
Call acquires a lock exclusive to the given key until the RPC has finished executing.
func (*Table[T]) EvictAll ¶ added in v0.4.2
EvictAll attempts to remove all entries from the Table for the given keys.
Keys owned by the same client are batched together for efficiency.
After the operation, a BatchEvictionErrs detailing which keys (if any) failed to be evicted can be retrieved when checking the returned error. Example:
if errs, ok := err.(nitecache.BatchEvictionErrs); ok { // Note that keys that AffectedKeys may return keys that were actually evicted successfully. keysThatFailed := errs.AffectedKeys() }
func (*Table[T]) GetHot ¶ added in v0.1.1
GetHot looks up local cache if the current node is the owner, otherwise looks up hot cache.
GetHot does not call the getter to autofill cache, does not increment metrics and does not affect the main cache's LFU/LRU (if used).
func (*Table[T]) GetMetrics ¶
GetMetrics returns a copy of the current table Metrics. For global cache Metrics, refer to Cache.GetMetrics
type TableBuilder ¶
type TableBuilder[T any] struct { // contains filtered or unexported fields }
func NewTable ¶
func NewTable[T any](name string) *TableBuilder[T]
func (*TableBuilder[T]) Build ¶
func (tb *TableBuilder[T]) Build(c *Cache) *Table[T]
func (*TableBuilder[T]) WithCodec ¶ added in v0.2.1
func (tb *TableBuilder[T]) WithCodec(codec Codec[T]) *TableBuilder[T]
WithCodec overrides the default encoding/decoding behavior.
Defaults to [BytesCodec] for []byte tables and JsonCodec for any other types. See Codec for custom implementation.
func (*TableBuilder[T]) WithGetter ¶
func (tb *TableBuilder[T]) WithGetter(fn inmem.Getter[string, T]) *TableBuilder[T]
WithGetter sets the auto cache filling function.
func (*TableBuilder[T]) WithHotCache ¶ added in v0.1.1
func (tb *TableBuilder[T]) WithHotCache(storage inmem.Storage[string, []byte]) *TableBuilder[T]
WithHotCache enables hot cache.
If hot cache is enabled, a new cache will be populated with values gotten from other peers that can be accessed only through Table.GetHot.
The owner of the hot cache is responsible for keeping it up to date (i.e. calls to Table.Put and Table.Evict won't update hot cache of other peers).
func (*TableBuilder[T]) WithProcedure ¶ added in v0.4.0
func (tb *TableBuilder[T]) WithProcedure(name string, function Procedure[T]) *TableBuilder[T]
WithProcedure Registers an RPC that can be called using Table.Call.
func (*TableBuilder[T]) WithStorage ¶ added in v0.4.0
func (tb *TableBuilder[T]) WithStorage(storage inmem.Storage[string, []byte]) *TableBuilder[T]
WithStorage specifies how to store values.
Must be one of LFU, LRU or nil.
if nil, the table will always grow unless keys are explicitly evicted.