4.0.0 • Published 8 days ago

@sourceloop/cache v4.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 days ago

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);
  }
  /// ...
3.0.10

3 months ago

3.0.8

6 months ago

3.0.7

7 months ago

4.0.0

8 days ago

3.0.9

5 months ago

3.0.6

8 months ago

3.0.5

8 months ago

3.0.4

9 months ago

3.0.3

9 months ago

3.0.2

9 months ago

2.0.2

11 months ago

2.0.1

1 year ago

3.0.1

10 months ago

2.1.0

10 months ago

2.0.0

1 year ago

0.8.1

1 year ago

0.8.0

1 year ago

0.7.10

1 year ago

0.7.9

1 year ago

0.7.8

1 year ago

0.7.7

1 year ago

0.7.6

1 year ago

0.7.5

1 year ago

0.7.4

1 year ago

0.7.3

1 year ago

0.7.2

2 years ago

0.7.1

2 years ago

0.6.6

2 years ago

0.6.3

2 years ago

0.6.2

2 years ago

0.6.5

2 years ago

0.6.4

2 years ago

0.7.0

2 years ago

0.6.1

2 years ago

0.4.9

2 years ago

0.4.10

2 years ago

0.5.0

2 years ago

0.6.0

2 years ago

0.5.1

2 years ago

0.4.8

2 years ago

0.4.7

2 years ago

0.4.6

2 years ago

0.4.5

2 years ago

0.4.4

2 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.3.8

3 years ago

0.4.1

3 years ago

0.3.0

3 years ago

0.3.6

3 years ago

0.3.5

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.1.0

3 years ago