rescacher

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

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

Go to latest
Published: May 8, 2022 License: MIT Imports: 4 Imported by: 0

README

rescacher

The idea is when you have an API and the value of it changes over time, rescacher will cache the next value, next next value,... of it and just throw it out on time with no delay.

Contents:

Use cases

  • The next values being calculated by server can be had errors or failed to calculate, rescacher will retry the calculation after an configured interval and cache many predicted results so server can use them gradually.
  • Your users dont have to wait or the event will happen at the right time (no delay).

Installation

go get github.com/func25/rescacher

How to use

Sample

You can get an sample from cacher_test.go, basically it will cache next 50 results into database and retrieve each result in 1 second over time.

IGen

First, we want to know how to generate the result and the current "result id" of the api right now by using an object implement IGen interface:

type IGen interface {
	Generate(ctx context.Context, turn int) (interface{}, error) // generate result of turn X
	GetCurrentTurn(ctx context.Context) (int, error)             // get current turn
}
ICacher

Next, you need a ICacher that can store/ load the "predicted value" and cacher itself into the database, we already made an cacher based on redis:

cacher, err = rcredis.NewCacher(client, rcredis.CacherConfig{
		Name:       "example",
		Gennerator: gen, // object implement IGen
})
Load value from cacher
// return the result of turn "id"
res, err := cacher.PopOrGen(ctx, id) 
fmt.Println(res)

Functions

  • GetOrGen: get the result of turn X (if it is not exist then generate), but the result still remain in database

  • PopOrGen: like GetOrGen but we remove the result out of database when successfully retrieve

Status: pre-release

This lib is under developing, please notice when using it

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacherOption

type CacherOption func(*TurnCacher)

func OptGapTime

func OptGapTime(gapTime time.Duration) CacherOption

func OptGapTurn

func OptGapTurn(gapTurn int) CacherOption

func OptKeyPrefix

func OptKeyPrefix(prefix string) CacherOption

func OptLocker

func OptLocker(locker Locker) CacherOption

func OptResetTurnIfNotFound

func OptResetTurnIfNotFound() CacherOption

type ICacher

type ICacher interface {
	Load(ctx context.Context, turn int, pop bool) (interface{}, error) // load result of turn X (own db)
	Save(ctx context.Context, turn int, value interface{}) error       // save result of turn Y (own db)
	GetCacher(ctx context.Context) (int, error)                        // get current cached turn (own db)
	SetCacher(ctx context.Context, turn int) error                     // save current cached turn (own db)
}

type IGen

type IGen interface {
	Generate(ctx context.Context, turn int) (interface{}, error) // generate result of turn X (custom)
	GetCurrentTurn(ctx context.Context) (int, error)             // get current turn (custom)
}

type Locker

type Locker interface {
	Lock() error
	Unlock() error
}

type TurnCacher

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

functask: slowly warm up first

func NewTurnCacher

func NewTurnCacher(name string, cacher ICacher, gentor IGen, opts ...CacherOption) *TurnCacher

func (*TurnCacher) GetCacherKey

func (t *TurnCacher) GetCacherKey() string

func (*TurnCacher) GetOnly

func (t *TurnCacher) GetOnly(ctx context.Context, turn int) (interface{}, error)

func (*TurnCacher) GetOrGen

func (t *TurnCacher) GetOrGen(ctx context.Context, turn int) (interface{}, error)

-- get

func (*TurnCacher) GetTurnKey

func (t *TurnCacher) GetTurnKey(turn int) string

-- key

func (*TurnCacher) PopOrGen

func (t *TurnCacher) PopOrGen(ctx context.Context, turn int) (interface{}, error)

func (*TurnCacher) Start

func (t *TurnCacher) Start()

-- progress

func (*TurnCacher) Stop

func (t *TurnCacher) Stop()

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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