Skip to content

Latest commit

 

History

History
executable file
·
69 lines (47 loc) · 1.06 KB

README.md

File metadata and controls

executable file
·
69 lines (47 loc) · 1.06 KB

AsyncCache

An async Cache Middleware in golang

##Install

go get github.com/lincolnlee/AsyncCache

##Example

package main

import (
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
	"github.com/lincolnlee/AsyncCache"
)

func main() {
	DoSomething()

	u := getUser(1)

	DoOtherThings()
}

func getUser(uid int) UserInfo {
	key := "UserInfo_" + string(uid)

	v := AsyncCache.InstanceContainer.AsyncCacheHandler.AsyncGetAndUpdateData(
		func() interface{} {
			return getUserDataFromDB(uid)
		},
		key)

	return UserInfo(v)
}

func getUserDataFromDB(uid int) UserInfo {
	db, _ := sql.Open("mysql", dbConnString)
	defer db.Close()

	rows, err := db.Query("SELECT id,username,age FROM users where id = ?", uid)
	checkError(err)

	var user UserInfo = nil

	for rows.Next() {
		var id int32
		var username string
		var age int32
		_ = rows.Scan(&id, &username, &age)

		user = UserInfo{id, username, age}
	}

	return user
}

##LICENSE

AsyncCache is licensed under the Apache Licence, Version 2.0.