Skip to content

Repository files navigation

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"
)
funcmain() {
DoSomething()
u:=getUser(1)
DoOtherThings()
}
funcgetUser(uidint) UserInfo {
key:="UserInfo_"+string(uid)
v:=AsyncCache.InstanceContainer.AsyncCacheHandler.AsyncGetAndUpdateData(
func() interface{} {
returngetUserDataFromDB(uid)
},
key)
returnUserInfo(v)
}
funcgetUserDataFromDB(uidint) UserInfo {
db, _:=sql.Open("mysql", dbConnString)
deferdb.Close()
rows, err:=db.Query("SELECT id,username,age FROM users where id = ?", uid)
checkError(err)
varuserUserInfo=nilforrows.Next() {
varidint32varusernamestringvarageint32_=rows.Scan(&id, &username, &age)
user=UserInfo{id, username, age}
}
returnuser
}

##LICENSE

AsyncCache is licensed under the Apache Licence, Version 2.0.

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages