Skip to content

Caching

Chau Nguyen edited this page Aug 8, 2017 · 24 revisions

Right now, the library supports in-memory (JS) caching as well as caching with Redis (see end). These are the default timers that made sense to me.

const endpointCacheTimers = {
  // defaults
  CHAMPION: cacheTimers.MONTH,
  CHAMPION_MASTERY: cacheTimers.SIX_HOURS,
  CURRENT_GAME: cacheTimers.NONE,
  FEATURED_GAMES: cacheTimers.NONE,
  LEAGUE: cacheTimers.SIX_HOURS,
  STATIC: cacheTimers.MONTH,
  STATUS: cacheTimers.NONE,
  MATCH: cacheTimers.MONTH,
  MATCHLIST: cacheTimers.HOUR,
  /*
    Match and Matchlist uses the same endpoints in the v3 API,
    but should still be cached much differently and so I still provide this option.
  */
  RUNES_MASTERIES: cacheTimers.WEEK,
  SUMMONER: cacheTimers.DAY,
  TOURNAMENT_STUB: cacheTimers.HOUR, // TODO: ??
  TOURNAMENT: cacheTimers.HOUR // TODO: ??
}

If you pass in a 'cache' (InMemoryCache, RedisCache, or your own impl), but not how long you want each type of request to be cached (cacheTTL object), then by default you'll use the above timers.

To pass in your own custom timers, initialize Kindred like this:

import TIME_CONSTANTS from KindredAPI.TIME_CONSTANTS // for convenience, has a bunch of set timers in seconds

var k = new KindredAPI.Kindred({
  key: RIOT_API_KEY,
  defaultRegion: REGIONS.NORTH_AMERICA,
  debug: true, // you can see if you're retrieving from cache with lack of requests showing
  showKey: true, // puts key in the debug urls so that you can easily open the urls in your browser
  limits: [ [10, 10], [500, 600] ],
  cache: new KindredAPI.InMemoryCache(),
  // deprecated 2.0.61 cacheOptions: CACHE_TYPES[0], // in-memory 
  cacheTTL: {
    // All values in SECONDS.
    CHAMPION: whatever,
    CHAMPION_MASTERY: whatever,
    CURRENT_GAME: whatever,
    FEATURED_GAMES: whatever,
    GAME: whatever,
    LEAGUE: whatever,
    STATIC: TIME_CONSTANTS.MONTH,
    STATUS: whatever,
    MATCH: whatever,
    MATCHLIST: whatever,
    RUNES_MASTERIES: whatever,
    STATS: whatever,
    SUMMONER: TIME_CONSTANTS.DAY
  }
})

You don't have to pass in every single cache timer for it to work.

eg:

cacheTTL: { SUMMONER: 1000 } // valid

To test basic caching, run the below code:

// initialize Kindred QuickStart
k.Summoner.by.name('Contractz')
 .then(data => k.Summoner.by.name('Contractz'))
 .catch(error => console.error(error))

Redis Cache Example

There are only 3 options that are set by default at the moment: host, port, and keyPrefix. If you initialize the RedisCache without options, the default redis options will be set and the key prefix will be "kindredAPI-".

https://github.com/ChauTNguyen/kindred-api/issues/22

Here is an example for initializing with different options.

const nonDefaultRedis = new RedisCache({
  host: '192.168.0.1',
  port: '5005',
  keyPrefix: 'helloWorld-',
})

const productionWithNonDefaultRedis = new Kindred({
  key: 'myProductionRitoAPIKey',
  limits: [[1500, 10], [90000, 600]],
  spread: true,
  timeout: 5000,
  cache: nonDefaultRedis,
})