put

open fun put(key: T, item: Y): Y

Adds the given item to the cache with the given key and returns any previous entry for the given key that may have already been in the cache.

If the size of the item is larger than the total cache size, the item will not be added to the cache and instead onItemEvicted will be called synchronously with the given key and item.

The size of the item is determined by the getSize method. To avoid errors where getSize returns different values for the same object when called at different times, the size value is acquired in put and retained until the item is evicted, replaced or removed.

If item is null the behavior here is a little odd. For the most part it's similar to simply calling remove with the given key. The difference is that calling this method with a null item will result in an entry remaining in the cache with a null value and 0 size. The only real consequence is that at some point onItemEvicted may be called with the given key and a null value. Ideally we'd make calling this method with a null item identical to remove but we're preserving this odd behavior to match older versions :(.

Parameters

key

The key to add the item at.

item

The item to add.