Browse Source

cache the prepared template

Patrick Falls 12 years ago
parent
commit
b518eb1875
1 changed files with 5 additions and 4 deletions
  1. 5 4
      go/src/hello/hello.go

+ 5 - 4
go/src/hello/hello.go

@@ -38,6 +38,7 @@ const (
 var (
 var (
 	stmts = make(chan *sql.Stmt, MAX_CON)
 	stmts = make(chan *sql.Stmt, MAX_CON)
 	fortuneStmts = make(chan *sql.Stmt, MAX_CON)
 	fortuneStmts = make(chan *sql.Stmt, MAX_CON)
+	tmpl = template.Must(template.ParseFiles("templates/layout.html", "templates/fortune.html"))
 )
 )
 
 
 func jsonHandler(w http.ResponseWriter, r *http.Request) {
 func jsonHandler(w http.ResponseWriter, r *http.Request) {
@@ -71,7 +72,7 @@ func fortuneHandler(w http.ResponseWriter, r *http.Request) {
 	// the Fortune table contains 12 rows, and we'll add another Fortune ourselves
 	// the Fortune table contains 12 rows, and we'll add another Fortune ourselves
 	fortunes := make([]Fortune, 13)
 	fortunes := make([]Fortune, 13)
   
   
-  stmt := <-fortuneStmts // wait for a connection
+	stmt := <-fortuneStmts // wait for a connection
   
   
 	// Execute the query
 	// Execute the query
 	rows, err := stmt.Query()
 	rows, err := stmt.Query()
@@ -89,10 +90,10 @@ func fortuneHandler(w http.ResponseWriter, r *http.Request) {
 		}
 		}
 		i++
 		i++
 	}
 	}
-  fortunes[i].Message = "Additional fortune added at request time."
+	fortuneStmts <- stmt // return a connection
+  	fortunes[i].Message = "Additional fortune added at request time."
 	
 	
-  sort.Sort(ByMessage{fortunes})
-	var tmpl = template.Must(template.ParseFiles("templates/layout.html", "templates/fortune.html"))
+	sort.Sort(ByMessage{fortunes})
 	if err := tmpl.Execute(w, map[string]interface{} {"fortunes": fortunes}); err != nil {
 	if err := tmpl.Execute(w, map[string]interface{} {"fortunes": fortunes}); err != nil {
 		http.Error(w, err.Error(), http.StatusInternalServerError)
 		http.Error(w, err.Error(), http.StatusInternalServerError)
 	}
 	}