coocood/freecache

EntryCount didn't update?

Closed this issue · 8 comments

The project is awesome, but i met something just like this:

When i get the cache.EntryCount(), it always be 3, Why?
The max expireSeconds is 20, why it always be 3 after 1min?

image

The code.

package main

import (
	"fmt"
	"time"

	"github.com/coocood/freecache"
)

var cache = freecache.NewCache(1024 * 1024)

func init() {
	cache.Set([]byte("key1"), []byte("value1"), 3)
	cache.Set([]byte("key2"), []byte("value2"), 10)
	cache.Set([]byte("key3"), []byte("value3"), 20)
}

func main() {
	go func() {
		for {
			fmt.Println(cache.EntryCount())

			time.Sleep(3 * time.Second)
		}
	}()
	for {
		time.Sleep(1 * time.Second)
	}
}

Thanks your reply~

It's lazily removed, it doesn't actively remove expired entries, only on access or new entry insert.

It's lazily removed, it doesn't actively remove expired entries, only on access or new entry insert.

According to what you mean, I changed to the code below, but found that EntryCount ended up being 5, why not 3, is this what you expected?

func init() {
	cache.Set([]byte("key1"), []byte("value1"), 1)
	cache.Set([]byte("key2"), []byte("value2"), 2)
	cache.Set([]byte("key3"), []byte("value3"), 20)
	fmt.Println(cache.EntryCount()) // 3
}

func main() {
	time.Sleep(3 * time.Second)
	cache.Set([]byte("key4"), []byte("value4"), 30)
	fmt.Println(cache.EntryCount()) // 4

	time.Sleep(25 * time.Second)
	cache.Set([]byte("key5"), []byte("value5"), 30)
	fmt.Println(cache.EntryCount()) // 5
}

Thanks you reply~
@coocood

I mean only when the cache is full.

If you Get "key1", "key2", "key3" after sleep, then the entry count would be updated.

Does it mean that after expiration, these expired contents will still occupy memory? Will it not be deleted until the memory is full?

The memory is allocated on cache create and never grow or shrink.

Okay, and then questions:

  1. Can I understand that when the memory is full, the expired content is deleted?
  2. If it don't actively delete expired content, will that affect the speed of the next get?
  3. Is there any way to proactively delete expired content so that I don't have to apply for too much memory in the first place

Thx~

The memory usage is constant, adding or removing entries doesn't change the memory usage.
You don't need to actively expire content, it doesn't have any benefit.

OK, thx~