Code Monkey home page Code Monkey logo

rescacher's Introduction

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

rescacher's People

Contributors

func25 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.