cache

package module
v0.0.0-...-8c8026c Latest Latest
Warning

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

Go to latest
Published: May 17, 2020 License: MIT Imports: 7 Imported by: 0

README

patrickmn-go-cache

Instruments patrickmn/go-cache interactions with Open Census

Documentation

Overview

Package cache instruments patrickmn/go-cache interactions with Open Census

Example
package main

import (
	"context"
	"fmt"
	"time"

	pcache "github.com/patrickmn/go-cache"

	cache "github.com/otternq/patrickmn-go-cache"
)

func main() {
	var (
		c            = pcache.New(time.Minute, time.Minute)
		cacheWrapper = cache.Wrap(c, "default")

		found bool
		val   interface{}
	)

	cache.RegisterAllViews()

	cacheWrapper.Set(context.TODO(), "key", "value", time.Second)

	val, found = cacheWrapper.Get(context.TODO(), "key")

	fmt.Println("Found:", found)
	fmt.Println("Val:", val)

}
Output:

Found: true
Val: value

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// GoCacheName is the name of the cache instance.
	GoCacheName, _ = tag.NewKey("go_cache_name")

	// GoCacheMethod is the cache method called.
	GoCacheMethod, _ = tag.NewKey("go_cache_method")

	// GoCacheStatus identifies found v.s not found.
	GoCacheStatus, _ = tag.NewKey("go_cache_status")

	DefaultTags = []tag.Key{GoCacheMethod, GoCacheStatus}
)

The following tags are aooplied to stats recorded by this package

View Source
var (
	GoCacheLatencyView = &view.View{
		Name:        "go.cache/client/latency",
		Description: "The distribution of latency of various calls in milliseconds",
		Measure:     MeasureLatencyMs,
		Aggregation: DefaultMillisecondsDistribution,
		TagKeys:     DefaultTags,
	}

	GoCacheCallsView = &view.View{
		Name:        "go.cache/client/calls",
		Description: "The number of various calls of methods",
		Measure:     MeasureLatencyMs,
		Aggregation: view.Count(),
		TagKeys:     DefaultTags,
	}

	DefaultViews = []*view.View{GoCacheLatencyView, GoCacheCallsView}
)

Package cache provides some convenience views. You still need to register these views for data to actually be collected. You can use the RegisterAllViews function for this.

View Source
var (
	DefaultMillisecondsDistribution = view.Distribution(
		0.0,
		0.001,
		0.005,
		0.01,
		0.05,
		0.1,
		0.5,
		1.0,
		1.5,
		2.0,
		2.5,
		5.0,
		10.0,
		25.0,
		50.0,
		100.0,
		200.0,
		400.0,
		600.0,
		800.0,
		1000.0,
		1500.0,
		2000.0,
		2500.0,
		5000.0,
		10000.0,
		20000.0,
		40000.0,
		100000.0,
		200000.0,
		500000.0,
	)
)

Default distributions used by views in this package

View Source
var (
	MeasureLatencyMs = stats.Int64("go.cache/latency", "The latency of calls in milliseconds", stats.UnitMilliseconds)
)

The following measures are supported for use in custom views.

Functions

func RegisterAllViews

func RegisterAllViews() error

RegisterAllViews registers all the cache views to enable collection of stats

Types

type Cacher

type Cacher interface {
	Add(c context.Context, k string, x interface{}, d time.Duration) error
	Decrement(c context.Context, k string, n int64) error
	DecrementFloat(c context.Context, k string, n float64) error
	DecrementFloat32(c context.Context, k string, n float32) (float32, error)
	DecrementFloat64(c context.Context, k string, n float64) (float64, error)
	DecrementInt(c context.Context, k string, n int) (int, error)
	DecrementInt16(c context.Context, k string, n int16) (int16, error)
	DecrementInt32(c context.Context, k string, n int32) (int32, error)
	DecrementInt64(c context.Context, k string, n int64) (int64, error)
	DecrementInt8(c context.Context, k string, n int8) (int8, error)
	DecrementUint(c context.Context, k string, n uint) (uint, error)
	DecrementUint16(c context.Context, k string, n uint16) (uint16, error)
	DecrementUint32(c context.Context, k string, n uint32) (uint32, error)
	DecrementUint64(c context.Context, k string, n uint64) (uint64, error)
	DecrementUint8(c context.Context, k string, n uint8) (uint8, error)
	DecrementUintptr(c context.Context, k string, n uintptr) (uintptr, error)
	Delete(c context.Context, k string)
	DeleteExpired(c context.Context)
	Flush(c context.Context)
	Get(c context.Context, k string) (interface{}, bool)
	GetWithExpiration(c context.Context, k string) (interface{}, time.Time, bool)
	Increment(c context.Context, k string, n int64) error
	IncrementFloat(c context.Context, k string, n float64) error
	IncrementFloat32(c context.Context, k string, n float32) (float32, error)
	IncrementFloat64(c context.Context, k string, n float64) (float64, error)
	IncrementInt(c context.Context, k string, n int) (int, error)
	IncrementInt16(c context.Context, k string, n int16) (int16, error)
	IncrementInt32(c context.Context, k string, n int32) (int32, error)
	IncrementInt64(c context.Context, k string, n int64) (int64, error)
	IncrementInt8(c context.Context, k string, n int8) (int8, error)
	IncrementUint(c context.Context, k string, n uint) (uint, error)
	IncrementUint16(c context.Context, k string, n uint16) (uint16, error)
	IncrementUint32(c context.Context, k string, n uint32) (uint32, error)
	IncrementUint64(c context.Context, k string, n uint64) (uint64, error)
	IncrementUint8(c context.Context, k string, n uint8) (uint8, error)
	IncrementUintptr(c context.Context, k string, n uintptr) (uintptr, error)
	ItemCount(c context.Context) int
	Items(c context.Context) map[string]pgocache.Item
	Load(c context.Context, r io.Reader) error
	LoadFile(c context.Context, fname string) error
	OnEvicted(c context.Context, f func(string, interface{}))
	Replace(c context.Context, k string, x interface{}, d time.Duration) error
	Save(c context.Context, w io.Writer) (err error)
	SaveFile(c context.Context, fname string) error
	Set(c context.Context, k string, x interface{}, d time.Duration)
	SetDefault(c context.Context, k string, x interface{})
}

Cacher defines a context aware implementation go-cache

type Wrapper

type Wrapper struct {
	Cache *pgocache.Cache
	// contains filtered or unexported fields
}

func Wrap

func Wrap(c *pgocache.Cache, instanceName string) *Wrapper

Wrap takes a cache instance and wraps it with OpenCensus instrumentation.

func (*Wrapper) Add

func (w *Wrapper) Add(ctx context.Context, k string, x interface{}, d time.Duration) (err error)

func (*Wrapper) Decrement

func (w *Wrapper) Decrement(ctx context.Context, k string, n int64) (err error)

func (*Wrapper) DecrementFloat

func (w *Wrapper) DecrementFloat(ctx context.Context, k string, n float64) (err error)

func (*Wrapper) DecrementFloat32

func (w *Wrapper) DecrementFloat32(ctx context.Context, k string, n float32) (v float32, err error)

func (*Wrapper) DecrementFloat64

func (w *Wrapper) DecrementFloat64(ctx context.Context, k string, n float64) (v float64, err error)

func (*Wrapper) DecrementInt

func (w *Wrapper) DecrementInt(ctx context.Context, k string, n int) (v int, err error)

func (*Wrapper) DecrementInt16

func (w *Wrapper) DecrementInt16(ctx context.Context, k string, n int16) (v int16, err error)

func (*Wrapper) DecrementInt32

func (w *Wrapper) DecrementInt32(ctx context.Context, k string, n int32) (v int32, err error)

func (*Wrapper) DecrementInt64

func (w *Wrapper) DecrementInt64(ctx context.Context, k string, n int64) (v int64, err error)

func (*Wrapper) DecrementInt8

func (w *Wrapper) DecrementInt8(ctx context.Context, k string, n int8) (v int8, err error)

func (*Wrapper) DecrementUint

func (w *Wrapper) DecrementUint(ctx context.Context, k string, n uint) (v uint, err error)

func (*Wrapper) DecrementUint16

func (w *Wrapper) DecrementUint16(ctx context.Context, k string, n uint16) (v uint16, err error)

func (*Wrapper) DecrementUint32

func (w *Wrapper) DecrementUint32(ctx context.Context, k string, n uint32) (v uint32, err error)

func (*Wrapper) DecrementUint64

func (w *Wrapper) DecrementUint64(ctx context.Context, k string, n uint64) (v uint64, err error)

func (*Wrapper) DecrementUint8

func (w *Wrapper) DecrementUint8(ctx context.Context, k string, n uint8) (v uint8, err error)

func (*Wrapper) DecrementUintptr

func (w *Wrapper) DecrementUintptr(ctx context.Context, k string, n uintptr) (v uintptr, err error)

func (*Wrapper) Delete

func (w *Wrapper) Delete(ctx context.Context, k string)

func (*Wrapper) DeleteExpired

func (w *Wrapper) DeleteExpired(ctx context.Context)

func (*Wrapper) Flush

func (w *Wrapper) Flush(ctx context.Context)

func (*Wrapper) Get

func (w *Wrapper) Get(ctx context.Context, k string) (v interface{}, found bool)

func (*Wrapper) GetWithExpiration

func (w *Wrapper) GetWithExpiration(ctx context.Context, k string) (v interface{}, exp time.Time, found bool)

func (*Wrapper) Increment

func (w *Wrapper) Increment(ctx context.Context, k string, n int64) (err error)

func (*Wrapper) IncrementFloat

func (w *Wrapper) IncrementFloat(ctx context.Context, k string, n float64) (err error)

func (*Wrapper) IncrementFloat32

func (w *Wrapper) IncrementFloat32(ctx context.Context, k string, n float32) (v float32, err error)

func (*Wrapper) IncrementFloat64

func (w *Wrapper) IncrementFloat64(ctx context.Context, k string, n float64) (v float64, err error)

func (*Wrapper) IncrementInt

func (w *Wrapper) IncrementInt(ctx context.Context, k string, n int) (v int, err error)

func (*Wrapper) IncrementInt16

func (w *Wrapper) IncrementInt16(ctx context.Context, k string, n int16) (v int16, err error)

func (*Wrapper) IncrementInt32

func (w *Wrapper) IncrementInt32(ctx context.Context, k string, n int32) (v int32, err error)

func (*Wrapper) IncrementInt64

func (w *Wrapper) IncrementInt64(ctx context.Context, k string, n int64) (v int64, err error)

func (*Wrapper) IncrementInt8

func (w *Wrapper) IncrementInt8(ctx context.Context, k string, n int8) (v int8, err error)

func (*Wrapper) IncrementUint

func (w *Wrapper) IncrementUint(ctx context.Context, k string, n uint) (v uint, err error)

func (*Wrapper) IncrementUint16

func (w *Wrapper) IncrementUint16(ctx context.Context, k string, n uint16) (v uint16, err error)

func (*Wrapper) IncrementUint32

func (w *Wrapper) IncrementUint32(ctx context.Context, k string, n uint32) (v uint32, err error)

func (*Wrapper) IncrementUint64

func (w *Wrapper) IncrementUint64(ctx context.Context, k string, n uint64) (v uint64, err error)

func (*Wrapper) IncrementUint8

func (w *Wrapper) IncrementUint8(ctx context.Context, k string, n uint8) (v uint8, err error)

func (*Wrapper) IncrementUintptr

func (w *Wrapper) IncrementUintptr(ctx context.Context, k string, n uintptr) (v uintptr, err error)

func (*Wrapper) ItemCount

func (w *Wrapper) ItemCount(ctx context.Context) (c int)

func (*Wrapper) Items

func (w *Wrapper) Items(ctx context.Context) (items map[string]pgocache.Item)

func (*Wrapper) Load

func (w *Wrapper) Load(ctx context.Context, r io.Reader) (err error)

func (*Wrapper) LoadFile

func (w *Wrapper) LoadFile(ctx context.Context, fname string) (err error)

func (*Wrapper) OnEvicted

func (w *Wrapper) OnEvicted(ctx context.Context, f func(string, interface{}))

func (*Wrapper) Replace

func (w *Wrapper) Replace(ctx context.Context, k string, x interface{}, d time.Duration) (err error)

func (*Wrapper) Save

func (w *Wrapper) Save(ctx context.Context, wr io.Writer) (err error)

func (*Wrapper) SaveFile

func (w *Wrapper) SaveFile(ctx context.Context, fname string) (err error)

func (*Wrapper) Set

func (w *Wrapper) Set(ctx context.Context, k string, x interface{}, d time.Duration)

func (*Wrapper) SetDefault

func (w *Wrapper) SetDefault(ctx context.Context, k string, x interface{})

Jump to

Keyboard shortcuts

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