gcache

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2024 License: MIT Imports: 11 Imported by: 0

README

gcache

gcache 是基于go-cache开源项目修改封装,暴露shareded模式,原理是利用分桶的策略,缓解golang的map共享一把锁的代价,将锁力度下降

Installation
Usage
单桶Cache模式,共享一个Map

func main() {
	// Create a cache with a default expiration time of 5 minutes, and which
	// purges expired items every 10 minutes
	c := gcache.New(5*time.Minute, 10*time.Minute)

	// Set the value of the key "foo" to "bar", with the default expiration time
	c.Set("foo", "bar", cache.DefaultExpiration)

	// Set the value of the key "baz" to 42, with no expiration time
	// (the item won't be removed until it is re-set, or removed using
	// c.Delete("baz")
	c.Set("baz", 42, cache.NoExpiration)

	// Get the string associated with the key "foo" from the cache
	foo, found := c.Get("foo")
	if found {
		fmt.Println(foo)
	}

	// Since Go is statically typed, and cache values can be anything, type
	// assertion is needed when values are being passed to functions that don't
	// take arbitrary types, (i.e. interface{}). The simplest way to do this for
	// values which will only be used once--e.g. for passing to another
	// function--is:
	foo, found := c.Get("foo")
	if found {
		MyFunction(foo.(string))
	}

	// This gets tedious if the value is used several times in the same function.
	// You might do either of the following instead:
	if x, found := c.Get("foo"); found {
		foo := x.(string)
		// ...
	}
	// or
	var foo string
	if x, found := c.Get("foo"); found {
		foo = x.(string)
	}
	// ...
	// foo can then be passed around freely as a string

	// Want performance? Store pointers!
	c.Set("foo", &MyStruct, cache.DefaultExpiration)
	if x, found := c.Get("foo"); found {
		foo := x.(*MyStruct)
			// ...
	}
}
分桶Cache模式,每个桶一个Map
var shardedKeys = []string{
	"f",
	"fo",
	"foo",
	"barf",
	"barfo",
	"foobar",
	"bazbarf",
	"bazbarfo",
	"bazbarfoo",
	"foobarbazq",
	"foobarbazqu",
	"foobarbazquu",
	"foobarbazquux",
}

func main() {
	// Create a cache with a default expiration time of 5 minutes, and which
	// purges expired items every 10 minutes
	tc := NewBucketCache(5*time.Minute, 10*time.Minute, 10)
	for _, v := range shardedKeys {
		tc.Set(v, "value", DefaultExpiration)
	}
}

Documentation

Index

Constants

View Source
const (
	// For use with functions that take an expiration time.
	NoExpiration time.Duration = -1
	// For use with functions that take an expiration time. Equivalent to
	// passing in the same expiration duration as was given to New() or
	// NewFrom() when the cache was created (e.g. 5 minutes.)
	DefaultExpiration time.Duration = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BucketCache

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

func NewBucketCache

func NewBucketCache(defaultExpiration, cleanupInterval time.Duration, shardnum int) *BucketCache

func (BucketCache) Add

func (sc BucketCache) Add(k string, x interface{}, d time.Duration) error

func (BucketCache) Decrement

func (sc BucketCache) Decrement(k string, n int64) error

func (BucketCache) DecrementFloat

func (sc BucketCache) DecrementFloat(k string, n float64) error

func (BucketCache) DecrementFloat32

func (sc BucketCache) DecrementFloat32(k string, n float32) (float32, error)

func (BucketCache) DecrementFloat64

func (sc BucketCache) DecrementFloat64(k string, n float64) (float64, error)

func (BucketCache) DecrementInt

func (sc BucketCache) DecrementInt(k string, n int) (int, error)

func (BucketCache) DecrementInt16

func (sc BucketCache) DecrementInt16(k string, n int16) (int16, error)

func (BucketCache) DecrementInt32

func (sc BucketCache) DecrementInt32(k string, n int32) (int32, error)

func (BucketCache) DecrementInt64

func (sc BucketCache) DecrementInt64(k string, n int64) (int64, error)

func (BucketCache) DecrementInt8

func (sc BucketCache) DecrementInt8(k string, n int8) (int8, error)

func (BucketCache) DecrementUint

func (sc BucketCache) DecrementUint(k string, n uint) (uint, error)

func (BucketCache) DecrementUint16

func (sc BucketCache) DecrementUint16(k string, n uint16) (uint16, error)

func (BucketCache) DecrementUint32

func (sc BucketCache) DecrementUint32(k string, n uint32) (uint32, error)

func (BucketCache) DecrementUint64

func (sc BucketCache) DecrementUint64(k string, n uint64) (uint64, error)

func (BucketCache) DecrementUint8

func (sc BucketCache) DecrementUint8(k string, n uint8) (uint8, error)

func (BucketCache) DecrementUintptr

func (sc BucketCache) DecrementUintptr(k string, n uintptr) (uintptr, error)

func (BucketCache) Delete

func (sc BucketCache) Delete(k string)

func (BucketCache) DeleteExpired

func (sc BucketCache) DeleteExpired()

func (BucketCache) Flush

func (sc BucketCache) Flush()

func (BucketCache) Get

func (sc BucketCache) Get(k string) (interface{}, bool)

func (BucketCache) GetWithExpiration

func (sc BucketCache) GetWithExpiration(k string) (interface{}, time.Time, bool)

func (BucketCache) Increment

func (sc BucketCache) Increment(k string, n int64) error

func (BucketCache) IncrementFloat

func (sc BucketCache) IncrementFloat(k string, n float64) error

func (BucketCache) IncrementFloat32

func (sc BucketCache) IncrementFloat32(k string, n float32) (float32, error)

func (BucketCache) IncrementFloat64

func (sc BucketCache) IncrementFloat64(k string, n float64) (float64, error)

func (BucketCache) IncrementInt

func (sc BucketCache) IncrementInt(k string, n int) (int, error)

func (BucketCache) IncrementInt16

func (sc BucketCache) IncrementInt16(k string, n int16) (int16, error)

func (BucketCache) IncrementInt32

func (sc BucketCache) IncrementInt32(k string, n int32) (int32, error)

func (BucketCache) IncrementInt64

func (sc BucketCache) IncrementInt64(k string, n int64) (int64, error)

func (BucketCache) IncrementInt8

func (sc BucketCache) IncrementInt8(k string, n int8) (int8, error)

func (BucketCache) IncrementUint

func (sc BucketCache) IncrementUint(k string, n uint) (uint, error)

func (BucketCache) IncrementUint16

func (sc BucketCache) IncrementUint16(k string, n uint16) (uint16, error)

func (BucketCache) IncrementUint32

func (sc BucketCache) IncrementUint32(k string, n uint32) (uint32, error)

func (BucketCache) IncrementUint64

func (sc BucketCache) IncrementUint64(k string, n uint64) (uint64, error)

func (BucketCache) IncrementUint8

func (sc BucketCache) IncrementUint8(k string, n uint8) (uint8, error)

func (BucketCache) IncrementUintptr

func (sc BucketCache) IncrementUintptr(k string, n uintptr) (uintptr, error)

func (BucketCache) Items

func (sc BucketCache) Items() []map[string]Item

Returns the items in the cache. This may include items that have expired, but have not yet been cleaned up. If this is significant, the Expiration fields of the items should be checked. Note that explicit synchronization is needed to use a cache and its corresponding Items() return values at the same time, as the maps are shared.

func (BucketCache) ItemsCount

func (sc BucketCache) ItemsCount() []int

func (BucketCache) Load

func (sc BucketCache) Load(r io.Reader) error

func (BucketCache) LoadFile

func (sc BucketCache) LoadFile(fname string) error

func (BucketCache) OnEvicted

func (sc BucketCache) OnEvicted(f func(string, interface{}))

func (BucketCache) Replace

func (sc BucketCache) Replace(k string, x interface{}, d time.Duration) error

func (BucketCache) SaveFile

func (sc BucketCache) SaveFile(fname string) error

func (BucketCache) Set

func (sc BucketCache) Set(k string, x interface{}, d time.Duration)

func (BucketCache) SetDefault

func (sc BucketCache) SetDefault(k string, x interface{})

func (BucketCache) SetRecover

func (sc BucketCache) SetRecover(k string, x interface{}, e int64)

type Item

type Item struct {
	Object     interface{}
	Expiration int64
}

func (Item) Expired

func (item Item) Expired() bool

Returns true if the item has expired.

Jump to

Keyboard shortcuts

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