cache

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package cache provides a cache manager for any type that implements the cache.Interface.

Example

This example demonstrate the basics of the cache interface. For more details check the documentation.

package main

import (
	"fmt"
	"time"

	cm "github.com/patrickascher/gofw/cache"
)

func main() {
	// import the provider package
	// import _ "github.com/patrickascher/gofw/cache/memory"

	// Initialize cache. Each call is creating a new cache instance.
	// The gc will be spawned in the background.
	c, err := cm.New(cm.MEMORY, nil)
	if err != nil {
		return
	}

	// Set a cache item for 5 hours.
	err = c.Set("foo", "bar", 5*time.Hour)

	// Set a cache item infinity
	err = c.Set("John", "Doe", cm.INFINITY)

	// Get a cache by key.
	item, err := c.Get("foo")
	if err != nil && item != nil {
		item.Value() // value of the item "bar"
	}

	// Get all items as map
	items := c.GetAll()
	fmt.Println(items)

	// Check if an key exists
	exists := c.Exist("foo")
	fmt.Println(exists)

	// Delete by key
	err = c.Delete("foo")

	// Delete all items
	err = c.DeleteAll()
}
Output:

Index

Examples

Constants

View Source
const (
	// MEMORY defined cache provider.
	MEMORY = "memory"
	// INFINITY should be used by the cache providers to identify
	// that the value should not get deleted by the garbage collector.
	INFINITY = 0
)

Variables

View Source
var (
	ErrUnknownProvider       = errors.New("cache: unknown cache-provider %q")
	ErrNoProvider            = errors.New("cache: empty cache-name or cache-provider is nil")
	ErrProviderAlreadyExists = errors.New("cache: cache-provider %#v is already registered")
)

Error messages

Functions

func Register

func Register(provider string, fn provider) error

Register the cache provider. This should be called in the init() of the providers. If the cache provider/name is empty or is already registered, an error will return.

Types

type Interface

type Interface interface {
	// Get returns a Valuer by its key.
	// If it does not exist, an error will return.
	Get(key string) (Valuer, error)
	// GetAll returns all existing Valuer as map.
	GetAll() map[string]Valuer
	// GetAll returns all existing Valuer as map.
	GetPrefixed(string) map[string]Valuer
	// Set a value by its key and lifetime.
	// If a value should not get deleted, cache.INFINITY can be used as time.Duration.
	Set(key string, value interface{}, ttl time.Duration) error
	// Exist checks if a value is set by the given key.
	Exist(key string) bool
	// Delete a value by its key.
	Delete(key string) error
	// DeleteAll values of the cache provider.
	DeleteAll() error
	DeletePrefixed(string) error
	// GC will spawn the garbage collector in a goroutine.
	// If your cache provider has its own gc (redis, memcached, ...) just return void in this method.
	GC()
}

Interface is used by cache providers.

func New

func New(provider string, options interface{}) (Interface, error)

New returns a specific cache provider by its name and given options. The available options are defined in the provider. If the provider is not registered an error will return.

type Valuer

type Valuer interface {
	Value() interface{}
}

Valuer is an interface to get the value of a cache object.

Directories

Path Synopsis
Package memory implements the cache.Interface and registers an in-memory provider.
Package memory implements the cache.Interface and registers an in-memory provider.

Jump to

Keyboard shortcuts

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