memorycachex

package
v0.0.0-...-0f89dd2 Latest Latest
Warning

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

Go to latest
Published: May 1, 2023 License: MIT Imports: 9 Imported by: 1

README

memorycachex

Package memorycachex is a thread-safe in-memory Mockingjay cache for HTTP responses that complies with the pagecache.Cache interface.

Installation

To install memorycachex, run:

go get git.sr.ht/~jamesponddotco/pagecache-go/memorycachex

Refer to the API documentation for more information.

Documentation

Overview

Package memorycachex implements the cachex.Cache interface as a in-memory cache store. It uses Mockingjay as the cache replacement policy.

Index

Constants

View Source
const (
	ErrKeyEmpty          xerrors.Error = "key must not be empty"
	ErrKeyMismatch       xerrors.Error = "key mismatch"
	ErrValueEmpty        xerrors.Error = "value must not be empty"
	ErrExpirationZero    xerrors.Error = "expiration must not be zero"
	ErrMarshalResponse   xerrors.Error = "failed to marshal response"
	ErrUnmarshalResponse xerrors.Error = "failed to unmarshal response"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry struct {
	Key        string
	Expiration time.Time
	Request    []byte
	Response   []byte
	Size       uint64
	Frequency  uint64
}

Entry represents a single cache entry. Entry is not tread-safe and should be protected by a sync.Mutex.

func NewEntry

func NewEntry(key string, resp *http.Response, expiration time.Time) (*Entry, error)

NewEntry creates a new cache entry with the specified key and expiration. You should call SerializeResponse after creating the entry to store the response.

func (*Entry) Access

func (e *Entry) Access()

Access increments the frequency counter when the entry is accessed.

func (*Entry) IsExpired

func (e *Entry) IsExpired() bool

Expired checks if the cache entry has expired.

func (*Entry) Load

func (e *Entry) Load(key string) (*http.Response, error)

Load loads the HTTP response from the cache entry.

func (*Entry) SetSize

func (e *Entry) SetSize(size uint64)

SetSize updates the size of the cache entry.

func (*Entry) SetTTL

func (e *Entry) SetTTL(ttl time.Duration)

SetTTL updates the expiration time of the cache entry.

type MemoryCache

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

MemoryCache is an in-memory cache implementing the Cache interface.

func NewCache

func NewCache(policy *pagecache.Policy, capacity uint64) *MemoryCache

NewCache creates a new MemoryCache instance with the specified policy and capacity.

func (*MemoryCache) Delete

func (mc *MemoryCache) Delete(_ context.Context, key string) error

func (*MemoryCache) Get

func (mc *MemoryCache) Get(_ context.Context, key string) (*http.Response, error)

func (*MemoryCache) Policy

func (mc *MemoryCache) Policy() *pagecache.Policy

func (*MemoryCache) Purge

func (mc *MemoryCache) Purge(_ context.Context) error

func (*MemoryCache) Set

func (mc *MemoryCache) Set(_ context.Context, key string, response *http.Response, expiration time.Duration) error

Jump to

Keyboard shortcuts

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