cache

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 24, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

README

cache

cache is a middleware that aim to have a transparent interface for a lot of cache implementations.

Use use many cache adapters, including memory and Memcache.

Installation
go get code.forgejo.org/go-chi/cache

Getting Help

// Cache is the interface that operates the cache data.
type Cache interface {
	// Put puts value into cache with key and expire time.
	Put(key string, val any, timeout int64) error
	// Get gets cached value by given key.
	Get(key string) any
	// Delete deletes cached value by given key.
	Delete(key string) error
	// Incr increases cached int-type value by given key as a counter.
	Incr(key string) error
	// Decr decreases cached int-type value by given key as a counter.
	Decr(key string) error
	// IsExist returns true if cached value exists.
	IsExist(key string) bool
	// Flush deletes all cached data.
	Flush() error
	// StartAndGC starts GC routine based on config string settings.
	StartAndGC(opt Options) error
	// Ping tests if the cache is alive
	Ping() error
}

Credits

This package is a modified version of go-chi/cache which is a modified version of go-macaron/cache.

License

This project is under the Apache License, Version 2.0. See the LICENSE file for the full license text.

Documentation

Overview

Package cache is a middleware that aim to have a transparent interface for a lot of cache implementations

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decr

func Decr(val any) (any, error)

func GenericPing

func GenericPing(c Cache) error

GenericPing test a cache by storing and retrieving a value

func Incr

func Incr(val any) (any, error)

func Register

func Register(name string, adapter Cache)

Register registers a adapter.

Types

type Cache

type Cache interface {
	// Put puts value into cache with key and expire time.
	Put(key string, val any, timeout int64) error
	// Get gets cached value by given key.
	Get(key string) any
	// Delete deletes cached value by given key.
	Delete(key string) error
	// Incr increases cached int-type value by given key as a counter.
	Incr(key string) error
	// Decr decreases cached int-type value by given key as a counter.
	Decr(key string) error
	// IsExist returns true if cached value exists.
	IsExist(key string) bool
	// Flush deletes all cached data.
	Flush() error
	// StartAndGC starts GC routine based on config string settings.
	StartAndGC(opt Options) error
	// Ping tests if the cache is alive
	Ping() error
}

Cache is the interface that operates the cache data.

func NewCacher

func NewCacher(opt Options) (Cache, error)

NewCacher creates and returns a new cacher by given adapter name and configuration. It panics when given adapter isn't registered and starts GC automatically.

type MemoryCacher

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

MemoryCacher represents a memory cache adapter implementation.

func (*MemoryCacher) Decr

func (c *MemoryCacher) Decr(key string) (err error)

Decr decreases cached int-type value by given key as a counter.

func (*MemoryCacher) Delete

func (c *MemoryCacher) Delete(key string) error

Delete deletes cached value by given key.

func (*MemoryCacher) Flush

func (c *MemoryCacher) Flush() error

Flush deletes all cached data.

func (*MemoryCacher) Get

func (c *MemoryCacher) Get(key string) any

Get gets cached value by given key.

func (*MemoryCacher) Incr

func (c *MemoryCacher) Incr(key string) (err error)

Incr increases cached int-type value by given key as a counter.

func (*MemoryCacher) IsExist

func (c *MemoryCacher) IsExist(key string) bool

IsExist returns true if cached value exists.

func (*MemoryCacher) Ping

func (c *MemoryCacher) Ping() error

Ping tests if the cache is alive.

func (*MemoryCacher) Put

func (c *MemoryCacher) Put(key string, val any, expire int64) error

Put puts value into cache with key and expire time. If expired is 0, it will be deleted by next GC operation.

func (*MemoryCacher) StartAndGC

func (c *MemoryCacher) StartAndGC(opt Options) error

StartAndGC starts GC routine based on config string settings.

type MemoryItem

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

MemoryItem represents a memory cache item.

type Options

type Options struct {
	// Name of adapter. Default is "memory".
	Adapter string
	// Adapter configuration, it's corresponding to adapter.
	AdapterConfig string
	// GC interval time in seconds. Default is 60.
	Interval int
	// Occupy entire database. Default is false.
	OccupyMode bool
	// Configuration section name. Default is "cache".
	Section string
}

Options represents a struct for specifying configuration options for the cache middleware.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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