cache

package module
v0.0.0-...-1cfe172 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2020 License: MIT Imports: 9 Imported by: 4

README

cache GoDoc License Build Status Coverage Status

baa module providers a cache management.

Features

  • multi storage support: memory, file, memcache, redis
  • Get/Set/Incr/Decr/Delete/Exist/Flush/Start

Getting Started

package main

import (
	"github.com/go-baa/baa"
	"github.com/go-baa/cache"
)

func main() {
	// new app
	app := baa.New()

	// register cache
	app.SetDI("cache", cache.New(cache.Options{
		Name:     "cache",
        Prefix:   "MyApp",
		Adapter:  "memory",
		Config:   map[string]interface{}{},
	}))

	// router
	app.Get("/", func(c *baa.Context) {
		ca := c.DI("cache").(cache.Cacher)
		ca.Set("test", "baa", 10)
        var v string
		ca.Get("test", &v)
		c.String(200, v)
	})

	// run app
	app.Run(":1323")
}

you should import cache adpater before use it, like this:

import(
    "github.com/go-baa/baa"
    "github.com/go-baa/cache"
    _ "github.com/go-baa/cache/memcache"
    _ "github.com/go-baa/cache/redis"
)

adapter memory has build in, do not need import.

Configuration

Common

Name

string

the cache name

Prefix

string

the cache key prefix, used for isolate different cache instance/app.

Adapter

string

the cache adapter name, choose support adapter: memory, file, memcache, redis.

Config

map[string]interface{}

the cache adapter config, use a dict, values was diffrent with adapter.

Adapter Memory

bytesLimit

int64

set the memory cache memory limit, default is 128m

Usage

app.SetDI("cache", cache.New(cache.Options{
    Name:     "cache",
    Prefix:   "MyApp",
    Adapter:  "memory",
    Config:   map[string]interface{}{
        "bytesLimit": int64(128 * 1024 * 1024), // 128m
    },
}))
Adapter Memcache

host

string

memcached server host.

port

string

memcached server port.

Usage

app.SetDI("cache", cache.New(cache.Options{
    Name:     "cache",
    Prefix:   "MyApp",
    Adapter:  "memcache",
    Config:   map[string]interface{}{
        "host": "127.0.0.1",
        "port": "11211",
    },
}))
Adapter Redis

host

string

redis server host.

port

string

redis server port.

password

string

redis server auth, default none.

poolsize

int

connection pool size, default 10.

Usage

app.SetDI("cache", cache.New(cache.Options{
    Name:     "cache",
    Prefix:   "MyApp",
    Adapter:  "redis",
    Config:   map[string]interface{}{
        "host":     "127.0.0.1",
        "port":     "6379",
        "password": "",
        "poolsize": 10,
    },
}))

Documentation

Overview

Package cache providers a cache management for baa.

Index

Constants

View Source
const (
	// MemoryLimit default memory size limit, 128mb
	// 128  1 << 7
	// 1024 1 << 10
	MemoryLimit int64 = 1 << 27
	// MemoryLimitMin minimum value for memory limit, 1mb
	MemoryLimitMin int64 = 1 << 20
	// MenoryObjectMaxSize maximum bytes for object, 1mb
	MenoryObjectMaxSize int64 = 1 << 20
)

Variables

This section is empty.

Functions

func Register

func Register(name string, f instanceFunc)

Register registers a adapter

func SimpleType

func SimpleType(v interface{}) bool

SimpleType check value type is simple type or not

func SimpleValue

func SimpleValue(v []byte, o interface{}) bool

SimpleValue return value to output with type convert

Types

type Cacher

type Cacher interface {
	// Exist return true if value cached by given key
	Exist(key string) bool
	// Get returns value to out by given key
	Get(key string, out interface{}) error
	// Set cache value by given key, cache ttl second
	Set(key string, v interface{}, ttl int64) error
	// Incr increases cached int-type value by given key as a counter
	// if key not exist, before increase set value with zero
	Incr(key string) (int64, error)
	// Decr decreases cached int-type value by given key as a counter
	// if key not exist, before increase set value with zero
	// NOTE: memcached returns uint type cannot be less than zero
	Decr(key string) (int64, error)
	// Delete delete cached data by given key
	Delete(key string) error
	// Flush flush cacher
	Flush() error
	// Start new a cacher and start service
	Start(Options) error
}

Cacher a cache management for baa

func New

func New(o Options) Cacher

New create a Cacher

func NewCacher

func NewCacher(name string, o Options) (Cacher, 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 NewMemory

func NewMemory() Cacher

NewMemory create a cache instance of memory

type Item

type Item struct {
	Val        interface{} // real object value
	TTL        int64       // cache life time
	Expiration int64       // expired time
}

Item cache storage item

func NewItem

func NewItem(val interface{}, ttl int64) *Item

NewItem create a cache item

func (*Item) Decode

func (t *Item) Decode(out interface{}) error

Decode item value to out interface

func (*Item) Decr

func (t *Item) Decr() error

Decr decreases given value

func (*Item) Encode

func (t *Item) Encode() (ItemBinary, error)

Encode encode item to bytes by gob

func (*Item) Expired

func (t *Item) Expired() bool

Expired check item has expired

func (*Item) Incr

func (t *Item) Incr() error

Incr increases given value

type ItemBinary

type ItemBinary []byte

ItemBinary cache item encoded data

func (ItemBinary) Item

func (t ItemBinary) Item() (*Item, error)

Item decode bytes data to cache item use gob

type Memory

type Memory struct {
	Name   string
	Prefix string
	// contains filtered or unexported fields
}

Memory implement a memory cache adapter for cacher

func (*Memory) Decr

func (c *Memory) Decr(key string) (int64, error)

Decr decreases cached int-type value by given key as a counter if key not exist, return errors

func (*Memory) Delete

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

Delete delete cached data by given key

func (*Memory) Exist

func (c *Memory) Exist(key string) bool

Exist return true if value cached by given key

func (*Memory) Flush

func (c *Memory) Flush() error

Flush flush cacher

func (*Memory) Get

func (c *Memory) Get(key string, out interface{}) error

Get returns value by given key

func (*Memory) Incr

func (c *Memory) Incr(key string) (int64, error)

Incr increases cached int-type value by given key as a counter if key not exist, before increase set value with zero

func (*Memory) Set

func (c *Memory) Set(key string, v interface{}, ttl int64) error

Set cache value by given key, cache ttl second

func (*Memory) Start

func (c *Memory) Start(o Options) error

Start new a cacher and start service

type Options

type Options struct {
	Name    string                 // cache name
	Adapter string                 // adapter
	Prefix  string                 // cache key prefix
	Config  map[string]interface{} // config for adapter
}

Options cache options

Directories

Path Synopsis
Package lru implements an LRU cache.
Package lru implements an LRU cache.

Jump to

Keyboard shortcuts

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