cachewb

package module
v0.0.0-...-ff7a2a4 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

README

Golang Cache with write-back strategy

go-cache-wb is a cache with write back(behind) strategy

Please read following links, to know more about caching strategies

Caution: This package is under developing

Download cacheWB

go get -u github.com/irmorteza/go-cache-wb

dependencies

github.com/go-sql-driver/mysql

How it works

CacheWB is the cache work in front of the storage, and does its CRUD actions on cache. To use the cachewb, you need do following steps:

  1. Create favor struct: The way, how CacheWB know about the storage structure, obtain from Struct fields and its Tags. Tags are used to clarifing, detail of storage to CacheWB. Tags are:
    • storage: Any field of struct who need to point a field in storage, should have storage tag with value of its real name in storage storage:"credit"
    • key: The key field od struct. It should be unique in storage. All CRUD action triger base of key field. key:"1"
    • update: The fields will updates after an change on struct value. Its default is 1 (enable). update:"1"
    • autoInc: Incremental field in storage. autoInc are required if you want to use insert and update. this field doesn't involve (change) in update. autoInc:"1"
  2. Add EmbedME: Embed build-in struct cachewb.EmbedME to your struct
  3. Handle update actions by Create functions (property): For specific action (for example add to a field) create an function, in that function, first call c.EmbedME.IncUpdate(). It would inform cachewb, the change on one of its item.
Supported storage (databases)
  • MySQL
  • SQL Server
  • MongoDB
Complete example:
import (
	"github.com/irmorteza/cachewb"
	"fmt"
	"time"
)
/*
	CREATE TABLE members (
	  id int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
	  name varchar(50) DEFAULT NULL,
	  credit int(11) DEFAULT NULL,
	  age smallint(6) DEFAULT NULL,
	  lastTime datetime DEFAULT NULL,
	  PRIMARY KEY (id)
	)
*/

type Members struct {
	cachewb.EmbedME
	Id       int64     `storage:"id" autoInc:"1"`
	Name     string    `storage:"name" key:"1"`
	Credit   int64     `storage:"credit"`
	Age      int64     `storage:"age" update:"0"`
	LastTime time.Time `storage:"lastTime"`
}

func (c *Members) AddCredit(a int64)  {
	e := c.EmbedME.IncUpdate()
	if e != nil {
		fmt.Println(e)
		return
	}
	c.Credit += a
}

func main() {
	cfg := cachewb.Config{
		Interval:               2,
		CacheWriteLatencyCount: 2,
		CacheWriteLatencyTime:  10,
		AccessTTL:              5,
		StorageName:            cachewb.MYSQL,
		Database: cachewb.ConfigMysql{
			Username:          "irmorteza",
			Password:          "123",
			Port:              3306,
			Host:              "localhost",
			DBName:            "hss_db",
			MaxOpenConnection: -1},
	}

	c := cachewb.NewCacheWB(cfg)
	f := c.GetContainer("members", Members{})
	res, e := f.Get("Morteza")
	fmt.Println(e)
	if e == nil {
		out := res.(*Members)
		fmt.Println(out)
	}


}

.

Documentation

Index

Constants

View Source
const (
	IntervalWorkerMaintainer           = 10
	IntervalWorkerQueryIndexMaintainer = 500
	CacheInsertAsyncLatency            = 1
	CacheFlushUpdatesLatencyTime       = 30
	CacheFlushUpdatesLatencyCount      = 10
	AccessTTLItems                     = 10
	AccessTTLQueryIndex                = 10
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheContainer

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

func (*CacheContainer) Flush

func (c *CacheContainer) Flush(withLock bool) error

Flush all updates in container to storage

func (*CacheContainer) Get

func (c *CacheContainer) Get(m map[string]interface{}) ([]interface{}, error)

Return an array of Items from cache. This method check cache first, and then search in storage, if not found. parameter m stand for query

func (*CacheContainer) GetBySquirrel

func (c *CacheContainer) GetBySquirrel(squirrelArgs ...interface{}) ([]interface{}, error)

Return an array of Items from cache. This method check cache first, and then search in storage, if not found. parameter squirrelArgs is an where query build by squirrel library you can find its document in github.com/Masterminds/squirrel

func (*CacheContainer) GetOne

func (c *CacheContainer) GetOne(uniqueIdentityValue interface{}) (interface{}, error)

Return an Item from cache. This method check cache first, and then search in storage, if not found. It return result for uniqueIdentityValue, if there was, if there were more than one result, you should use Get()

func (*CacheContainer) GetStatistic

func (c *CacheContainer) GetStatistic() map[string]map[string]interface{}

func (*CacheContainer) Insert

func (c *CacheContainer) Insert(in ...interface{}) (map[string]int64, error)

Insert Items to container. Item add to database synchronously,

func (*CacheContainer) InsertAsync

func (c *CacheContainer) InsertAsync(in ...interface{}) error

Asynchronously insert items to container. Items bulk insert to database.

func (*CacheContainer) Remove

func (c *CacheContainer) Remove(uniqueIdentities ...interface{}) (map[string]int64, error)

Remove from cache and storage just by uniqueIdentities

func (*CacheContainer) RemoveFromCache

func (c *CacheContainer) RemoveFromCache(uniqueIdentities ...interface{})

Remove from cache just by uniqueIdentities

func (*CacheContainer) RemoveIndirect

func (c *CacheContainer) RemoveIndirect(m map[string]interface{}) (map[string]int64, error)

Remove from cache and storage by any keys (caution: This method may have some overload on storage) Unlike method `Remove`, You can use `RemoveIndirect` to removeByUniqueIdentity from cache and storage by any keys. First, RemoveIndirect call storage.Get() by keys and values arguments, internally, to find uniqueIdentities. And then removeByUniqueIdentity then by uniqueIdentities through the `Remove` method

type CacheWB

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

func NewCacheWB

func NewCacheWB() *CacheWB

Get an CacheWB variable

func (*CacheWB) FlushAll

func (c *CacheWB) FlushAll(withLock bool)

Flush all data remained in cache (for all containers), but not flushed to database yet. withLock cause to avoid any updates on container (while the container is flushing)

func (*CacheWB) GetContainer

func (c *CacheWB) GetContainer(tableName string, cfg Config, objType interface{}) *CacheContainer

Get the container of given table name, with type of objType, objType is a variable of an structure that will contain a row of table

func (*CacheWB) GetViewContainer

func (c *CacheWB) GetViewContainer(viewName string, viewQuery string, cfg Config, objType interface{}) *CacheContainer

func (*CacheWB) GracefulShutdown

func (c *CacheWB) GracefulShutdown() bool

GracefulShutdown avoid all new update in all container and flush all containers too. After calling it, no update accepts.

type Config

type Config struct {
	// Interval of maintainer worker.
	// The worker check items of cache for theirs update and time to live status
	// Default is 10 seconds
	IntervalWorkerMaintainer int
	// Interval of QueryIndexMaintainer worker.
	// QueryIndexMaintainer check items of QueryIndex in cache for theirs time to live status
	// Default is 500 seconds
	IntervalWorkerQueryIndexMaintainer int
	// Maximum time, Insert worker wait for new update to make batch insert.
	// This parameter is corresponding to InsertAsync
	// Default is 1 second
	CacheInsertAsyncLatency int
	// For an item in cache: Maximum time after last update, that updates flush to storage
	// Default is 30 seconds
	CacheFlushUpdatesLatencyTime int
	// For an item in cache: Maximum updates count, that updates flush to storage
	// Default is 10 seconds
	CacheFlushUpdatesLatencyCount int
	// Type of database
	StorageName StorageKind
	// Config of database is using
	Database interface{}
	// Maximum time an item will remain in cache without any access. Then it will be removed
	// Default is 10 seconds
	AccessTTLItems int
	// Maximum time an QueryIndex will remain in cache without any access. Then it will be removed
	// Default is 10 seconds
	AccessTTLQueryIndex int
	// Enable Log
	// Default is false
	Log bool
	// Enable gathering statistics
	// Default is false
	Statistic bool
}

type ConfigMysql

type ConfigMysql struct {
	Host              string
	Username          string
	Password          string
	Port              int
	DBName            string
	MaxOpenConnection int
}

type EmbedME

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

func (*EmbedME) IncUpdate

func (c *EmbedME) IncUpdate() error

Trigger cache for new update

func (*EmbedME) UpdateStorage

func (c *EmbedME) UpdateStorage() error

Flush updates of holder item in storage

type StorageKind

type StorageKind uint
const (
	Invalid StorageKind = iota
	MYSQL
	MONGODB
	SQL
)

Jump to

Keyboard shortcuts

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