Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cachettl configuration option for not refreshing ttl #643

Merged
merged 2 commits into from
Sep 18, 2024

Conversation

atzoum
Copy link
Collaborator

@atzoum atzoum commented Sep 17, 2024

Description

There are use cases where refreshing an item's ttl after accessing it isn't appropriate. Thus, it is now possible to disable ttl refresh functionality both in cachettl and resourcettl

Security

  • The code changed/added as part of this pull request won't create any security issues with how the software is being used.

@debanjan97
Copy link
Member

we can add a test to verify the new behaviour of resourcettl as well (might not be needed since we added one for cachettl, feel free to ignore)

	t.Run("should expire despite accessed when refreshTTL is set to false", func(t *testing.T) {
		ttl = 10 * time.Second
		producer := &MockProducer{}
		c := resourcettl.NewCache[string, *cleanuper](ttl, false)

		r1, checkin1, err1 := c.Checkout(key, producer.NewCleanuper)
		require.NoError(t, err1, "it should be able to create a new resource")
		require.NotNil(t, r1, "it should return a resource")
		require.EqualValues(t, 1, producer.instances.Load(), "it should create a new resource")

		time.Sleep(ttl / 2) // wait for some time less than ttl]
		checkin1()

		r2, checkin2, err2 := c.Checkout(key, producer.NewCleanuper)
		require.NoError(t, err2, "it should be able to checkout the same resource")
		require.NotNil(t, r2, "it should return a resource")
		require.EqualValues(t, 1, producer.instances.Load(), "it shouldn't create a new resource")
		require.Equal(t, r1.id, r2.id, "it should return the same resource")

		time.Sleep(ttl/2 + time.Millisecond) // wait for expiration
		checkin2()

		r3, checkin3, err3 := c.Checkout(key, producer.NewCleanuper)
		require.NoError(t, err3, "it should be able to create a new resource")
		require.NotNil(t, r3, "it should return a resource")
		require.EqualValues(t, 2, producer.instances.Load(), "it should create a new resource since the previous one expired")
		require.NotEqual(t, r1.id, r3.id, "it should return a different resource")
		time.Sleep(time.Millisecond)
		checkin3()
	})

@atzoum atzoum merged commit 1961d98 into main Sep 18, 2024
14 checks passed
@atzoum atzoum deleted the chore.cachettlNoRefresh branch September 18, 2024 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants