cache

package module
v0.21.4 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2021 License: GPL-3.0 Imports: 6 Imported by: 0

README

cache

light cache in go

  • GoDoc

Introduction

Installation
go get "github.com/iTrellis/cache"
Features
  • Simple lru
  • It can set Unique | Bag | DuplicateBag values per key
TODO
  • main node: to manage cache
  • consistent hash to several nodes to install keys
Cache

cache is manager for k-vs tables base on TableCache

// Cache Manager functions for executing k-v tables base on TableCache
type Cache interface {
	// Returns a list of all tables at the node.
	All() []string
	// Get TableCache
	GetTableCache(tab string) (TableCache, bool)
	// Creates a new table.
	New(tab string, options ...OptionFunc) error
	// Inserts the object or all of the objects in list.
	Insert(tab string, key, value interface{}) bool
	// Inserts the object or all of the objects with expired time in list.
	InsertExpire(tab string, key, value interface{}, expire time.Duration) bool
	// Deletes the entire table Tab.
	Delete(tab string) bool
	// Deletes all objects with key, Key from table Tab.
	DeleteObject(tab string, key interface{}) bool
	// Delete all objects in the table Tab. Remain table in cache.
	DeleteObjects(tab string)
	// Look up values with key, Key from table Tab.
	Lookup(tab string, key interface{}) ([]interface{}, bool)
	// Look up all values in the Tab.
	LookupAll(tab string) (map[interface{}][]interface{}, bool)
	// Returns true if one or more elements in the table has key Key, otherwise false.
	Member(tab string, key interface{}) bool
	// Retruns all keys in the table Tab.
	Members(tab string) ([]interface{}, bool)
	// Set key Key expire time in the table Tab.
	SetExpire(tab string, key interface{}, expire time.Duration) bool
}
TableCache

table cache is manager for k-vs

// TableCache
type TableCache interface {
	// Inserts the object or all of the objects in list.
	Insert(key, values interface{}) bool
	// Inserts the object or all of the objects with expired time in list.
	InsertExpire(key, value interface{}, expire time.Duration) bool
	// Deletes all objects with key: Key.
	DeleteObject(key interface{}) bool
	// Delete all objects in the table Tab. Remain table in cache.
	DeleteObjects()
	// Returns true if one or more elements in the table has key: Key, otherwise false.
	Member(key interface{}) bool
	// Retruns all keys in the table Tab.
	Members() ([]interface{}, bool)
	// Look up values with key: Key.
	Lookup(key interface{}) ([]interface{}, bool)
	// Look up all values in the Tab.
	LookupAll() (map[interface{}][]interface{}, bool)
	// Set Key Expire time
	SetExpire(key interface{}, expire time.Duration) bool
}
Sample: NewTableCache with options

Examples

Documentation

Index

Constants

View Source
const (
	NoExpire time.Duration = 0
)

Timers

Variables

View Source
var (
	ErrTableExists           = errors.New("table already exists")
	ErrNewTableCache         = errors.New("failed new table")
	ErrOrderSetMustBeBool    = errors.New("order set must be bool")
	ErrUnknownTableOption    = errors.New("unknown table option")
	ErrUnknownTableValueMode = errors.New("unknown table value mode")
)

errors

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Returns a list of all tables at the node.
	All() []string
	// Get TableCache
	GetTableCache(tab string) (TableCache, bool)
	// Creates a new table.
	New(tab string, options ...OptionFunc) error
	// Inserts the object or all of the objects in list.
	Insert(tab string, key, value interface{}) bool
	// Inserts the object or all of the objects with expired time in list.
	InsertExpire(tab string, key, value interface{}, expire time.Duration) bool
	// Deletes the entire table Tab.
	Delete(tab string) bool
	// Deletes all objects with key, Key from table Tab.
	DeleteObject(tab string, key interface{}) bool
	// Delete all objects in the table Tab. Remain table in cache.
	DeleteObjects(tab string)
	// Look up values with key, Key from table Tab.
	Lookup(tab string, key interface{}) ([]interface{}, bool)
	// Look up all values in the Tab.
	LookupAll(tab string) (map[interface{}][]interface{}, bool)
	// Returns true if one or more elements in the table has key Key, otherwise false.
	Member(tab string, key interface{}) bool
	// Retruns all keys in the table Tab.
	Members(tab string) ([]interface{}, bool)
	// Set key Key expire time in the table Tab.
	SetExpire(tab string, key interface{}, expire time.Duration) bool
}

Cache Manager functions for executing k-v tables base on TableCache

func New

func New() Cache

New return cache manager

type DataValues

type DataValues struct {
	Key    interface{}
	Values []interface{}
	Exists map[interface{}]bool
	Expire *time.Time
}

DataValues define k-vs struct

type EvictCallback

type EvictCallback func(key interface{}, value interface{})

EvictCallback is used to get a callback when a cache entry is evicted

type LRU

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

LRU implements a non-thread safe fixed size LRU cache

func NewLRU

func NewLRU(name string, opts Options) (*LRU, error)

NewLRU constructs an LRU of the given options

func (*LRU) DeleteObject

func (p *LRU) DeleteObject(key interface{}) (present bool)

DeleteObject deletes the provided key from the cache, returning if the key was contained.

func (*LRU) DeleteObjects

func (p *LRU) DeleteObjects()

DeleteObjects is used to completely clear the cache.

func (*LRU) Insert

func (p *LRU) Insert(key, value interface{}) (evicted bool)

Insert insert a value to the cache. Returns true if an eviction occurred.

func (*LRU) InsertExpire

func (p *LRU) InsertExpire(key, value interface{}, expire time.Duration) bool

InsertExpire insert a value to the cache. Returns true if insert kv successful.

func (*LRU) Lookup

func (p *LRU) Lookup(key interface{}) ([]interface{}, bool)

Lookup Look up values with key: Key.

func (*LRU) LookupAll

func (p *LRU) LookupAll() (items map[interface{}][]interface{}, ok bool)

LookupAll Look up all key-value pairs.

func (*LRU) Member

func (p *LRU) Member(key interface{}) bool

Member Returns true if one or more elements in the table has key: Key, otherwise false.

func (*LRU) Members

func (p *LRU) Members() (keys []interface{}, ok bool)

Members Retruns all keys in the table Tab.

func (*LRU) RemoveOldest

func (p *LRU) RemoveOldest() (key, value interface{}, ok bool)

RemoveOldest removes the oldest item from the cache.

func (*LRU) SetExpire

func (p *LRU) SetExpire(key interface{}, expire time.Duration) bool

SetExpire Set Key Expire time

type OptionFunc

type OptionFunc func(*Options)

OptionFunc 参数处理函数

func OptionEvict

func OptionEvict(evict EvictCallback) OptionFunc

OptionEvict set the evict ballback

func OptionKeySize

func OptionKeySize(size int) OptionFunc

OptionKeySize set the size of keys

func OptionValueMode

func OptionValueMode(mode ValueMode) OptionFunc

OptionValueMode set the values' model

type Options

type Options struct {
	ValueMode ValueMode

	Size int

	Evict EvictCallback
}

Options configure

type TableCache

type TableCache interface {
	// Inserts the object or all of the objects in list.
	Insert(key, values interface{}) bool
	// Inserts the object or all of the objects with expired time in list.
	InsertExpire(key, value interface{}, expire time.Duration) bool
	// Deletes all objects with key: Key.
	DeleteObject(key interface{}) bool
	// Delete all objects in the table Tab. Remain table in cache.
	DeleteObjects()
	// Returns true if one or more elements in the table has key: Key, otherwise false.
	Member(key interface{}) bool
	// Retruns all keys in the table Tab.
	Members() ([]interface{}, bool)
	// Look up values with key: Key.
	Lookup(key interface{}) ([]interface{}, bool)
	// Look up all values in the Tab.
	LookupAll() (map[interface{}][]interface{}, bool)
	// Set Key Expire time
	SetExpire(key interface{}, expire time.Duration) bool
}

TableCache table manager for k-vs functions

func NewTableCache

func NewTableCache(name string, opts ...OptionFunc) (TableCache, error)

NewTableCache constructs a fixed size cache.

type ValueMode

type ValueMode int

ValueMode define value mode

const (
	// only one value
	ValueModeUnique ValueMode = iota
	// The table is a bag table, which can have many objects
	// but only one instance of each object, per key.
	ValueModeBag
	// The table is a duplicate_bag table, which can have many objects,
	// including multiple copies of the same object, per key.
	ValueModeDuplicateBag
)

ValueMode

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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