Download:Node.js+Koa2+MySQL to create a boutique project of front and back end separation “Old Island”

Nodejs has gradually become one of the required technologies for front-end students to advance. This course will lead you to develop a high-quality project “Old Island” from 0 to 1, and teach you to use Node.js KOA2 to create a super user-friendly Web framework, so that front-end students can completely overcome the server-side development difficulties and headache of Mysql database knowledge. In addition to the routine business development, this course also includes a large number of ADVANCED JS knowledge applications, asynchronous programming and programming thinking, which is of great help to students to develop front-end. The course is not only to lead front-end students to deeply learn Nodejs server development, but also a powerful tool for students to get employment and promotion. It is suitable for front-end engineers who want to develop towards the big front-end direction, engineers who want to deeply learn Node.js and students who want to creatively complete the “graduation project” and meet the requirements of technical reserve

Node.js func AsyncAdd(run func() error) {//TODO: Go run()} func GetInstance(CTX context. context, ID uint64) (string, error) { data,err := GetFromRedis(ctx,id) if err ! = nil && err ! Nil{return “”, err} if err == redis.Nil{data,err = GetFromDB(CTX,id) if err! = nil{ return “”, err } AsyncAdd(func() error{ return UpdateCache(ctx,id,data) }) } return data,nil } func GetFromRedis(ctx context.Context,id uint64) (string,error) { // TODO: Return “”,nil} func GetFromDB(CTX context. context,id uint64) (string,error) {// TODO: Return “”,nil} func UpdateCache(CTX context. context,id interface{},data string) error {// TODO: Return nil} func main() {CTX,cancel := context.withtimeout (context.background (), 3 * time.Second) defer cancel() _,err := GetInstance(ctx,2021) if err ! = nil{return}} parse let’s just parse, what does this piece of code do? In fact, it is very simple. If we want to get a piece of information, we will first get it from the cache. If we can’t get it from the cache, we will get it from DB. Isn’t the whole design great, but in practice, asynchronous cache updates have never been successful?

The recipe for failure lies in this code:

AsyncAdd(func() error{
        return UpdateCache(ctx,id,data)
    })
Copy the code

There is only one cause of error, this is CTX, if changed to this, nothing is wrong.

AsyncAdd(func() error{

ctxAsync,cancel := context.WithTimeout(context.Background(),3 * time.Second)

defer cancel()

return UpdateCache(ctxAsync,id,data)

})