middleware

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2025 License: MPL-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package middleware provides various middleware implementations for the hypercache service. This package includes logging middleware that wraps the hypercache service to provide execution time logging and method call tracing for debugging and monitoring purposes.

Package middleware contains service middlewares for hypercache.

Package middleware provides various middleware implementations for the hypercache service. This package includes stats middleware that collects and reports cache operation statistics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLoggingMiddleware

func NewLoggingMiddleware(next hypercache.Service, logger Logger) hypercache.Service

NewLoggingMiddleware returns a new LoggingMiddleware.

func NewOTelMetricsMiddleware added in v0.1.8

func NewOTelMetricsMiddleware(next hypercache.Service, meter metric.Meter) (hypercache.Service, error)

NewOTelMetricsMiddleware constructs a metrics middleware using the provided meter.

func NewOTelTracingMiddleware added in v0.1.8

func NewOTelTracingMiddleware(next hypercache.Service, tracer trace.Tracer, opts ...OTelTracingOption) hypercache.Service

NewOTelTracingMiddleware creates a tracing middleware.

func NewStatsCollectorMiddleware

func NewStatsCollectorMiddleware(next hypercache.Service, statsCollector stats.ICollector) hypercache.Service

NewStatsCollectorMiddleware returns a new StatsCollectorMiddleware.

Types

type Logger

type Logger interface {
	Printf(format string, v ...any)
}

Logger describes a logging interface allowing to implement different external, or custom logger. Tested with logrus, and Uber's Zap (high-performance), but should work with any other logger that matches the interface.

type LoggingMiddleware

type LoggingMiddleware struct {
	// contains filtered or unexported fields
}

LoggingMiddleware is a middleware that logs the time it takes to execute the next middleware. Must implement the hypercache.Service interface.

func (LoggingMiddleware) Allocation

func (mw LoggingMiddleware) Allocation() int64

Allocation returns the size allocation in bytes cache.

func (LoggingMiddleware) Capacity

func (mw LoggingMiddleware) Capacity() int

Capacity takes to execute the next middleware.

func (LoggingMiddleware) Clear

func (mw LoggingMiddleware) Clear(ctx context.Context) error

Clear logs the time it takes to execute the next middleware.

func (LoggingMiddleware) Count

func (mw LoggingMiddleware) Count(ctx context.Context) int

Count takes to execute the next middleware.

func (LoggingMiddleware) Get

func (mw LoggingMiddleware) Get(ctx context.Context, key string) (any, bool)

Get logs the time it takes to execute the next middleware.

func (LoggingMiddleware) GetMultiple

func (mw LoggingMiddleware) GetMultiple(ctx context.Context, keys ...string) (map[string]any, map[string]error)

GetMultiple logs the time it takes to execute the next middleware.

func (LoggingMiddleware) GetOrSet

func (mw LoggingMiddleware) GetOrSet(ctx context.Context, key string, value any, expiration time.Duration) (any, error)

GetOrSet logs the time it takes to execute the next middleware.

func (LoggingMiddleware) GetStats

func (mw LoggingMiddleware) GetStats() stats.Stats

GetStats logs the time it takes to execute the next middleware.

func (LoggingMiddleware) GetWithInfo

func (mw LoggingMiddleware) GetWithInfo(ctx context.Context, key string) (*cache.Item, bool)

GetWithInfo logs the time it takes to execute the next middleware.

func (LoggingMiddleware) List

func (mw LoggingMiddleware) List(ctx context.Context, filters ...backend.IFilter) ([]*cache.Item, error)

List logs the time it takes to execute the next middleware.

func (LoggingMiddleware) Remove

func (mw LoggingMiddleware) Remove(ctx context.Context, keys ...string) error

Remove logs the time it takes to execute the next middleware.

func (LoggingMiddleware) Set

func (mw LoggingMiddleware) Set(ctx context.Context, key string, value any, expiration time.Duration) error

Set logs the time it takes to execute the next middleware.

func (LoggingMiddleware) Stop

func (mw LoggingMiddleware) Stop(ctx context.Context) error

Stop logs the time it takes to execute the next middleware.

func (LoggingMiddleware) TriggerEviction

func (mw LoggingMiddleware) TriggerEviction(ctx context.Context)

TriggerEviction logs the time it takes to execute the next middleware.

type OTelMetricsMiddleware added in v0.1.8

type OTelMetricsMiddleware struct {
	// contains filtered or unexported fields
}

OTelMetricsMiddleware emits OpenTelemetry metrics for service methods.

func (*OTelMetricsMiddleware) Allocation added in v0.1.8

func (mw *OTelMetricsMiddleware) Allocation() int64

Allocation returns allocated size.

func (*OTelMetricsMiddleware) Capacity added in v0.1.8

func (mw *OTelMetricsMiddleware) Capacity() int

Capacity returns cache capacity.

func (*OTelMetricsMiddleware) Clear added in v0.1.8

func (mw *OTelMetricsMiddleware) Clear(ctx context.Context) error

Clear implements Service.Clear with metrics.

func (*OTelMetricsMiddleware) Count added in v0.1.8

func (mw *OTelMetricsMiddleware) Count(ctx context.Context) int

Count returns items count.

func (*OTelMetricsMiddleware) Get added in v0.1.8

func (mw *OTelMetricsMiddleware) Get(ctx context.Context, key string) (any, bool)

Get implements Service.Get with metrics.

func (*OTelMetricsMiddleware) GetMultiple added in v0.1.8

func (mw *OTelMetricsMiddleware) GetMultiple(ctx context.Context, keys ...string) (map[string]any, map[string]error)

GetMultiple implements Service.GetMultiple with metrics.

func (*OTelMetricsMiddleware) GetOrSet added in v0.1.8

func (mw *OTelMetricsMiddleware) GetOrSet(ctx context.Context, key string, value any, expiration time.Duration) (any, error)

GetOrSet implements Service.GetOrSet with metrics.

func (*OTelMetricsMiddleware) GetStats added in v0.1.8

func (mw *OTelMetricsMiddleware) GetStats() stats.Stats

GetStats returns stats.

func (*OTelMetricsMiddleware) GetWithInfo added in v0.1.8

func (mw *OTelMetricsMiddleware) GetWithInfo(ctx context.Context, key string) (*cache.Item, bool)

GetWithInfo implements Service.GetWithInfo with metrics.

func (*OTelMetricsMiddleware) List added in v0.1.8

func (mw *OTelMetricsMiddleware) List(ctx context.Context, filters ...backend.IFilter) ([]*cache.Item, error)

List implements Service.List with metrics.

func (*OTelMetricsMiddleware) Remove added in v0.1.8

func (mw *OTelMetricsMiddleware) Remove(ctx context.Context, keys ...string) error

Remove implements Service.Remove with metrics.

func (*OTelMetricsMiddleware) Set added in v0.1.8

func (mw *OTelMetricsMiddleware) Set(ctx context.Context, key string, value any, expiration time.Duration) error

Set implements Service.Set with metrics.

func (*OTelMetricsMiddleware) Stop added in v0.1.8

Stop stops the underlying service.

func (*OTelMetricsMiddleware) TriggerEviction added in v0.1.8

func (mw *OTelMetricsMiddleware) TriggerEviction(ctx context.Context)

TriggerEviction triggers eviction.

type OTelTracingMiddleware added in v0.1.8

type OTelTracingMiddleware struct {
	// contains filtered or unexported fields
}

OTelTracingMiddleware wraps hypercache.Service methods with OpenTelemetry spans.

func (OTelTracingMiddleware) Allocation added in v0.1.8

func (mw OTelTracingMiddleware) Allocation() int64

Allocation returns allocated size.

func (OTelTracingMiddleware) Capacity added in v0.1.8

func (mw OTelTracingMiddleware) Capacity() int

Capacity returns cache capacity.

func (OTelTracingMiddleware) Clear added in v0.1.8

Clear implements Service.Clear with tracing.

func (OTelTracingMiddleware) Count added in v0.1.8

func (mw OTelTracingMiddleware) Count(ctx context.Context) int

Count returns items count.

func (OTelTracingMiddleware) Get added in v0.1.8

func (mw OTelTracingMiddleware) Get(ctx context.Context, key string) (any, bool)

Get implements Service.Get with tracing.

func (OTelTracingMiddleware) GetMultiple added in v0.1.8

func (mw OTelTracingMiddleware) GetMultiple(ctx context.Context, keys ...string) (map[string]any, map[string]error)

GetMultiple implements Service.GetMultiple with tracing.

func (OTelTracingMiddleware) GetOrSet added in v0.1.8

func (mw OTelTracingMiddleware) GetOrSet(ctx context.Context, key string, value any, expiration time.Duration) (any, error)

GetOrSet implements Service.GetOrSet with tracing.

func (OTelTracingMiddleware) GetStats added in v0.1.8

func (mw OTelTracingMiddleware) GetStats() stats.Stats

GetStats returns stats.

func (OTelTracingMiddleware) GetWithInfo added in v0.1.8

func (mw OTelTracingMiddleware) GetWithInfo(ctx context.Context, key string) (*cache.Item, bool)

GetWithInfo implements Service.GetWithInfo with tracing.

func (OTelTracingMiddleware) List added in v0.1.8

func (mw OTelTracingMiddleware) List(ctx context.Context, filters ...backend.IFilter) ([]*cache.Item, error)

List implements Service.List with tracing.

func (OTelTracingMiddleware) Remove added in v0.1.8

func (mw OTelTracingMiddleware) Remove(ctx context.Context, keys ...string) error

Remove implements Service.Remove with tracing.

func (OTelTracingMiddleware) Set added in v0.1.8

func (mw OTelTracingMiddleware) Set(ctx context.Context, key string, value any, expiration time.Duration) error

Set implements Service.Set with tracing.

func (OTelTracingMiddleware) Stop added in v0.1.8

Stop stops the service with a span.

func (OTelTracingMiddleware) TriggerEviction added in v0.1.8

func (mw OTelTracingMiddleware) TriggerEviction(ctx context.Context)

TriggerEviction triggers eviction with a span.

type OTelTracingOption added in v0.1.8

type OTelTracingOption func(*OTelTracingMiddleware)

OTelTracingOption allows configuring the tracing middleware.

func WithCommonAttributes added in v0.1.8

func WithCommonAttributes(attributes ...attribute.KeyValue) OTelTracingOption

WithCommonAttributes sets attributes applied to all spans.

type StatsCollectorMiddleware

type StatsCollectorMiddleware struct {
	// contains filtered or unexported fields
}

StatsCollectorMiddleware is a middleware that collects stats. It can and should re-use the same stats collector as the hypercache. Must implement the hypercache.Service interface.

func (StatsCollectorMiddleware) Allocation

func (mw StatsCollectorMiddleware) Allocation() int64

Allocation returns the size allocation in bytes cache.

func (StatsCollectorMiddleware) Capacity

func (mw StatsCollectorMiddleware) Capacity() int

Capacity returns the capacity of the cache.

func (StatsCollectorMiddleware) Clear

Clear collects stats for the Clear method.

func (StatsCollectorMiddleware) Count

Count returns the count of the items in the cache.

func (StatsCollectorMiddleware) Get

func (mw StatsCollectorMiddleware) Get(ctx context.Context, key string) (any, bool)

Get collects stats for the Get method.

func (StatsCollectorMiddleware) GetMultiple

func (mw StatsCollectorMiddleware) GetMultiple(ctx context.Context, keys ...string) (map[string]any, map[string]error)

GetMultiple collects stats for the GetMultiple method.

func (StatsCollectorMiddleware) GetOrSet

func (mw StatsCollectorMiddleware) GetOrSet(ctx context.Context, key string, value any, expiration time.Duration) (any, error)

GetOrSet collects stats for the GetOrSet method.

func (StatsCollectorMiddleware) GetStats

func (mw StatsCollectorMiddleware) GetStats() stats.Stats

GetStats returns the stats of the cache.

func (StatsCollectorMiddleware) GetWithInfo

func (mw StatsCollectorMiddleware) GetWithInfo(ctx context.Context, key string) (*cache.Item, bool)

GetWithInfo collects stats for the GetWithInfo method.

func (StatsCollectorMiddleware) List

func (mw StatsCollectorMiddleware) List(ctx context.Context, filters ...backend.IFilter) ([]*cache.Item, error)

List collects stats for the List method.

func (StatsCollectorMiddleware) Remove

func (mw StatsCollectorMiddleware) Remove(ctx context.Context, keys ...string) error

Remove collects stats for the Remove method.

func (StatsCollectorMiddleware) Set

func (mw StatsCollectorMiddleware) Set(ctx context.Context, key string, value any, expiration time.Duration) error

Set collects stats for the Set method.

func (StatsCollectorMiddleware) Stop

Stop collects the stats for Stop methods and stops the cache and all its goroutines (if any).

func (StatsCollectorMiddleware) TriggerEviction

func (mw StatsCollectorMiddleware) TriggerEviction(ctx context.Context)

TriggerEviction triggers the eviction of the cache.

Jump to

Keyboard shortcuts

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