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

Export Touch function #40

Closed
DerkSchooltink opened this issue Mar 15, 2021 · 3 comments
Closed

Export Touch function #40

DerkSchooltink opened this issue Mar 15, 2021 · 3 comments

Comments

@DerkSchooltink
Copy link

By using ttlcache.Cache#SkipTTLExtensionOnHit(bool) you can disable touching the cache entry when using Get. This offers an additional level over control TTL behaviour.

To offer even more control of this mechanism, I would like to propose to export a touch function as well:

func (cache *Cache) Touch(key string) error {
	cache.mutex.Lock()
	item, exists := cache.items[key]
	if !exists {
		return ErrNotFound
	}
	
	item.touch()
	cache.mutex.Unlock()
	return nil
}

Or something similar 😄

This is a lot more convenient that manually getting and setting the item with a new TTL.

@ReneKroon
Copy link
Contributor

Hi Derk,

I understand this can be useful, but can you add your usecase? How would you use this functionality? I designed this expecting that you want the data with the key in order to do something useful. So if you explain how you would use it, it's probably an easy change.

Greetings,

Rene

@DerkSchooltink
Copy link
Author

The idea is that when I want to add something to the cache, but the element with that key is already present, I instead just want to reset the TTL.

I would say this is a valid use-case opposed to just using Set(key, value) error in scenarios where you're not interested in updating the value for a certain key (static key-pair values), and treat it more as an append-only cache.

@ReneKroon if you have tips regarding my use-case I would love to hear them! This seemed like the most pragmatic approach for it, let me know if you agree. 😄

@ReneKroon
Copy link
Contributor

i added the code on master, can you verify that it works for you?

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

No branches or pull requests

2 participants