Skip to content

ReferenceCache

JonathanMontane edited this page May 9, 2016 · 3 revisions

A ReferenceCache component acts as a caching middleware that contains possible resolutions of a Reference. At its core, a ReferenceCache is a Map of References that are derived from the initial cached reference.

Import

/* if in src/ */
import { ReferenceCache } from './models/Reference'

Interface

ReferenceCache extends Immutable.Record using the following pattern:

class ReferenceCache extends Immutable.Record({
    cached: null,
    resolved: new Immutable.OrderedMap(),
    final: null
})

Fields

ReferenceCache.cached
  • ReferenceCache.cached expects a Reference. This Reference is also stored in ReferenceCache.resolved.initial
ReferenceCache.resolved
  • ReferenceCache.resolved expects an Immutable.OrderedMap that contains all the resolved depths. This Reference is also virtually stored in ReferenceCache.resolved[-1].
ReferenceCache.final
  • ReferenceCache.final expects either a Number or null depending on whether the full resolution of the ReferenceCache.cached was achieved. It represents the depth at which the resolution is final.

Methods

ReferenceCache.resolve
  • ReferenceCache.resolve(references, depth = 0) returns a new ReferenceCache also containing the new depth, resolution pair. If an equivalent depth already exists in this ReferenceCache, it will be replaced. If the resolution at depth is equal to the resolution at depth - 1, the ReferenceCache.final is set to depth - 1

Example

Please note that this example is abstract, and is there purely to represent the different interactions with ReferenceCache. The data may be non-sensical.

import references from './samples/references-examples'
import referenceContainers from './samples/referenceContainers-examples'

import { ReferenceCache } from './models/Reference'

let cache = new ReferenceCache({
    cached: references[0],
    resolved: new Immutable.OrderedMap({
        -1: references[0]
    }),
    final: -1
})

cache = cache
    .set('cached', references[1])
    .set('resolved', new Immutable.OrderedMap({
        -1: references[1],
         0: references[2]    // Note: for your own sanity, avoid doing this
    }))
    .set('final', 0)

cache = cache
    .resolve(referenceContainers[0], 3)
    .resolve(referenceContainers[1], 5)    // Note: will use the cached data from the first resolve.
Clone this wiki locally