@sourceloop/cache
TypeScript icon, indicating that this package has built-in type declarations

4.0.0 • Public • Published

caching-guard

A Caching component that provides helpers for caching in Loopback4 based microservices.

LoopBack

Installation

Install CachingComponent using npm;

$ [npm install | yarn add] @sourceloop/cache

Basic Usage

Configure and load CachingComponent in the application constructor as shown below.

import {CachingComponent} from '@sourceloop/cache';
// ...
export class MyApplication extends BootMixin(
  ServiceMixin(RepositoryMixin(RestApplication)),
) {
  constructor(options: ApplicationConfig = {}) {
    this.bind(CacheComponentBindings.CacheConfig).to({
      ttl: 1000,
      strategy: RedisStoreStrategy,
      datasourceName: 'redisCacheStore',
    });
    this.component(CachingComponent);
    // ...
  }
  // ...
}

In a repository

To add caching to a repository, just add it as a mixin to the base class -

export class TestWithMixinRepository extends CacheMixin(
  DefaultCrudRepository<Test, number, {}>,
) {
  cacheIdentifier = 'testRepo';
  constructor(@inject('datasources.memorydb') dataSource: juggler.DataSource) {
    super(Test, dataSource);
  }
}

In a controller or service

To add caching to a service or controller, just implement the ICachedService interface, adding a binding for the ICacheService and the applying the relevant decorators to the methods you want cached -

export class TestController implements ICachedService {
  constructor(
    @repository(TestWithoutCachingRepository)
    public testModelRepository: TestWithoutCachingRepository,
    @inject(CacheComponentBindings.CacheService)
    public cache: ICacheService,
  ) {}
  cacheIdentifier = 'testRepo';

  @cacheInvalidator()
  @post('/tests')
  @response(200, {
    description: 'Test model instance',
    content: {'application/json': {schema: getModelSchemaRef(Test)}},
  })
  async create(
    @requestBody({
      content: {
        'application/json': {
          schema: getModelSchemaRef(Test, {
            title: 'NewTest',
            exclude: ['id'],
          }),
        },
      },
    })
    testModel: Omit<Test, 'id'>,
  ): Promise<Test> {
    return this.testModelRepository.create(testModel);
  }
  // ...
  @cachedItem()
  @get('/tests/count')
  @response(200, {
    description: 'Test model count',
    content: {'application/json': {schema: CountSchema}},
  })
  async count(@param.where(Test) where?: Where<Test>): Promise<Count> {
    return this.testModelRepository.count(where);
  }
  /// ...

Package Sidebar

Install

npm i @sourceloop/cache

Weekly Downloads

93

Version

4.0.0

License

MIT

Unpacked Size

106 kB

Total Files

71

Last publish

Collaborators

  • samarpan_sf
  • npm-sourcefuse
  • dev-hitesh-gupta
  • akshatdubeysf
  • barleendhaliwalsf
  • ankurb1999
  • yeshasf
  • abir.ganguly