Created
July 23, 2024 08:50
-
-
Save hxzhouh/bf7d6276d6a703628abbd812964c3081 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"golang.org/x/sync/singleflight" | |
"log" | |
"time" | |
) | |
type User struct { | |
Name string | |
Age int | |
} | |
var Cache map[string]User | |
var g *singleflight.Group | |
const clients = 5 | |
func init() { | |
Cache = make(map[string]User) | |
g = new(singleflight.Group) | |
} | |
func GetUser(name string) User { | |
if user, ok := Cache[name]; ok { | |
return user | |
} | |
// get user from db | |
return GetUserFromDBSingleFlight(name) | |
} | |
func GetUserFromDBSingleFlight(name string) User { | |
// use singleflight to avoid duplicate request | |
v1, _, _ := g.Do("user", func() (interface{}, error) { | |
return GetUserFromDB(name), nil | |
}) | |
return v1.(User) | |
} | |
func GetUserFromDB(name string) User { | |
log.Println("get user from db") | |
// time-consuming operation | |
time.Sleep(100 * time.Millisecond) | |
return User{ | |
Name: "huizhou92", | |
Age: 32, | |
} | |
} | |
func main() { | |
for i := 0; i < clients; i++ { | |
go func() { | |
log.Println(GetUser("huizhou92")) | |
}() | |
} | |
time.Sleep(1 * time.Second) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment