nestjs-iacry

Identity and Access Management (IAM) module for NestJS, inspired by AWS IAM

View the Project on GitHub AlexanderC/nestjs-iacry

Caching

nestjs-iacry supports optional caching to reduce database reads for frequently checked policies.

ioredis

npm install --save ioredis
import { IACryModule, SEQUELIZE_STORAGE, IOREDIS_CACHE } from 'nestjs-iacry';
import Redis from 'ioredis';

IACryModule.forRoot({
  storage: SEQUELIZE_STORAGE,
  storageRepository: PolicyStorageModel,
  cache: IOREDIS_CACHE,
  cacheClient: new Redis('redis://localhost:6379'),
  cacheOptions: {
    expire: 600,                  // 10 minutes (default: 3600 = 1 hour)
    prefix: 'MY_APP_POLICIES/',   // key prefix (default: 'IACRY_PCACHE/')
  },
});

Cache Behavior

CachedStorageOptions

Option Type Default Description
expire number 3600 Cache TTL in seconds
prefix string 'IACRY_PCACHE/' Key prefix for all cache entries

Custom Cache Adapter

Implement the Cache interface:

import { Cache } from 'nestjs-iacry';

export class MyCache implements Cache {
  async set(key: string, value: string, expire?: number): Promise<boolean> {
    // Store value, optionally with TTL in seconds
    return true;
  }

  async has(key: string): Promise<boolean> {
    // Check if key exists
  }

  async get(key: string): Promise<string> {
    // Retrieve value
  }

  async remove(key: string): Promise<boolean> {
    // Delete key
    return true;
  }
}

Then pass it directly:

IACryModule.forRoot({
  storage: SEQUELIZE_STORAGE,
  storageRepository: PolicyStorageModel,
  cache: new MyCache(),
});

Next: Advanced