Evict

  • evict

evict(obj)

Evicts an immutable item from the cache. Removes any references of the item existing inside other items.

Parameters

  • obj Object | string either the object's unique id or the object itself.

Returns:

  • boolean, true if the item was evicted, false otherwise.

Example

let item = {uid:1}
One.put(item)
One.get(1) === undefined // false

One.evict(1)
One.get(1) === undefined // true

Something a little more interesting

let item = {uid:1}
let item2 = {
  uid:2, 
  item: item1
}

One.put(item2)
One.get(1) === undefined // false (item1 was put by reference)

One.evict(1)
let cached = One.get(2);
console.log(JSON.stringify(cached)) // {uid:2} item1 is removed by eviction

It works with arrays

One.put([{uid:1}, {uid:2}]};
One.get(1) === undefined // false
One.get(2) === undefined // false

One.evict([1, 2]);
One.get(1) === undefined // true
One.get(2) === undefined // true

Putting an item after removing one of its references also evicts if the reference is the last item in the cache

let item1 = {uid:1}
let item2 = {uid:2, val:item1}
One.put(item2); // cache contains both item1, item2

let editable = One.getEditable(2)
editable.val = undefined
One.put(editable)

One.get(1) === undefined // true since item1 wasn't referenced 
// by any other entity - the cache de-references it for garbage collection

Or

let item = {uid:1}
let item2 = {uid:2, children:[item]}
One.put(item2);

let editable = One.get(2);
editable.children === [];
One.put(editable);

One.get(1) === undefined // true - no one else references item1

But not if another entity still references it

let item1 = {uid:1}
let item2 = {uid:2, val:item}
let item3 = {uid:3, val:item}
One.put([item1, item2]);

let editable = One.getEditable(2);
item2.val = undefined;
One.put(item2);

One.get(1) === undefined // false item1 is still in the cache because of item3 referencing it

Last updated