How to use lru-memoizer - 9 common examples

To help you get started, we’ve selected a few lru-memoizer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github auth0 / auth0-authorization-extension / server / lib / auth0.js View on Github external
import async from 'async';
import nconf from 'nconf';
import moment from 'moment';
import Promise from 'bluebird';
import request from 'request';
import memoizer from 'lru-memoizer';
import { getDb } from './storage/getdb';

// TODO: Move to static client.
export const getTokenCached = memoizer({
  load: (sub, callback) => {
    getDb().getToken(sub)
      .then((token) => {
        callback(null, token.accessToken);
      })
      .catch(callback);
  },
  hash: (sub) => sub,
  max: 100,
  maxAge: nconf.get('DATA_CACHE_MAX_AGE')
});

export const getToken = (sub, cb) => {
  const token = nconf.get('AUTH0_APIV2_TOKEN');
  if (token) {
    return cb(null, token);
github auth0-extensions / auth0-delegated-administration-extension / server / lib / scriptmanager.js View on Github external
constructor(storage, cacheAge = 1000 * 10) {
    if (storage === null || storage === undefined) {
      throw new ArgumentError('Must provide a storage object.');
    }

    this.log = logger.debug.bind(logger);
    this.cache = { };
    this.storage = storage;
    this.getCached = Promise.promisify(
      memoizer({
        load: (name, callback) => {
          this.get(name)
            .then((script) => {
              callback(null, script);
              return null;
            })
            .catch(callback);
        },
        hash: name => name,
        max: 100,
        maxAge: cacheAge
      })
    );
  }
github auth0 / auth0-authorization-extension / server / lib / queries.js View on Github external
load: (db, callback) => {
    db.getRoles()
      .then(roles => {
        callback(null, roles);
      })
      .catch(err => callback(err));
  },
  hash: (db) => db.hash || 'roles',
  max: 100,
  maxAge: nconf.get('DATA_CACHE_MAX_AGE')
});

/*
 * Cache groups.
 */
export const getGroupsCached = memoizer({
  load: (db, callback) => {
    db.getGroups()
      .then(groups => {
        callback(null, groups);
      })
      .catch(err => callback(err));
  },
  hash: (db) => db.hash || 'groups',
  max: 100,
  maxAge: nconf.get('DATA_CACHE_MAX_AGE')
});

/*
 * Get the full connection names for all mappings.
 */
export const getMappingsWithNames = (auth0, groupMappings) =>
github auth0-extensions / auth0-delegated-administration-extension / server / lib / managementApiClient.js View on Github external
import ms from 'ms';
import { ManagementClient } from 'auth0';
import Promise from 'bluebird';
import memoizer from 'lru-memoizer';
import request from 'request';

import logger from './logger';

const getAccessToken = Promise.promisify(
  memoizer({
    load: (domain, clientId, clientSecret, callback) => {
      logger.debug(`Requesting access token for ${clientId} - https://${domain}/api/v2/`);

      const options = {
        uri: `https://${domain}/oauth/token`,
        body: {
          audience: `https://${domain}/api/v2/`,
          grant_type: 'client_credentials',
          client_id: clientId,
          client_secret: clientSecret
        },
        json: true
      };

      request.post(options, (err, res, body) => {
        if (err) {
github auth0 / auth0-authorization-extension / server / lib / queries.js View on Github external
apiCall(auth0, auth0.connections.getAll, [ { fields: 'id,name,strategy' } ])
      .then(connections => _.chain(connections)
        .sortBy((conn) => conn.name.toLowerCase())
        .value())
      .then(connections => callback(null, connections))
      .catch(err => callback(err));
  },
  hash: (auth0) => auth0.hash || 'connections',
  max: 100,
  maxAge: nconf.get('DATA_CACHE_MAX_AGE')
});

/*
 * Cache permissions.
 */
export const getPermissionsCached = memoizer({
  load: (db, callback) => {
    db.getPermissions()
      .then(permissions => {
        callback(null, permissions);
      })
      .catch(err => callback(err));
  },
  hash: (db) => db.hash || 'permissions',
  max: 100,
  maxAge: nconf.get('DATA_CACHE_MAX_AGE')
});

/*
 * Cache roles.
 */
export const getRolesCached = memoizer({
github auth0 / auth0-authorization-extension / server / lib / queries.js View on Github external
load: (db, callback) => {
    db.getPermissions()
      .then(permissions => {
        callback(null, permissions);
      })
      .catch(err => callback(err));
  },
  hash: (db) => db.hash || 'permissions',
  max: 100,
  maxAge: nconf.get('DATA_CACHE_MAX_AGE')
});

/*
 * Cache roles.
 */
export const getRolesCached = memoizer({
  load: (db, callback) => {
    db.getRoles()
      .then(roles => {
        callback(null, roles);
      })
      .catch(err => callback(err));
  },
  hash: (db) => db.hash || 'roles',
  max: 100,
  maxAge: nconf.get('DATA_CACHE_MAX_AGE')
});

/*
 * Cache groups.
 */
export const getGroupsCached = memoizer({
github auth0 / auth0-authorization-extension / server / lib / queries.js View on Github external
import _ from 'lodash';
import nconf from 'nconf';
import Promise from 'bluebird';
import memoizer from 'lru-memoizer';
import apiCall from './apiCall';

const compact = (entity) => ({
  _id: entity._id,
  name: entity.name,
  description: entity.description
});

/*
 * Cache connections.
 */
export const getConnectionsCached = memoizer({
  load: (auth0, callback) => {
    apiCall(auth0, auth0.connections.getAll, [ { fields: 'id,name,strategy' } ])
      .then(connections => _.chain(connections)
        .sortBy((conn) => conn.name.toLowerCase())
        .value())
      .then(connections => callback(null, connections))
      .catch(err => callback(err));
  },
  hash: (auth0) => auth0.hash || 'connections',
  max: 100,
  maxAge: nconf.get('DATA_CACHE_MAX_AGE')
});

/*
 * Cache permissions.
 */
github auth0 / auth0-authorization-extension / server / lib / managementApiClient.js View on Github external
import ms from 'ms';
import Promise from 'bluebird';
import memoizer from 'lru-memoizer';
import request from 'request-promise';

import config from './config';
import logger from './logger';

let auth0 = require('auth0');
if (config('HOSTING_ENV') === 'webtask') {
  auth0 = require('auth0@2.0.0');
}

const getAccessToken = Promise.promisify(
  memoizer({
    load: (domain, clientId, clientSecret, callback) => {
      logger.debug(`Requesting access token for ${clientId} - https://${domain}/api/v2/`);

      const options = {
        uri: `https://${domain}/oauth/token`,
        body: {
          audience: `https://${domain}/api/v2/`,
          grant_type: 'client_credentials',
          client_id: clientId,
          client_secret: clientSecret
        },
        json: true
      };

      request.post(options)
        .then((data) => callback(null, data.access_token))
github auth0-extensions / auth0-sso-dashboard-extension / server / lib / queries.js View on Github external
request('POST', `https://${config('AUTH0_DOMAIN')}/oauth/token`)
      .send(body)
      .set('Content-Type', 'application/json')
      .end((err, res) => {
        if (err || !res.body || !res.body.access_token) {
          logger.error(res.body);
          return reject(err);
        }

        return resolve(res.body.access_token);
      });
  });

const getAuthorizationTokenCached = Promise.promisify(
  memoizer({
      load: (callback) => {
        getAuthorizationToken()
          .then(accessToken => callback(null, accessToken))
          .catch(err => callback(err));
      },
      hash: () => 'auth0-authz-apiToken',
      max: 100,
      maxAge: 60 * 60000
    }
  ));

export const getGroups = () =>
  new Promise((resolve, reject) => {
    getAuthorizationTokenCached()
      .then((token) => {
        if (!token) {

lru-memoizer

Memoize functions results using an lru-cache.

MIT
Latest version published 2 months ago

Package Health Score

80 / 100
Full package analysis

Popular lru-memoizer functions