freecache icon indicating copy to clipboard operation
freecache copied to clipboard

EntryCount() not accurate when value is updated?

Open fladz opened this issue 9 years ago • 2 comments
trafficstars

It seems EntryCount() doesn't show accurate number when a key's value is updated..? Just by a brief look at segment.set(), looks like in certain cases, segment.delEntryPtr() doesn't match so segment.entryCount won't get subtracted but then when segment.insertEntryPtr() is called next, it increments segment.entryCount.

A very simple example:

func main() {
  cache := freecache.NewCache(0)
  var err error
  var entries = 50

  // Add entries
  for i := 0; i < entries; i++ {
    if err = cache.Set([]byte("entry_" + strconv.Itoa(i)), []byte("something"), -1); err != nil {
      fmt.Printf("ERROR: %d - %s\n", i, err)
    }
  }
  fmt.Printf("%d entries in freecache\n", cache.EntryCount())

  // Modify entries
  var key, value []byte
  var newvalue = []byte("some new value")
  for i := 0; i < entries; i++ {
    key = []byte("entry_" + strconv.Itoa(i))
    if err = cache.Set(key, newvalue, -1); err != nil {
      fmt.Printf("ERROR: %d - %s\n", i, err)
      continue
    }
    if value, err = cache.Get(key); err != nil {
      fmt.Printf("ERROR: %d - %s\n", i, err)
      continue
    }
    if !bytes.Equal(value, newvalue) {
      fmt.Printf("ERROR: %d %v != %v\n", i, value, newvalue)
    }
  }
  fmt.Printf("%d entries in freecache\n", cache.EntryCount())
}

The first EntryCount() shows 50 (expected) but the 2nd one shows 54. The new value retrieved from cache.Get() seems accurate so I'm assuming this is just what EntryCount() returns is inaccurate. If I call cache.Del() before calling cache.Set() in the 2nd loop, the 2nd EntryCount() shows 50.

fladz avatar Nov 21 '16 21:11 fladz

Can you provide more information about your Go version and OS? I can't reproduce this issue on maxOS, Go 1.7.

Thank you.

coocood avatar Nov 24 '16 15:11 coocood

My environment is Go 1.7 on CentOS 6.7 and 6.8. I can also see the same mismatched number on Ubuntu 14.04.

fladz avatar Nov 28 '16 16:11 fladz