Skip to content

Spool: Caches for Fabrix to cache through many different store options

License

Notifications You must be signed in to change notification settings

fabrix-app/spool-caches

Folders and files

NameName
Last commit message
Last commit date

Latest commit

5e22016 Â· Dec 7, 2022

History

17 Commits
Aug 7, 2018
Aug 7, 2018
Feb 2, 2019
Feb 2, 2019
Jan 16, 2020
Aug 7, 2018
Aug 7, 2018
Aug 7, 2018
Jan 16, 2020
Dec 7, 2022
Jan 16, 2020

Repository files navigation

spool-caches

Gitter NPM version Build Status Test Coverage Dependency Status Follow @FabrixApp on Twitter

📦 Caches Spool

Implements cache-manager as a Fabrix spool

Install

$ npm install --save @fabrix/spool-caches

Configure

// config/main.ts
export const main = {
  spools: [
    // ... other spools
    require('@fabrix/spool-caches').CachesSpool
  ]
}

Configuration

const mongoStore = require('cache-manager-mongodb')
const redisStore = require('cache-manager-redis')
// config/caches.ts
export const caches = {
  stores: [
  // Example for redis Store
  {
    name: 'my-redis-store',
    store: redisStore,
    host: 'localhost',
    auth_pass: ''
    db: 0,
    ttl: 600 // Default TTL
  },
  // Example for memory store
  {
    name: 'memory-store',
    store: 'memory',
    max: 100,
    ttl: 60
  },
  // Example for mongo store
  {
    name: 'mongo-store',
    store: mongoStore,
    options: {
      host: 'localhost',
      port: '27017',
      username: 'username',
      password: 'password',
      database: 'mymondodb',
      collection: 'cacheManager',
      compression: false,
      poolSize: 5,
      auto_reconnect: true
      ttl: 60
    }
  }],

  defaults: ['memory-store']
}

For more information about store (type and configuration) please see the cache-manager documentation.

Usage

  const myDefaultCache = this.app.services.CacheService.getStore() // Return the first store into defaults config
  myDefaultCache.set('mystoreddata', 'testValue', {ttl: 10}).then(result => {
  return myDefaultCache.get('mystoreddata').then(result => {
        console.log(result)
        // >> 'testValue'
        return myDefaultCache.del('mystoreddata')
    })
  })

You can retrieve a specific store by name like this: const myMongoCache = this.app.services.CacheService.getStore('mongo-store')

You can retrieve a multi caching store like this (without parameters to get multi caching with defaults stores): const myMongoCache = this.app.services.CacheService.getMultiCachingStore(['memory-store', 'mongo-store'])