forked from scylladb/gocql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
125 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package gocql | ||
|
||
import ( | ||
"github.com/gocql/gocql/internal/lru" | ||
"sync" | ||
) | ||
|
||
const defaultMaxPreparedStmts = 1000 | ||
|
||
// preparedLRU is the prepared statement cache | ||
type preparedLRU struct { | ||
mu sync.Mutex | ||
lru *lru.Cache | ||
} | ||
|
||
// Max adjusts the maximum size of the cache and cleans up the oldest records if | ||
// the new max is lower than the previous value. Not concurrency safe. | ||
func (p *preparedLRU) max(max int) { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
|
||
for p.lru.Len() > max { | ||
p.lru.RemoveOldest() | ||
} | ||
p.lru.MaxEntries = max | ||
} | ||
|
||
func (p *preparedLRU) clear() { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
|
||
for p.lru.Len() > 0 { | ||
p.lru.RemoveOldest() | ||
} | ||
} | ||
|
||
func (p *preparedLRU) add(key string, val *inflightPrepare) { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
p.lru.Add(key, val) | ||
} | ||
|
||
func (p *preparedLRU) remove(key string) bool { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
return p.lru.Remove(key) | ||
} | ||
|
||
func (p *preparedLRU) execIfMissing(key string, fn func(lru *lru.Cache) *inflightPrepare) (*inflightPrepare, bool) { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
|
||
val, ok := p.lru.Get(key) | ||
if ok { | ||
return val.(*inflightPrepare), true | ||
} | ||
|
||
return fn(p.lru), false | ||
} | ||
|
||
func (p *preparedLRU) keyFor(addr, keyspace, statement string) string { | ||
// TODO: maybe use []byte for keys? | ||
return addr + keyspace + statement | ||
} |