cache

package
v0.0.0-...-90e90ca Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const Forever = -1

Forever defines an infinite expiration time.

Variables

View Source
var ErrNotFound = errors.New("key not found")

ErrNotFound is the error that's returned by the Get() method when no cache key was found.

Functions

This section is empty.

Types

type FileCache

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

FileCache represents a file-based cache store that persists data to a JSON file on disk.

func NewFileCache

func NewFileCache(filePath string) (*FileCache, error)

NewFileCache creates a new FileCache instance, initializing it with the given file path. If the file doesn't exist, it will be created.

TODO, add pretty print mechanism.

func (*FileCache) Close

func (f *FileCache) Close() error

func (*FileCache) Delete

func (f *FileCache) Delete(_ context.Context, key string) error

func (*FileCache) Flush

func (f *FileCache) Flush(_ context.Context)

func (*FileCache) Get

func (f *FileCache) Get(_ context.Context, key string, v any) error

func (*FileCache) Invalidate

func (f *FileCache) Invalidate(_ context.Context, tags []string)

func (*FileCache) Ping

func (f *FileCache) Ping(_ context.Context) error

func (*FileCache) Set

func (f *FileCache) Set(_ context.Context, key string, value any, options Options)

type MemCache

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

MemCache represents an in-memory cache store.

func NewInMemory

func NewInMemory(defaultExpiration time.Duration) *MemCache

NewInMemory creates a new instance of MemCache.

func (*MemCache) Close

func (c *MemCache) Close() error

func (*MemCache) Delete

func (c *MemCache) Delete(_ context.Context, key string) error

func (*MemCache) Flush

func (c *MemCache) Flush(_ context.Context)

func (*MemCache) Get

func (c *MemCache) Get(_ context.Context, key string, value interface{}) error

func (*MemCache) Invalidate

func (c *MemCache) Invalidate(_ context.Context, tags []string)

func (*MemCache) Ping

func (c *MemCache) Ping(_ context.Context) error

func (*MemCache) Set

func (c *MemCache) Set(_ context.Context, key string, value interface{}, opts Options)

type OSCache

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

OSCache represents a file system-based cache where each key is stored as a separate file. It uses a separate index file to store expiration times and tags named index.json which is stored in the base directory.

func NewOSCache

func NewOSCache(basePath string, prettyPrint bool) (*OSCache, error)

NewOSCache creates and initializes a new OSCache instance. It creates the base directory if it doesn't exist and loads the index.

func (*OSCache) Close

func (o *OSCache) Close() error

func (*OSCache) Delete

func (o *OSCache) Delete(_ context.Context, key string) error

func (*OSCache) Flush

func (o *OSCache) Flush(_ context.Context)

func (*OSCache) Get

func (o *OSCache) Get(ctx context.Context, key string, v any) error

func (*OSCache) Invalidate

func (o *OSCache) Invalidate(_ context.Context, tags []string)

func (*OSCache) Ping

func (o *OSCache) Ping(_ context.Context) error

func (*OSCache) Set

func (o *OSCache) Set(_ context.Context, key string, value any, options Options)

type Options

type Options struct {
	// Expiration allows to specify a global expiration
	// time when setting a value.
	Expiration time.Duration
	// Tags allows specifying associated tags to the
	// current value.
	Tags []string
}

Options represents the cache store available options when using Set().

type Redis

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

Redis defines the methods for interacting with the cache layer.

func NewRedis

func NewRedis(opts *redis.Options) *Redis

NewRedis returns a new instance of the Redis cache store.

func (*Redis) Close

func (r *Redis) Close() error

func (*Redis) Delete

func (r *Redis) Delete(ctx context.Context, key string) error

func (*Redis) Flush

func (r *Redis) Flush(ctx context.Context)

func (*Redis) Get

func (r *Redis) Get(ctx context.Context, key string, v any) error

func (*Redis) Invalidate

func (r *Redis) Invalidate(ctx context.Context, tags []string)

func (*Redis) Ping

func (r *Redis) Ping(ctx context.Context) error

func (*Redis) Set

func (r *Redis) Set(ctx context.Context, key string, value any, options Options)

type Store

type Store interface {
	// Ping pings the Redis cache to ensure its alive.
	Ping(context.Context) error
	// Get retrieves a specific item from the cache by key. Values are
	// automatically marshalled for use with Redis.
	Get(context.Context, string, any) error
	// Set stores a singular item in memory by key, value
	// and options (tags and expiration time). Values are automatically
	// marshalled for use with Redis & Memcache.
	Set(context.Context, string, any, Options)
	// Delete removes a singular item from the cache by
	// a specific key.
	Delete(context.Context, string) error
	// Invalidate removes items from the cache via the tags passed.
	Invalidate(context.Context, []string)
	// Flush removes all items from the cache.
	Flush(context.Context)
	// Closer closes the client, releasing any open resources.
	io.Closer
}

Store defines methods for interacting with the caching system.

Example
var store cache.Store
if env.IsDevelopment() {
	store = cache.NewInMemory(time.Hour)
} else {
	store = cache.NewRedis(&redis.Options{
		Addr:     "localhost:6379",
		Password: "passsword",
		DB:       0,
	})
}

// Set a value in the cache with a specific key and options
store.Set(context.Background(), "key", "value", cache.Options{
	Expiration: time.Minute * 30,
	Tags:       []string{"tag1", "tag2"},
})

// Retrieve the value from the cache
var value string
err := store.Get(context.Background(), "key", &value)
if err != nil {
	fmt.Println("Error getting value from MemCache:", err)
	return
}
fmt.Println(value)
Output:

value

Directories

Path Synopsis
Code generated by MockGen.
Code generated by MockGen.
Code generated by MockGen.
Code generated by MockGen.

Jump to

Keyboard shortcuts

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