cached-remote

1.0.3 • Public • Published

Remote API model, cached using Redis

The library is driven by needs of query remote API, but caching it locally. It

How to install

$ npm i -S cached-remote

Usage

import CachedRemote from 'cached-remote'
import redis from 'redis'

const modelName = 'clubs'
const host = `http://some-remote-api.io`
const basePath = '/api'
// Or use global client/pool
const cacheClient = redis.createClient()

class Clubs extends CachedRemote {
  constructor () {
    super({ modelName, host, basePath, cacheClient })
  }

  // Custom endpoints (non REST)
  findByHostname (hostname) {
    const cacheIdentifier = `hostname:${hostname}`

    return this.readFromCache(cacheIdentifier)
      .then(cachedData => {
        if (cachedData) { return cachedData }

        const query = { hostname }
        const extraPath = '/by-hostname'

        return this.searchFromRemote({ query, extraPath })
          .then(remoteData => {
            this.writeInCache(cacheIdentifier, remoteData)

            return remoteData
          })
      })
  }
}

export default new Clubs()

Then in your code you can use it:

import Clubs from 'models'

Clubs.find(5).then(club => console.log(club))
Clubs.findByHostname('mylovelyhostname.com').then(club => console.log(club))

Development instructions

  1. Create branch using:
$ git checkout -b feature-branch
  1. Run the linter and tests (tests are requiring Redis server running on port 6379) using:
$ npm run lint
$ npm run tests
  1. Create PR after you are finished.

  2. Then publish it to NPM registry using:

$ npm run release

Readme

Keywords

none

Package Sidebar

Install

npm i cached-remote

Weekly Downloads

1

Version

1.0.3

License

ISC

Unpacked Size

7.68 kB

Total Files

3

Last publish

Collaborators

  • zdraganov