cache

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeyIsEmpty is returned when the key is empty.
	ErrKeyIsEmpty = errors.New("key cannot be empty")
	// ErrValueIsNil is returned when the value is nil.
	ErrValueIsNil = errors.New("value cannot be nil")
	// ErrValueIsEmpty is returned when the value is empty.
	ErrValueIsEmpty = errors.New("value cannot be empty")
	// ErrInvalidTTL is returned when the TTL is less than or equal to 0.
	ErrInvalidTTL = errors.New("ttl must be greater than 0")
	// ErrKeyNotFound is returned when the key is not found in the cache.
	ErrKeyNotFound = errors.New("key not found")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	Set(Key, Value) error
	Get(Key) (Value, error)
	Delete(Key) error
	Contains(Key) (bool, error)
}

Cache is an interface that describes the behavior of a cache.

type InMemoryCache

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

InMemoryCache is a struct that represents a key-value In-Memory Cache.

func NewInMemoryCache

func NewInMemoryCache() *InMemoryCache

NewInMemoryCache creates a new InMemoryCache.

func (*InMemoryCache) Contains

func (c *InMemoryCache) Contains(key Key) (bool, error)

Contains checks if a value with the specified key exists in the cache.

func (*InMemoryCache) Delete

func (c *InMemoryCache) Delete(key Key) error

Delete removes the element with the specified key from the cache.

func (*InMemoryCache) Get

func (c *InMemoryCache) Get(key Key) (Value, error)

Get returns the value of the element with the specified key.

func (*InMemoryCache) Set

func (c *InMemoryCache) Set(key Key, value Value) error

Set adds a key-value pair to the InMemoryCache with a specified time-to-live (TTL). The element will be deleted after the TTL has passed.

type Key

type Key string

Key is a string that represents a key in the cache.

type Value

type Value struct {
	Value []byte
	TTL   time.Duration
}

Value is a struct that represents a value in the cache.

Jump to

Keyboard shortcuts

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