cache

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2023 License: Apache-2.0 Imports: 18 Imported by: 4

README

go-cache

This project encapsulates multiple db servers, redis、ledis、memcache、file、memory、nosql、postgresql

example

package main

import (
	"github.com/admpub/cache"
	_ "github.com/admpub/cache/redis"
)

func main() {
	ca, err := cache.Cacher(cache.Options{
		Adapter:       "redis",
		AdapterConfig: "addr=127.0.0.1:6379",
		OccupyMode:    true,
	})

	if err != nil {
		panic(err)
	}

	ca.Put("liyan", "cache", 60)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound     = errors.New("not found")
	ErrExpired      = errors.New("expired")
	ErrNotSupported = errors.New("not supported operation")
)
View Source
var DefaultCodec encoding.Codec = json.JSON

Functions

func Adapters

func Adapters() []string

func CacheItemPoolRelease added in v0.3.5

func CacheItemPoolRelease(item *Item)

func Config

func Config() *ini.File

func Decr

func Decr(val interface{}) (interface{}, error)

func HasAdapter

func HasAdapter(name string) bool

func Incr

func Incr(val interface{}) (interface{}, error)

func Register

func Register(name string, adapter Cache)

Register registers a adapter.

func Version

func Version() string

Types

type Cache

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

Cache is the interface that operates the cache data.

func Cacher

func Cacher(ctx context.Context, options ...Options) (Cache, error)

Cacher is a middleware that maps a cache.Cache service into the Macaron handler chain. An single variadic cache.Options struct can be optionally provided to configure.

func NewCacher

func NewCacher(ctx context.Context, name string, 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.

func NewMemoryCacher

func NewMemoryCacher() Cache

NewMemoryCacher creates and returns a new memory cacher.

type Codec

type Codec interface {
	SetCodec(encoding.Codec)
	Codec() encoding.Codec
}

type FileCacher

type FileCacher struct {
	GetAs
	// contains filtered or unexported fields
}

FileCacher represents a file cache adapter implementation.

func NewFileCacher

func NewFileCacher() *FileCacher

NewFileCacher creates and returns a new file cacher.

func (*FileCacher) Client

func (c *FileCacher) Client() interface{}

func (*FileCacher) Close

func (c *FileCacher) Close() error

func (*FileCacher) Codec

func (c *FileCacher) Codec() encoding.Codec

func (*FileCacher) Decr

func (c *FileCacher) Decr(ctx context.Context, key string) error

Decr cached int value.

func (*FileCacher) Delete

func (c *FileCacher) Delete(ctx context.Context, key string) error

Delete deletes cached value by given key.

func (*FileCacher) Flush

func (c *FileCacher) Flush(ctx context.Context) error

Flush deletes all cached data.

func (*FileCacher) Get

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

Get gets cached value by given key.

func (*FileCacher) Incr

func (c *FileCacher) Incr(ctx context.Context, key string) error

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

func (*FileCacher) IsExist

func (c *FileCacher) IsExist(ctx context.Context, key string) (bool, error)

IsExist returns true if cached value exists.

func (*FileCacher) Put

func (c *FileCacher) Put(ctx context.Context, key string, val interface{}, 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 (*FileCacher) SetCodec

func (c *FileCacher) SetCodec(codec encoding.Codec)

func (*FileCacher) StartAndGC

func (c *FileCacher) StartAndGC(ctx context.Context, opt Options) error

StartAndGC starts GC routine based on config string settings.

type GetAs

type GetAs struct {
	Cache
}

func As

func As(cache Cache) GetAs

func (GetAs) Any

func (g GetAs) Any(ctx context.Context, key string) interface{}

func (GetAs) Bytes

func (g GetAs) Bytes(ctx context.Context, key string) []byte

func (GetAs) Float32

func (g GetAs) Float32(ctx context.Context, key string) float32

func (GetAs) Float64

func (g GetAs) Float64(ctx context.Context, key string) float64

func (GetAs) Int

func (g GetAs) Int(ctx context.Context, key string) int

func (GetAs) Int32

func (g GetAs) Int32(ctx context.Context, key string) int32

func (GetAs) Int64

func (g GetAs) Int64(ctx context.Context, key string) int64

func (GetAs) Map

func (g GetAs) Map(ctx context.Context, key string) map[string]interface{}

func (GetAs) Mapx

func (g GetAs) Mapx(ctx context.Context, key string) param.Store

func (GetAs) Slice

func (g GetAs) Slice(ctx context.Context, key string) []interface{}

func (GetAs) String

func (g GetAs) String(ctx context.Context, key string) string

func (GetAs) Uint

func (g GetAs) Uint(ctx context.Context, key string) uint

func (GetAs) Uint32

func (g GetAs) Uint32(ctx context.Context, key string) uint32

func (GetAs) Uint64

func (g GetAs) Uint64(ctx context.Context, key string) uint64

type Getter

type Getter interface {
	String(ctx context.Context, key string) string
	Int(ctx context.Context, key string) int
	Uint(ctx context.Context, key string) uint
	Int64(ctx context.Context, key string) int64
	Uint64(ctx context.Context, key string) uint64
	Int32(ctx context.Context, key string) int32
	Uint32(ctx context.Context, key string) uint32
	Float32(ctx context.Context, key string) float32
	Float64(ctx context.Context, key string) float64
	Bytes(ctx context.Context, key string) []byte
	Map(ctx context.Context, key string) map[string]interface{}
	Any(ctx context.Context, key string) interface{}
	Mapx(ctx context.Context, key string) param.Store
	Slice(ctx context.Context, key string) []interface{}
}

type Item

type Item struct {
	Val     interface{}
	Created int64
	Expire  int64
}

Item represents a cache item.

func CacheItemPoolGet added in v0.3.5

func CacheItemPoolGet() *Item

func (*Item) Reset added in v0.3.1

func (item *Item) Reset()

type MemoryCacher

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

MemoryCacher represents a memory cache adapter implementation.

func (*MemoryCacher) Client

func (c *MemoryCacher) Client() interface{}

func (*MemoryCacher) Close

func (c *MemoryCacher) Close() error

func (*MemoryCacher) Codec

func (c *MemoryCacher) Codec() encoding.Codec

func (*MemoryCacher) Decr

func (c *MemoryCacher) Decr(ctx context.Context, key string) (err error)

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

func (*MemoryCacher) Delete

func (c *MemoryCacher) Delete(ctx context.Context, key string) error

Delete deletes cached value by given key.

func (*MemoryCacher) Flush

func (c *MemoryCacher) Flush(ctx context.Context) error

Flush deletes all cached data.

func (*MemoryCacher) Get

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

Get gets cached value by given key.

func (*MemoryCacher) Incr

func (c *MemoryCacher) Incr(ctx context.Context, key string) (err error)

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

func (*MemoryCacher) IsExist

func (c *MemoryCacher) IsExist(ctx context.Context, key string) (bool, error)

IsExist returns true if cached value exists.

func (*MemoryCacher) Put

func (c *MemoryCacher) Put(ctx context.Context, key string, val interface{}, 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) SetCodec

func (c *MemoryCacher) SetCodec(codec encoding.Codec)

func (*MemoryCacher) StartAndGC

func (c *MemoryCacher) StartAndGC(ctx context.Context, 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
gob
x

Jump to

Keyboard shortcuts

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