|
@@ -18,8 +18,9 @@ import (
|
|
)
|
|
)
|
|
|
|
|
|
var (
|
|
var (
|
|
- child bool
|
|
|
|
- db *pgxpool.Pool
|
|
|
|
|
|
+ child bool
|
|
|
|
+ db *pgxpool.Pool
|
|
|
|
+ cachedWorlds Worlds
|
|
)
|
|
)
|
|
|
|
|
|
const (
|
|
const (
|
|
@@ -28,7 +29,7 @@ const (
|
|
helloworld = "Hello, World!"
|
|
helloworld = "Hello, World!"
|
|
worldselectsql = "SELECT id, randomNumber FROM World WHERE id = $1"
|
|
worldselectsql = "SELECT id, randomNumber FROM World WHERE id = $1"
|
|
worldupdatesql = "UPDATE World SET randomNumber = $1 WHERE id = $2"
|
|
worldupdatesql = "UPDATE World SET randomNumber = $1 WHERE id = $2"
|
|
- worldcachesql = "SELECT * FROM World"
|
|
|
|
|
|
+ worldcachesql = "SELECT * FROM World LIMIT $1"
|
|
fortuneselectsql = "SELECT id, message FROM Fortune"
|
|
fortuneselectsql = "SELECT id, message FROM Fortune"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -112,7 +113,7 @@ func ReleaseWorld(w *World) {
|
|
// WorldsPool ...
|
|
// WorldsPool ...
|
|
var WorldsPool = sync.Pool{
|
|
var WorldsPool = sync.Pool{
|
|
New: func() interface{} {
|
|
New: func() interface{} {
|
|
- return make(Worlds, 0, 512)
|
|
|
|
|
|
+ return make(Worlds, 0, 500)
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
|
|
@@ -144,6 +145,27 @@ func initDatabase() {
|
|
if err != nil {
|
|
if err != nil {
|
|
panic(err)
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
+ populateCache()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// this will populate the cached worlds for the cache test
|
|
|
|
+func populateCache() {
|
|
|
|
+ worlds := make(Worlds, worldcount)
|
|
|
|
+ rows, err := db.Query(context.Background(), worldcachesql, worldcount)
|
|
|
|
+ if err != nil {
|
|
|
|
+ panic(err)
|
|
|
|
+ }
|
|
|
|
+ for i := 0; i < worldcount; i++ {
|
|
|
|
+ w := &worlds[i]
|
|
|
|
+ if !rows.Next() {
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ if err := rows.Scan(&w.ID, &w.RandomNumber); err != nil {
|
|
|
|
+ panic(err)
|
|
|
|
+ }
|
|
|
|
+ //db.QueryRow(context.Background(), worldselectsql, RandomWorld()).Scan(&w.ID, &w.RandomNumber)
|
|
|
|
+ }
|
|
|
|
+ cachedWorlds = worlds
|
|
}
|
|
}
|
|
|
|
|
|
// jsonHandler :
|
|
// jsonHandler :
|
|
@@ -226,26 +248,15 @@ func plaintextHandler(c *fiber.Ctx) {
|
|
c.SendString(helloworld)
|
|
c.SendString(helloworld)
|
|
}
|
|
}
|
|
|
|
|
|
-var cachePopulated = false
|
|
|
|
-var catchedWorlds []World
|
|
|
|
-
|
|
|
|
-func populateCache() {
|
|
|
|
- worlds := AcquireWorlds()[:500]
|
|
|
|
- for i := 0; i < 500; i++ {
|
|
|
|
- w := &worlds[i]
|
|
|
|
- db.QueryRow(context.Background(), worldselectsql, RandomWorld()).Scan(&w.ID, &w.RandomNumber)
|
|
|
|
- }
|
|
|
|
- catchedWorlds = worlds
|
|
|
|
- cachePopulated = true
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
// cachedHandler :
|
|
// cachedHandler :
|
|
func cachedHandler(c *fiber.Ctx) {
|
|
func cachedHandler(c *fiber.Ctx) {
|
|
- if !cachePopulated {
|
|
|
|
- populateCache()
|
|
|
|
- }
|
|
|
|
n := QueriesCount(c)
|
|
n := QueriesCount(c)
|
|
- c.JSON(catchedWorlds[:n])
|
|
|
|
|
|
+ worlds := AcquireWorlds()[:n]
|
|
|
|
+ for i := 0; i < n; i++ {
|
|
|
|
+ worlds[i] = cachedWorlds[RandomWorld()-1]
|
|
|
|
+ }
|
|
|
|
+ c.JSON(worlds)
|
|
|
|
+ ReleaseWorlds(worlds)
|
|
}
|
|
}
|
|
|
|
|
|
// RandomWorld :
|
|
// RandomWorld :
|