Leverege Platform

Leverege Platform

  • Overview
  • JSDocs
  • REST API
  • Help

›Utilities

Platform Overview

  • Overview
  • Architecture
  • Core Concepts

Guides

  • Platform Setup
  • Building a Project
  • API UI

Services

  • API Server
  • Message Processor
  • Transponder
  • Reason
  • Scheduler
  • Messenger
  • Emailer
  • Rest Server
  • DB Curator

API

  • API
  • Interface
  • Project
  • System
  • Device
  • Blueprint
  • Collection
  • Child
  • History
  • Scenario
  • Script
  • Template
  • Timer
  • User
  • Users

Server Libraries

  • Log
  • Messages
  • Message Queue
  • Cache
  • Lock
  • Limit
  • Cluster Manager
  • Error
  • Exit

Utilities

  • Promise
  • Data Store
  • Factory
  • Path
  • Reasoner
  • Object Util
  • Array Util
  • String Util
  • Base62 Util
  • Value Cache
  • Validator
  • Lol

UI

  • API Redux
  • UI Builder
  • UI Elements
  • UI Redux
  • UI Mapbox Elements
  • UI Active Theme
  • UI Color Elements
  • UI Linear View Elements

Value Cache

The value cache is designed to simplify the storing of calculated values based on other values. This can be useful in React applications where you want to use the same properties object if the data didn't change.

Install

npm install --save @leverege/value-cache

Usage

There are two potential usages. A version where the cache can contain only one computed value, and a version that uses a key to handle many computed values.

One Value - ValueCache

const { ValueCache } = require( '@leverege/value-cache' )

class MyClass {
  constructor() {
    this.cache = new ValueCache()
  }

  doStuff( a, b, c ) {
    if ( this.cache.isSame( a, b, c ) ) {
      return this.cache.value
    }
    let v = // compute v based on a, b, c
    return this.cache.set( v, a, b, c )
  }
}

Another way to use the cache is by creating a function to compute the value and calling calc( fn, args ).

function calculate( a, b, c ) {
  let v = ... // compute v based on a, b, c
  return v
}
return this.cache.calc( calculate, a, b, c )

Many Values - ValuesCache

The ValuesCache stores ValueCache objects at keys so you don't have to make many ValueCache Object. It has all the same methods as the ValueCache, but the first argument is a key that represents the value you're caching.

On difference to note - to get the value you all value( key ) instead of accessing .value off the ValueCache.

const { ValueCache } = require( '@leverege/value-cache' )

class MyClass {
  constructor() {
    this.cache = new ValuesCache()
  }

  doStuff( a, b, c ) {
    if ( this.cache.isSame( 'myThing', a, b, c ) ) {
      return this.cache.value( 'myThing' )
    }
    let v = // compute v based on a, b, c
    return this.cache.set( 'myThing', v, a, b, c )
  }
}

The calc() methods works as well, but requires the key

function calculate( a, b, c ) {
  let v = ... // compute v based on a, b, c
  return v
}

return this.cache.calc( 'myThing', calculate, a, b, c )
← Base62 UtilValidator →
  • Install
  • Usage
    • One Value - ValueCache
  • Many Values - ValuesCache
Leverege Platform
Docs
Overview
Connect
FacebookLinkedInTwitter
Facebook Open Source