jmdobry / angular-cache

angular-cache is a very useful replacement for the Angular 1 $cacheFactory.
http://jmdobry.github.io/angular-cache
MIT License
1.39k stars 156 forks source link

Add feature to 'touch' elements in the cache #103

Closed sthomp closed 10 years ago

sthomp commented 10 years ago

This is mainly for TTL values. It would be nice to be able to 'touch' elements in the cache. The expected behavior would be that the timer for the given element would reset.

jmdobry commented 10 years ago

@sthomp I see what you're saying. Would the following suit your needs?

var itemToTouch = cache.get(someKey);
if (itemToTouch) {
  cache.put(someKey, itemToTouch);
}

or would you be better served with a AngularCache#touch(key) method being added to the API?

sthomp commented 10 years ago

Yep that's similar to what Im currently doing:

if(itemToTouch){ cache.remove(someKey); cache.put(someKey, itemToTouch); }

I wasn't clear if I needed the remove().

I've written a small function called getAndTouch() that will return the item and touch it if it exists. Works fine for me but not sure if thats something you want to build into your core API.