stash

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Key

func Key(r *http.Request, scope []string) string

Key builds a cache key from the request, incorporating standard Vary headers and any additional scope specifiers.

Every field is followed by a "|" delimiter to prevent boundary-shifting collisions (e.g. Path="/api/useri" Query="d=123" must not hash identically to Path="/api/user" Query="id=123").

func KeyWithCustomHeaders

func KeyWithCustomHeaders(r *http.Request, scope []string, additionalHeaders []string) string

KeyWithCustomHeaders builds a cache key including arbitrary request headers, useful for CDN Vary expansion (e.g. CF-IPCountry, X-Tenant).

Every field is followed by a "|" delimiter to prevent boundary-shifting collisions. See Key() for the full rationale.

Types

type Config

type Config struct {
	Driver     string
	DefaultTTL time.Duration
	MaxItems   int

	// MaxCacheableSize is the maximum response body size (bytes) that will be
	// stored. Responses larger than this are passed through without caching.
	// 0 means use the built-in default (def.CacheMaxBodySize).
	MaxCacheableSize int64

	Redis  *alaye.RedisCache
	Policy *alaye.TTLPolicy
}

type Entry

type Entry struct {
	Body        []byte
	Headers     http.Header
	Status      int
	StoredAt    time.Time
	CreatedAt   time.Time
	TTL         time.Duration
	VaryHeaders map[string]string
	ContentType string

	// SurrogateTags holds CDN cache tags (e.g. "product:42", "category:books").
	// Used by PurgeByTag to selectively invalidate groups of cached responses.
	// Populated from the upstream Surrogate-Key or Cache-Tag response header.
	SurrogateTags []string
}

func (*Entry) HasTag added in v0.1.2

func (e *Entry) HasTag(tag string) bool

HasTag reports whether this entry carries the given surrogate tag.

func (*Entry) IsStale added in v0.1.2

func (e *Entry) IsStale() bool

IsStale reports whether the entry has exceeded its TTL. An entry with a zero TTL is never considered stale.

type MemoryStore

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

func NewMemoryStore

func NewMemoryStore(cfg *Config) (*MemoryStore, error)

func (*MemoryStore) Clear

func (s *MemoryStore) Clear() error

func (*MemoryStore) Close

func (s *MemoryStore) Close() error

func (*MemoryStore) Delete

func (s *MemoryStore) Delete(key string)

func (*MemoryStore) Get

func (s *MemoryStore) Get(key string) (*Entry, bool)

func (*MemoryStore) Purge added in v0.1.2

func (s *MemoryStore) Purge(tag string) error

PurgeByTag removes all entries whose SurrogateTags contain the given tag.

func (*MemoryStore) Set

func (s *MemoryStore) Set(key string, entry *Entry, ttl time.Duration)

func (*MemoryStore) SetWithPolicy

func (s *MemoryStore) SetWithPolicy(key string, entry *Entry, policy *alaye.TTLPolicy, defaultTTL time.Duration)

type RedisStore

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

func NewRedisStore

func NewRedisStore(cfg *Config) (*RedisStore, error)

func (*RedisStore) Clear

func (s *RedisStore) Clear() error

func (*RedisStore) Close

func (s *RedisStore) Close() error

func (*RedisStore) Delete

func (s *RedisStore) Delete(key string)

func (*RedisStore) Get

func (s *RedisStore) Get(key string) (*Entry, bool)

func (*RedisStore) Purge added in v0.1.2

func (s *RedisStore) Purge(tag string) error

PurgeByTag removes all entries indexed under the given surrogate tag. Uses the tag index maintained in Set to avoid a full SCAN.

func (*RedisStore) Set

func (s *RedisStore) Set(key string, entry *Entry, ttl time.Duration)

func (*RedisStore) SetWithPolicy

func (s *RedisStore) SetWithPolicy(key string, entry *Entry, policy *alaye.TTLPolicy, defaultTTL time.Duration)

type Store

type Store interface {
	Get(key string) (*Entry, bool)
	Set(key string, entry *Entry, ttl time.Duration)
	SetWithPolicy(key string, entry *Entry, policy *alaye.TTLPolicy, defaultTTL time.Duration)
	Delete(key string)
	Clear() error
	Close() error

	// Purge removes all cached entries that carry the given surrogate tag.
	Purge(tag string) error
}

Store is the cache backend interface. PurgeByTag is required for CDN surrogate-key invalidation.

func NewStore

func NewStore(cfg *Config) (Store, error)

Jump to

Keyboard shortcuts

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