Skip to content

irmorteza/go-cache-wb

Repository files navigation

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)
	}


}

.

Releases

No releases published

Packages

No packages published

Languages