Skip to content

Commit

Permalink
make stmtCacher thread-safe
Browse files Browse the repository at this point in the history
Go maps aren't thread safe, adding a mutex to guard the map
  • Loading branch information
arsonox committed May 8, 2014
1 parent 920ea87 commit bb3c2d3
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion stmtcacher.go
@@ -1,6 +1,9 @@
package squirrel

import "database/sql"
import (
"database/sql"
"sync"
)

// Prepareer is the interface that wraps the Prepare method.
//
Expand All @@ -20,6 +23,7 @@ type DBProxy interface {
type stmtCacher struct {
prep Preparer
cache map[string]*sql.Stmt
mu sync.Mutex
}

// NewStmtCacher returns a DBProxy wrapping prep that caches Prepared Stmts.
Expand All @@ -30,6 +34,8 @@ func NewStmtCacher(prep Preparer) DBProxy {
}

func (sc *stmtCacher) Prepare(query string) (*sql.Stmt, error) {
sc.mu.Lock()
defer sc.mu.Unlock()
stmt, ok := sc.cache[query]
if ok {
return stmt, nil
Expand Down

0 comments on commit bb3c2d3

Please sign in to comment.