cache

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2017 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package cache is a generated protocol buffer package.

It is generated from these files:

index.proto

It has these top-level messages:

IndexMeta
IndexItem

Index

Constants

View Source
const (
	MaxShards    = 128
	MaxValueSize = int64(128 << 20) // 128MB
	MinShardSize = MaxValueSize + 4096
)

Variables

View Source
var (
	ErrNotFound  = errors.New("key not found")
	ErrValueSize = errors.New("value size exceeded")
)
View Source
var (
	ErrOutOfRange = errors.New("out of data range")
	ErrHeader     = errors.New("header err")
	ErrShortRead  = errors.New("short read")
)
View Source
var (
	ErrInvalidLengthIndex = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowIndex   = fmt.Errorf("proto: integer overflow")
)
View Source
var DefualtCacheOptions = CacheOptions{
	ShardNum:  16,
	Size:      32 * MaxValueSize,
	TTL:       0,
	Allocator: NewAllocatorPool(),
}
View Source
var DefualtShardOptions = ShardOptions{
	Size:      MinShardSize,
	TTL:       0,
	Allocator: NewAllocatorPool(),
}

Functions

This section is empty.

Types

type Allocator

type Allocator interface {
	Malloc(n int) []byte
	Free([]byte)
}

func NewAllocatorPool

func NewAllocatorPool() Allocator

type AllocatorPool

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

func (*AllocatorPool) Free

func (p *AllocatorPool) Free(b []byte)

func (*AllocatorPool) GetMetrics

func (p *AllocatorPool) GetMetrics() AllocatorPoolMetrics

func (*AllocatorPool) Malloc

func (p *AllocatorPool) Malloc(n int) []byte

type AllocatorPoolMetrics

type AllocatorPoolMetrics struct {
	Malloc int64
	Free   int64
	New    int64

	ErrMalloc int64
	ErrFree   int64
}

type Cache

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

func NewCache

func NewCache(path string, options *CacheOptions) (*Cache, error)

func (*Cache) Close

func (c *Cache) Close() error

func (*Cache) Del

func (c *Cache) Del(key string) error

func (*Cache) Get

func (c *Cache) Get(key string) (Item, error)

func (*Cache) GetMetrics

func (c *Cache) GetMetrics() CacheMetrics

func (*Cache) GetMetricsByShards

func (c *Cache) GetMetricsByShards() []CacheMetrics

func (*Cache) GetOptions

func (c *Cache) GetOptions() CacheOptions

func (*Cache) GetStats

func (c *Cache) GetStats() CacheStats

func (*Cache) GetStatsByShards

func (c *Cache) GetStatsByShards() []CacheStats

func (*Cache) Set

func (c *Cache) Set(item Item) error

type CacheData

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

func LoadCacheData

func LoadCacheData(fn string, sz int64) (*CacheData, error)

func (*CacheData) Close

func (d *CacheData) Close() error

func (*CacheData) Read

func (d *CacheData) Read(offset int64, b []byte) error

func (*CacheData) Size

func (d *CacheData) Size() int64

func (*CacheData) Write

func (d *CacheData) Write(offset int64, b []byte) error

type CacheIndex

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

func LoadCacheIndex

func LoadCacheIndex(fn string, datasize int64) (*CacheIndex, error)

func (*CacheIndex) Close

func (i *CacheIndex) Close() error

func (*CacheIndex) Del

func (i *CacheIndex) Del(key string) error

func (*CacheIndex) Dels

func (i *CacheIndex) Dels(keys []string) error

func (*CacheIndex) Get

func (i *CacheIndex) Get(key string) (*IndexItem, error)

func (*CacheIndex) GetIndexMeta

func (i *CacheIndex) GetIndexMeta() IndexMeta

func (*CacheIndex) GetKeys

func (i *CacheIndex) GetKeys() (n uint64, err error)

func (*CacheIndex) Iter

func (i *CacheIndex) Iter(lastkey string, maxIter int, f func(key string, item IndexItem) error) error

func (*CacheIndex) Reserve

func (i *CacheIndex) Reserve(size int32) (*IndexItem, error)

func (*CacheIndex) Set

func (i *CacheIndex) Set(key string, item *IndexItem) error

type CacheMetrics

type CacheMetrics struct {
	GetTotal   int64 // number of get request
	GetHits    int64 // number of items that hit from data
	GetMisses  int64 // number of items that not found
	GetExpired int64 // number of items that expired when get
	SetTotal   int64 // number of set request
	DelTotal   int64 // number of del request
	Expired    int64 // number of items that expired
	Evicted    int64 // number of items evicted
	EvictedAge int64 // min age of the last evicted item
}

func (*CacheMetrics) Add

func (m *CacheMetrics) Add(o CacheMetrics)

type CacheOptions

type CacheOptions struct {
	ShardNum  int
	Size      int64
	TTL       int64
	Allocator Allocator
}

type CacheStats

type CacheStats struct {
	Keys       uint64 // number of keys
	Bytes      uint64 // bytes of keys that used
	LastUpdate int64  // stat time, the stat is async updated
}

func (*CacheStats) Add

func (st *CacheStats) Add(o CacheStats)

type ConsistentHash

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

a simplify consistent hash impelment without weight

func NewConsistentHashTable

func NewConsistentHashTable(n int) ConsistentHash

NewConsistentHashTable creates a ConsistentHash with value[0, n)

func (*ConsistentHash) Get

func (c *ConsistentHash) Get(key string) int

type IndexItem

type IndexItem struct {
	Term      int64  `protobuf:"varint,1,req,name=Term" json:"Term"`
	Offset    int64  `protobuf:"varint,2,req,name=Offset" json:"Offset"`
	ValueSize int32  `protobuf:"varint,3,req,name=ValueSize" json:"ValueSize"`
	Timestamp int64  `protobuf:"varint,4,req,name=Timestamp" json:"Timestamp"`
	TTL       uint32 `protobuf:"varint,5,req,name=TTL" json:"TTL"`
	Flags     uint32 `protobuf:"varint,6,req,name=Flags" json:"Flags"`
}

func (*IndexItem) Descriptor

func (*IndexItem) Descriptor() ([]byte, []int)

func (*IndexItem) Marshal

func (m *IndexItem) Marshal() (data []byte, err error)

func (*IndexItem) MarshalTo

func (m *IndexItem) MarshalTo(data []byte) (int, error)

func (*IndexItem) ProtoMessage

func (*IndexItem) ProtoMessage()

func (*IndexItem) Reset

func (m *IndexItem) Reset()

func (*IndexItem) Size

func (m *IndexItem) Size() (n int)

func (*IndexItem) String

func (m *IndexItem) String() string

func (IndexItem) TotalSize

func (i IndexItem) TotalSize() int64

TotalSize returns bytes used including index & data of the item

func (*IndexItem) Unmarshal

func (m *IndexItem) Unmarshal(data []byte) error

type IndexMeta

type IndexMeta struct {
	Term     int64 `protobuf:"varint,1,req,name=Term" json:"Term"`
	Head     int64 `protobuf:"varint,2,req,name=Head" json:"Head"`
	DataSize int64 `protobuf:"varint,3,req,name=DataSize" json:"DataSize"`
}

func (*IndexMeta) Descriptor

func (*IndexMeta) Descriptor() ([]byte, []int)

func (IndexMeta) IsValidate

func (m IndexMeta) IsValidate(i IndexItem) bool

IsValidate return if the item is validate

func (*IndexMeta) Marshal

func (m *IndexMeta) Marshal() (data []byte, err error)

func (*IndexMeta) MarshalTo

func (m *IndexMeta) MarshalTo(data []byte) (int, error)

func (*IndexMeta) ProtoMessage

func (*IndexMeta) ProtoMessage()

func (*IndexMeta) Reset

func (m *IndexMeta) Reset()

func (*IndexMeta) Size

func (m *IndexMeta) Size() (n int)

func (*IndexMeta) String

func (m *IndexMeta) String() string

func (*IndexMeta) Unmarshal

func (m *IndexMeta) Unmarshal(data []byte) error

type Item

type Item struct {
	Key       string
	Value     []byte
	Timestamp int64
	TTL       uint32
	Flags     uint32
	// contains filtered or unexported fields
}

func (*Item) Free

func (i *Item) Free()

type Shard

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

func LoadCacheShard

func LoadCacheShard(fn string, options *ShardOptions) (*Shard, error)

func (*Shard) Close

func (s *Shard) Close() error

func (*Shard) Del

func (s *Shard) Del(key string) error

func (*Shard) GCLoop

func (s *Shard) GCLoop()

func (*Shard) Get

func (s *Shard) Get(key string) (Item, error)

func (*Shard) GetMetrics

func (s *Shard) GetMetrics() CacheMetrics

func (*Shard) GetStats

func (s *Shard) GetStats() CacheStats

func (*Shard) Set

func (s *Shard) Set(ci Item) error

type ShardOptions

type ShardOptions struct {
	Size      int64
	TTL       int64
	Allocator Allocator
}

Jump to

Keyboard shortcuts

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