Commit c519eeef authored by gwenn's avatar gwenn

Stmt cache plugged but current default size is zero.

parent e0a89fb2
......@@ -15,24 +15,24 @@ const (
)
// Like http://www.sqlite.org/tclsqlite.html#cache
type Cache struct {
type cache struct {
m sync.Mutex
l *list.List
maxSize int // Cache turned off when maxSize <= 0
}
func newCache() *Cache {
func newCache() *cache {
return newCacheSize(defaultCacheSize)
}
func newCacheSize(maxSize int) *Cache {
func newCacheSize(maxSize int) *cache {
if maxSize <= 0 {
return &Cache{maxSize: maxSize}
return &cache{maxSize: maxSize}
}
return &Cache{l: list.New(), maxSize: maxSize}
return &cache{l: list.New(), maxSize: maxSize}
}
// TODO To be called in Conn#Prepare
func (c *Cache) find(sql string) *Stmt {
// To be called in Conn#CacheOrPrepare
func (c *cache) find(sql string) *Stmt {
if c.maxSize <= 0 {
return nil
}
......@@ -50,8 +50,8 @@ func (c *Cache) find(sql string) *Stmt {
}
// To be called in Stmt#Finalize
func (c *Cache) release(s *Stmt) error {
if c.maxSize <= 0 || len(s.tail) > 0 {
func (c *cache) release(s *Stmt) error {
if c.maxSize <= 0 || len(s.tail) > 0 || s.Busy() {
return s.finalize()
}
c.m.Lock()
......@@ -67,8 +67,8 @@ func (c *Cache) release(s *Stmt) error {
}
// Finalize and free the cached prepared statements
// (To be called in Conn#Close)
func (c *Cache) flush() {
// To be called in Conn#Close
func (c *cache) flush() {
if c.maxSize <= 0 {
return
}
......
package sqlite_test
import (
// . "github.com/gwenn/gosqlite"
"testing"
)
func TestDisabledCache(t *testing.T) {
db := open(t)
defer db.Close()
db.SetCacheSize(0)
if size, maxSize := db.CacheSize(); size != 0 || maxSize != 0 {
t.Errorf("%d <> %d || %d <> %d", 0, size, 0, maxSize)
}
s, err := db.CacheOrPrepare("SELECT 1")
checkNoError(t, err, "couldn't prepare stmt: %#v")
if !s.Cacheable {
t.Error("expected cacheable stmt")
}
err = s.Finalize()
checkNoError(t, err, "couldn't finalize stmt: %#v")
if size, maxSize := db.CacheSize(); size != 0 || maxSize != 0 {
t.Errorf("%d <> %d || %d <> %d", 0, size, 0, maxSize)
}
}
......@@ -184,7 +184,7 @@ func (c *Conn) LastError() error {
type Conn struct {
db *C.sqlite3
Filename string
stmtCache *Cache
stmtCache *cache
authorizer *sqliteAuthorizer
busyHandler *sqliteBusyHandler
profile *sqliteProfile
......
......@@ -118,6 +118,26 @@ func (c *Conn) Prepare(cmd string, args ...interface{}) (*Stmt, error) {
return s, nil
}
// Like Prepare but first look in the Stmt cache.
func (c *Conn) CacheOrPrepare(cmd string, args ...interface{}) (*Stmt, error) {
s := c.stmtCache.find(cmd)
if s != nil {
if len(args) > 0 {
err := s.Bind(args...)
if err != nil {
s.finalize() // don't put it back in the cache
return nil, err
}
}
return s, nil
}
s, err := c.Prepare(cmd, args...)
if s != nil {
s.Cacheable = true
}
return s, err
}
// One-step statement execution.
// Don't use it with SELECT or anything that returns data.
// The Stmt is reset at each call.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment