package memorycachex
import "git.sr.ht/~jamesponddotco/pagecache-go/memorycachex"
Package memorycachex implements the cachex.Cache interface as a in-memory cache store. It uses Mockingjay as the cache replacement policy.
Index ¶
- Constants
- type Entry
- func NewEntry(key string, resp *http.Response, expiration time.Time) (*Entry, error)
- func (e *Entry) Access()
- func (e *Entry) IsExpired() bool
- func (e *Entry) Load(key string) (*http.Response, error)
- func (e *Entry) SetSize(size uint64)
- func (e *Entry) SetTTL(ttl time.Duration)
- type MemoryCache
- func NewCache(policy *pagecache.Policy, capacity uint64) *MemoryCache
- func (mc *MemoryCache) Delete(_ context.Context, key string) error
- func (mc *MemoryCache) Get(_ context.Context, key string) (*http.Response, error)
- func (mc *MemoryCache) Policy() *pagecache.Policy
- func (mc *MemoryCache) Purge(_ context.Context) error
- func (mc *MemoryCache) Set(_ context.Context, key string, response *http.Response, expiration time.Duration) error
Constants ¶
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" )
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 ¶
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 ¶
Expired checks if the cache entry has expired.
func (*Entry) Load ¶
Load loads the HTTP response from the cache entry.
func (*Entry) SetSize ¶
SetSize updates the size of the cache entry.
func (*Entry) SetTTL ¶
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 (*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
Source Files ¶
cache.go entry.go memorycachex.go
- Version
- v0.0.0-20230501194707-0f89dd22f418 (latest)
- Published
- May 1, 2023
- Platform
- linux/amd64
- Imports
- 9 packages
- Last checked
- 1 month ago –
Tools for package owners.