smartcache

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2022 License: MIT Imports: 9 Imported by: 1

README

SmartCache

Upgrade from my dumbcache

Go Reference Go

Hope it can be tool to help your works.

Installation
go get github.com/teng231/smartcache
How it work?

flow like that:

┌────────┐       ┌────────┐      ┌───────────────┐
│        │       │        │      │               │
│ Engine ├─────►│ Session├─────►│   Collection  │
│        │       │        │      │               │
└────────┘       └────────┘      └───────────────┘
  • When start app you create engine. Engine define structure of collection.
  • Engine provider select to access module cache.
  • Session process cache or set, get value to cache, from local module or remote module
  • Collection local cache module
Example

You can see full example here: Example


cache := smartcache.Start(
    &smartcache.CollectionConfig{
        Key:      "key1",
        Capacity: 100,
    },
)

if err := cache.Select(context.TODO(), "key1").Upsert("abc", 1); err != nil {
    log.Print(err)
}
var out1 int
cache.Select(context.TODO(), "key1").Get("abc", nil).Exec(&out1)
log.Print(out1)

Benmark Result:

follow link https://www.cloudbees.com/blog/real-life-go-benchmarking

$ go test -cpuprofile=cpu.out -benchmem -memprofile=mem.out -bench=BenchmarkEngineCacheWrite -run=^a

goos: linux
goarch: amd64
pkg: github.com/teng231/smartcache
cpu: Intel(R) Core(TM) i5-10400 CPU @ 2.90GHz
BenchmarkEngineCacheWrite/input_size_100-12         	1000000000	         0.0000460 ns/op	       0 B/op	       0 allocs/op
BenchmarkEngineCacheWrite/input_size_1000-12        	1000000000	         0.0004365 ns/op	       0 B/op	       0 allocs/op
BenchmarkEngineCacheWrite/input_size_74382-12       	1000000000	         0.05490 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/teng231/smartcache	0.513s
$ go test -cpuprofile=cpu.out -benchmem -memprofile=mem.out -bench=BenchmarkEngineCachex100000W10g -run=^a

goos: linux
goarch: amd64
pkg: github.com/teng231/smartcache
cpu: Intel(R) Core(TM) i5-10400 CPU @ 2.90GHz
BenchmarkEngineCachex100000W10g-12    	1000000000	         0.008111 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/teng231/smartcache	0.229s

Contributed: Quang Anh

Documentation

Index

Constants

View Source
const (
	E_not_found_any_collection_key = "not_found_any_collection_key"
	E_upsert_problem               = "upsert_problem"
	E_remove_problem               = "remove_problem"
	E_no_item_to_get               = "no_item_to_get"
	E_cancelled                    = "cancelled"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Collection

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

func CreateCollection

func CreateCollection(config *CollectionConfig) (*Collection, error)

func (*Collection) Delete

func (c *Collection) Delete(ctx context.Context, key interface{}) error

func (*Collection) GC

func (c *Collection) GC() error

GC remove key expired you need slow run it

func (*Collection) Get

func (c *Collection) Get(ctx context.Context, key interface{}) (interface{}, bool)

func (*Collection) IsKeyExisted

func (c *Collection) IsKeyExisted(key interface{}) bool

func (*Collection) Iter

func (c *Collection) Iter(ctx context.Context, key interface{}, filtering func(item interface{}, index int))

func (*Collection) Key

func (c *Collection) Key() string

func (*Collection) Len

func (c *Collection) Len() int

func (*Collection) Upsert

func (c *Collection) Upsert(ctx context.Context, key interface{}, value interface{}) error

func (*Collection) Upserts

func (c *Collection) Upserts(ctx context.Context, in ...*CollectionKV) (int, error)

type CollectionConfig

type CollectionConfig struct {
	Key            string
	Capacity       int
	ExpireDuration time.Duration
	GCInterval     time.Duration
}

type CollectionKV

type CollectionKV struct {
	Key   interface{} `json:"key"`
	Value interface{} `json:"value"`
}

type CollectionValue

type CollectionValue struct {
	Created int64       `json:"created"`
	Value   interface{} `json:"value"`
}

* Collection is define a structure of data Save data inmem here

type Engine

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

* Engine is define a structure for config and save all data. When engine run will create a session to execute with condition.

func Start

func Start(cfs ...*CollectionConfig) *Engine

func (*Engine) AddCollection

func (e *Engine) AddCollection(cfs ...*CollectionConfig) error

func (*Engine) Collection

func (e *Engine) Collection() map[string]*Collection

func (*Engine) CollectionConfig

func (e *Engine) CollectionConfig() map[string]*CollectionConfig

func (*Engine) Info

func (e *Engine) Info()

func (*Engine) Select

func (e *Engine) Select(ctx context.Context, collectionKey string) *Session

type GetterFn

type GetterFn func(interface{}) (interface{}, error)

type ICollection

type ICollection interface {
	Upsert(ctx context.Context, key, value interface{}) error
	Upserts(ctx context.Context, in ...*CollectionKV) (int, error)
	Delete(ctx context.Context, key interface{}) error
	Get(ctx context.Context, key interface{}) (interface{}, bool)
	Iter(ctx context.Context, key interface{}, filtering func(item interface{}, index int))
	Key() string
	Len() int
	IsKeyExisted(key interface{}) bool
	GC()
}

type IEngine

type IEngine interface {
	Select(ctx context.Context, collectionKey string) ICollection
	AddCollection(cf ...*CollectionConfig) error
	Collection() map[string]*Collection
	CollectionConfig() map[string]*CollectionConfig
	Info()
}

type ISession

type ISession interface {
	Filter(iter func(interface{}, int) bool, getterFns ...GetterFn) ISession
	Get(iter func(interface{}, int) bool, getterFns ...GetterFn) error
	Exec(outptr interface{}) error
	Upsert(key, value interface{}, setterFns ...SetterFn) error
	Delete(key interface{}, setterFns ...SetterFn) error
	Close()
}

type Session

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

func (*Session) Close

func (s *Session) Close()

func (*Session) Delete

func (s *Session) Delete(key interface{}, setterFns ...SetterFn) error

func (*Session) Exec

func (s *Session) Exec(outptr interface{}) (bool, error)

func (*Session) Filter

func (s *Session) Filter(key interface{}, iter func(interface{}, int) bool, getterFns ...GetterFn) *Session

func (*Session) Get

func (s *Session) Get(key interface{}, iter func(interface{}, int) bool, getterFns ...GetterFn) *Session

func (*Session) KeyBulder

func (s *Session) KeyBulder(sim interface{}) interface{}

func (*Session) Upsert

func (s *Session) Upsert(key interface{}, value interface{}, setterFns ...SetterFn) error

type SessionConfig

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

type SetterFn

type SetterFn func(interface{}, interface{}) error

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

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