ramcache

package module
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package ramcache implements the driver.Cache interface using an in-memory map.

It's useful for testing, development, and caching small data sets. It's not recommended for production due to lack of data persistence across restarts.

URL Format

The URL should have the following format:

ramcache://[?query]

The optional query part can be used to configure the in-memory cache options through query parameters. The keys of the query parameters should match the case-insensitive field names of the Options structure.

Value Types

Values being set in the cache should be of type [][byte], [string], or implement one of the following interfaces:

Usage

import (
    "context"
    "log"

    "github.com/bartventer/gocache"
    _ "github.com/bartventer/gocache/ramcache"
)

func main() {
    ctx := context.Background()
	urlStr := "ramcache://?cleanupinterval=1m"
    c, err := cache.OpenCache(ctx, urlStr)
    if err != nil {
        log.Fatalf("Failed to initialize cache: %v", err)
    }
    // ... use c with the cache.Cache interface
}

You can create a RAM cache with New:

import (
    "context"

    "github.com/bartventer/gocache/ramcache"
)

func main() {
    ctx := context.Background()
    c := ramcache.New[string](ctx, &ramcache.Options{
		CleanupInterval: 1 * time.Minute,
	})
    // ... use c with the cache.Cache interface
}

Limitations

Please note that due to the limitations of the RAM Cache, pattern matching operations are not supported. This includes the cache.Cache Count and DelKeys methods, which will return a cache.ErrPatternMatchingNotSupported error if called.

Index

Constants

View Source
const Scheme = "ramcache"

Scheme is the cache scheme for the in-memory cache.

Variables

This section is empty.

Functions

func New

func New[K driver.String](ctx context.Context, opts *Options) *ramcache[K]

New returns a new in-memory cache implementation.

Types

type Options

type Options struct {
	// CleanupInterval is the interval at which checks for expired items are performed.
	// If not set, the default is 5 minutes.
	CleanupInterval time.Duration
}

Options are the configuration options for the RAM cache.

Jump to

Keyboard shortcuts

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