Skip to content

expirable-lru: cache.Get() not refreshing entry's TTL #186

@shellohunter

Description

@shellohunter

The following test case will fail. Seems there's no way to keep an entry alive.
IMO, Peek retrieves data without updating its TTL, but Get does. So if we keep Get its value before it expires, wee keep it alive. Am I correct?

func TestExpirableLRUKeepAlive(t *testing.T) {
	cache := expirable.NewLRU[string, string](5, nil, time.Millisecond*100)
	cache.Add("A", "Apple")

	start := time.Now()
	for range time.NewTicker(time.Millisecond * 10).C {
		elapsed := time.Since(start).Milliseconds()
		if v, ok := cache.Get("A"); !ok || v != "Apple" {
			t.Fatalf("entry expired after %dms", elapsed)
		}
		if elapsed > 500 {
			break
		}
		t.Logf("so far so good: %dms", elapsed)
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions