Browse Source

Added missing cluster configuration option to adjust the size of the cache.

Phillip Couto 11 years ago
parent
commit
f56b352c90
1 changed files with 35 additions and 25 deletions
  1. 35 25
      cluster.go

+ 35 - 25
cluster.go

@@ -14,36 +14,38 @@ import (
 // behavior to fit the most common use cases. Applications that requre a
 // different setup must implement their own cluster.
 type ClusterConfig struct {
-	Hosts           []string      // addresses for the initial connections
-	CQLVersion      string        // CQL version (default: 3.0.0)
-	ProtoVersion    int           // version of the native protocol (default: 2)
-	Timeout         time.Duration // connection timeout (default: 600ms)
-	DefaultPort     int           // default port (default: 9042)
-	Keyspace        string        // initial keyspace (optional)
-	NumConns        int           // number of connections per host (default: 2)
-	NumStreams      int           // number of streams per connection (default: 128)
-	Consistency     Consistency   // default consistency level (default: Quorum)
-	Compressor      Compressor    // compression algorithm (default: nil)
-	Authenticator   Authenticator // authenticator (default: nil)
-	RetryPolicy     RetryPolicy   // Default retry policy to use for queries (default: 0)
-	SocketKeepalive time.Duration // The keepalive period to use, enabled if > 0 (default: 0)
-	ConnPoolType    NewPoolFunc   // The function used to create the connection pool for the session (default: NewSimplePool)
-	DiscoverHosts   bool          // If set, gocql will attempt to automatically discover other members of the Cassandra cluster (default: false)
+	Hosts            []string      // addresses for the initial connections
+	CQLVersion       string        // CQL version (default: 3.0.0)
+	ProtoVersion     int           // version of the native protocol (default: 2)
+	Timeout          time.Duration // connection timeout (default: 600ms)
+	DefaultPort      int           // default port (default: 9042)
+	Keyspace         string        // initial keyspace (optional)
+	NumConns         int           // number of connections per host (default: 2)
+	NumStreams       int           // number of streams per connection (default: 128)
+	Consistency      Consistency   // default consistency level (default: Quorum)
+	Compressor       Compressor    // compression algorithm (default: nil)
+	Authenticator    Authenticator // authenticator (default: nil)
+	RetryPolicy      RetryPolicy   // Default retry policy to use for queries (default: 0)
+	SocketKeepalive  time.Duration // The keepalive period to use, enabled if > 0 (default: 0)
+	ConnPoolType     NewPoolFunc   // The function used to create the connection pool for the session (default: NewSimplePool)
+	DiscoverHosts    bool          // If set, gocql will attempt to automatically discover other members of the Cassandra cluster (default: false)
+	MaxPreparedStmts int           // Sets the maximum cache size for prepared statements globally for gocql (default: 1000)
 }
 
 // NewCluster generates a new config for the default cluster implementation.
 func NewCluster(hosts ...string) *ClusterConfig {
 	cfg := &ClusterConfig{
-		Hosts:         hosts,
-		CQLVersion:    "3.0.0",
-		ProtoVersion:  2,
-		Timeout:       600 * time.Millisecond,
-		DefaultPort:   9042,
-		NumConns:      2,
-		NumStreams:    128,
-		Consistency:   Quorum,
-		ConnPoolType:  NewSimplePool,
-		DiscoverHosts: false,
+		Hosts:            hosts,
+		CQLVersion:       "3.0.0",
+		ProtoVersion:     2,
+		Timeout:          600 * time.Millisecond,
+		DefaultPort:      9042,
+		NumConns:         2,
+		NumStreams:       128,
+		Consistency:      Quorum,
+		ConnPoolType:     NewSimplePool,
+		DiscoverHosts:    false,
+		MaxPreparedStmts: 1000,
 	}
 	return cfg
 }
@@ -58,6 +60,14 @@ func (cfg *ClusterConfig) CreateSession() (*Session, error) {
 	}
 	pool := cfg.ConnPoolType(cfg)
 
+	//Adjust the size of the prepared statements cache to match the latest configuration
+	stmtsLRU.mu.Lock()
+	for stmtsLRU.lru.Len() > cfg.MaxPreparedStmts {
+		stmtsLRU.lru.RemoveOldest()
+	}
+	stmtsLRU.lru.MaxEntries = cfg.MaxPreparedStmts
+	stmtsLRU.mu.Unlock()
+
 	//See if there are any connections in the pool
 	if pool.Size() > 0 {
 		s := NewSession(pool, *cfg)