cache

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2022 License: Apache-2.0 Imports: 15 Imported by: 0

README

CmpCache

cache implement lru limited by memory usage. keys in CmpCache sorted by timestamp and lazy remove.

  • entry store value of cache data as Value type. data only support Int | String | Float | Bool | UnsignedValue

  • pair contains key , value ,overdue timestamp

  • segment cache contains 256 segments. key hash to uint64 and assigned to specific segment

  • store contains segments , locks of each segment ,and a temporary slice for hash

  • benchmark.

    method-duration ns/op
    BenchmarkLRU_Rand 306
    BenchmarkLRU_Freq 278
    BenchmarkLRU_FreqParallel-8 148
  • Interface

    • Remove(key string) error

    • WriteMulti(pairs map[string]Values) error

    • Write(key string, value Values,overdueTimeStamp int64) error

      Add key value pair in cache.

    • IncreaseSize(size int64)

      Increase capacity of memory thar cache could use

    • DecrementSize(size int64) error

      Decrease capacity of memory thar cache could use

    • Get(key string) (Values,bool, error)

      Return value that cache stored . The second return value is key expired or not.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	EvictError              = errors.New("cache memory evicted")
	NilPtrError             = errors.New("nil ptr error")
	ParamIllegalError       = errors.New("size of segments illegal")
	EntryTypeDifferentError = errors.New("entry and cacheValues type different")
	WrongEntryKeyError      = errors.New("entry key is wrong")
	InitEntryFailedError    = errors.New("entry initialized failed")
	ValueNotFoundError      = errors.New("cacheValues not found")
	KeyNotFoundError        = errors.New("key not found")
	KeyTooLongError         = errors.New("key too long")
	SizeTooSmallError       = errors.New("total size too small")
	ValueTypeNotFoundError  = errors.New("cacheValues type not found")
	ValueNotSupportError    = errors.New("value type not support")
	IllegalCacheSize        = errors.New("illegal cache size")
)
View Source
var (
	ExpireFreshQueue *queue.TaskQueue
)

Functions

func GenerateKey

func GenerateKey(keys ...string) string

Types

type BoolValue

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

func (BoolValue) Size

func (b BoolValue) Size() int64

func (BoolValue) String

func (b BoolValue) String() string

func (BoolValue) Type

func (b BoolValue) Type() EntryType

func (BoolValue) Value

func (b BoolValue) Value() interface{}

type ByteSliceValue

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

func (ByteSliceValue) Size

func (b ByteSliceValue) Size() int64

func (ByteSliceValue) String

func (b ByteSliceValue) String() string

func (ByteSliceValue) Type

func (b ByteSliceValue) Type() EntryType

func (ByteSliceValue) Value

func (b ByteSliceValue) Value() interface{}

type ByteValue

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

func (ByteValue) Size

func (b ByteValue) Size() int64

func (ByteValue) String

func (b ByteValue) String() string

func (ByteValue) Type

func (b ByteValue) Type() EntryType

func (ByteValue) Value

func (b ByteValue) Value() interface{}

type Cache

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

Cache implement concurrent safe cache with LRU and ttl strategy.

func GetFreeCache

func GetFreeCache() *Cache

func New

func New(size, segSize int64) (*Cache, error)

New returns cache. parma size means memory cache can use.

func (*Cache) DecrementSize

func (c *Cache) DecrementSize(size int64) error

DecrementSize reduce specific size of max size

func (*Cache) Get

func (c *Cache) Get(key string) (Values, bool, error)

Get returns cache from key whether key is expired. nil will return if key dose not hit.

func (*Cache) IncreaseSize

func (c *Cache) IncreaseSize(size int64)

IncreaseSize add specific size of max size

func (*Cache) Len

func (c *Cache) Len() int

Len return overdueTimestamp of key in cache

func (*Cache) Remove

func (c *Cache) Remove(key string) (Values, error)

Remove remove cache

func (*Cache) Set

func (c *Cache) Set(key string, value Values, duration int64) error

Set write key, cacheValues, overdueTimestamp into cache. whether update or add cache, remove is first.

type CmpCache

type CmpCache interface {
	Remove(key string) (Values, error)
	Set(key string, value Values, overdueTimeStamp int64) error
	IncreaseSize(size int64)
	DecrementSize(size int64) error
	Get(key string) (Values, bool, error)
}

type EntryType

type EntryType int
const (
	UndefinedType EntryType = iota
	IntType
	BoolType
	FloatType
	StringType
	UnsignedType
	ByteType
	ByteSliceType
	InterfaceType
)

type FloatValue

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

func (FloatValue) Size

func (f FloatValue) Size() int64

func (FloatValue) String

func (f FloatValue) String() string

func (FloatValue) Type

func (f FloatValue) Type() EntryType

func (FloatValue) Value

func (f FloatValue) Value() interface{}

type IntValue

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

func (IntValue) Size

func (i IntValue) Size() int64

func (IntValue) String

func (i IntValue) String() string

func (IntValue) Type

func (i IntValue) Type() EntryType

func (IntValue) Value

func (i IntValue) Value() interface{}

type InterfaceValue added in v1.4.0

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

func (InterfaceValue) Size added in v1.4.0

func (i InterfaceValue) Size() int64

func (InterfaceValue) String added in v1.4.0

func (i InterfaceValue) String() string

func (InterfaceValue) Type added in v1.4.0

func (i InterfaceValue) Type() EntryType

func (InterfaceValue) Value added in v1.4.0

func (i InterfaceValue) Value() interface{}

type StringValue

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

func (StringValue) Size

func (s StringValue) Size() int64

func (StringValue) String

func (s StringValue) String() string

func (StringValue) Type

func (s StringValue) Type() EntryType

func (StringValue) Value

func (s StringValue) Value() interface{}

type UnsignedValue

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

func (UnsignedValue) Size

func (u UnsignedValue) Size() int64

func (UnsignedValue) String

func (u UnsignedValue) String() string

func (UnsignedValue) Type

func (u UnsignedValue) Type() EntryType

func (UnsignedValue) Value

func (u UnsignedValue) Value() interface{}

type Value

type Value interface {
	// String returns string
	String() string
	// Type returns type of value
	Type() EntryType
	// Size returns size of value
	Size() int64
	// Value returns any type
	Value() interface{}
}

type Values

type Values []Value

func GetBoolValue

func GetBoolValue(d bool) (Values, error)

func GetByteSliceValue

func GetByteSliceValue(d []byte) (Values, error)

func GetByteValue

func GetByteValue(d byte) (Values, error)

func GetIntValue

func GetIntValue(d int64) (Values, error)

func GetInterfaceValue added in v1.4.0

func GetInterfaceValue(o interface{}) (Values, error)

func GetStringValue

func GetStringValue(d string) (Values, error)

func GetUnsignedValue

func GetUnsignedValue(d uint64) (Values, error)

func MarshalValue

func MarshalValue(o interface{}) (Values, error)

Jump to

Keyboard shortcuts

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