Make sure to copy cache keys

This commit is contained in:
Derek Collison
2012-11-13 19:09:28 -08:00
parent 990de9556b
commit e3ef2fea29
2 changed files with 9 additions and 8 deletions

View File

@@ -163,10 +163,9 @@ func (s *Sublist) removeFromCache(subject []byte, sub interface{}) {
if !matchLiteral(k, subject) {
continue
}
r := s.cache.Get(k)
if r == nil {
continue
}
// FIXME, right now just remove all matching cache
// entries. Should be smarter and walk small result
// lists and delete
s.cache.Remove(k)
}
}
@@ -209,7 +208,10 @@ func (s *Sublist) Match(subject []byte) []interface{} {
if int(s.cache.Count()) >= s.cmax {
s.cache.RemoveRandom()
}
s.cache.Set(subject, results)
// Make sure we copy the subject key here
scopy := make([]byte, len(subject))
copy(scopy, subject)
s.cache.Set(scopy, results)
s.mu.Unlock()
return results

View File

@@ -274,10 +274,9 @@ func TestStats(t *testing.T) {
if stats.MaxFanout != 3 {
t.Fatalf("Wrong stats for MaxFanout: %d vs %d\n", stats.MaxFanout, 3)
}
if stats.AvgFanout != 2.5 {
t.Fatalf("Wrong stats for MaxFanout: %d vs %d\n", stats.AvgFanout, 2.5)
if stats.AvgFanout != 3.0 {
t.Fatalf("Wrong stats for AvgFanout: %g vs %g\n", stats.AvgFanout, 3.0)
}
s.ResetStats()
stats = s.Stats()
if time.Since(stats.StatsTime) > 50*time.Millisecond {