npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

loopback-api-cache

v1.0.3

Published

A loopback component for cache support

Downloads

174

Readme

loopback-api-cache

A LoopBack 4 component for controller cache support.

Overview

The component lets you cache (using any cache mechanism Redis, Memcached, etc) the response of an endpoint using the @cache() decorator.

Installation

npm install --save loopback-api-cache

How to use it

Start by creating a Model for the cache. It should have id: string, data: any and ttl: number fields.

lb4 model
? Model class name: cache
? Please select the model base class (Use arrow keys)
❯ Entity (A persisted model with an ID) 
? Allow additional (free-form) properties? (y/N) No

? Enter the property name: id
? Property type: (Use arrow keys)
❯ string 
? Is id the ID property? (y/N) Yes
? Is it required?: Yes
? Default value [leave blank for none]: 

...

At the end you would have something like

// src/models/cache.model.ts
import { Entity, model, property } from '@loopback/repository';

@model()
export class Cache extends Entity {
  @property({
    type: 'string',
    id: true,
    required: true,
  })
  id: string;

  @property({
    type: 'any',
    required: true,
  })
  data: any;

  @property({
    type: 'number',
    required: true,
  })
  ttl: number;

  constructor(data?: Partial<Cache>) {
    super(data);
  }
}

Now create the cache datasource and repository of your choice. We are going to be using Redis for this example.

lb4 datasource
? Datasource name: cache
? Select the connector for cache: 
❯ Redis key-value connector (supported by StrongLoop) 
...
lb4 repository
❯ CacheDatasource 
? Select the model(s) you want to generate a repository 
❯◉ Cache
...

For more information on how to create datasources or repositories go to

https://loopback.io/doc/en/lb4/Command-line-interface.html https://loopback.io/doc/en/lb4/DataSources.html https://loopback.io/doc/en/lb4/Repositories.html

Decorate your controller methods with @cache(ttl) to be able to cache the response.

// src/controllers/user.controller.ts
import { repository } from '@loopback/repository';
import { get, param } from '@loopback/rest';
import { cache } from 'loopback-api-cache';
import { UserRepository } from '../repositories';

export class UserController {
  constructor(@repository(UserRepository) protected userRepository: UserRepository) {}

  // caching response for 60 seconds
  @cache(60)
  @get('/user/:id')
  user(@param.path.string('id') id: string): Promise<User> {
    return this.userRepository.findById(id);
  }
}

Next, implement a cache strategy provider.

// src/providers/cache-strategy.provider.ts
import { inject, Provider, ValueOrPromise } from '@loopback/core';
import { repository } from '@loopback/repository';
import { CacheBindings, CacheMetadata, CacheStrategy } from 'loopback-api-cache';
import { CacheRepository } from '../repositories';

export class CacheStrategyProvider implements Provider<CacheStrategy | undefined> {
  constructor(
    @inject(CacheBindings.METADATA)
    private metadata: CacheMetadata,
    @repository(CacheRepository) protected cacheRepo: CacheRepository
  ) {}

  value(): ValueOrPromise<CacheStrategy | undefined> {
    if (!this.metadata) {
      return undefined;
    }

    return {
      check: (path: string) =>
        this.cacheRepo.get(path).catch(err => {
          console.error(err);
          return undefined;
        }),
      set: async (path: string, result: any) => {
        const cache = new Cache({ id: result.id, data: result, ttl: this.metadata.ttl });
        this.cacheRepo.set(path, cache, { ttl: ttlInMs }).catch(err => {
          console.error(err);
        });
      },
    };
  }
}

In order to perform the check and set of our cache, we need to implement a custom Sequence invoking the corresponding methods at the right time during the request handling.

// src/sequence.ts
import { inject } from '@loopback/context';
import { FindRoute, InvokeMethod, ParseParams, Reject, RequestContext, RestBindings, Send, SequenceHandler } from '@loopback/rest';
import { CacheBindings, CacheCheckFn, CacheSetFn } from 'loopback-api-cache';

const SequenceActions = RestBindings.SequenceActions;

export class MySequence implements SequenceHandler {
  constructor(
    @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute,
    @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams,
    @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod,
    @inject(SequenceActions.SEND) public send: Send,
    @inject(SequenceActions.REJECT) public reject: Reject,
    @inject(CacheBindings.CACHE_CHECK_ACTION) protected checkCache: CacheCheckFn,
    @inject(CacheBindings.CACHE_SET_ACTION) protected setCache: CacheSetFn
  ) {}

  async handle(context: RequestContext) {
    try {
      const { request, response } = context;
      const route = this.findRoute(request);
      const args = await this.parseParams(request, route);

      // Important part added to check for cache and respond with that if found
      const cache = await this.checkCache(request);
      if (cache) {
        this.send(response, cache.data);
        return;
      }

      const result = await this.invoke(route, args);
      this.send(response, result);

      // Important part added to set cache with the result
      this.setCache(request, result);
    } catch (error) {
      this.reject(context, error);
    }
  }
}

Don't forget to inject CacheBindings.CACHE_CHECK_ACTION and CacheBindings.CACHE_SET_ACTION

+ @inject(CacheBindings.CACHE_CHECK_ACTION) protected checkCache: CacheCheckFn,
+ @inject(CacheBindings.CACHE_SET_ACTION) protected setCache: CacheSetFn,

Finally, put it all together in your application class:

// src/application.ts
import { BootMixin } from '@loopback/boot';
import { ApplicationConfig } from '@loopback/core';
import { RestApplication, RestBindings, RestServer } from '@loopback/rest';
import { CacheBindings, CacheComponent } from 'loopback-api-cache';
import { MySequence } from './sequence';

export class MyApp extends BootMixin(RestApplication) {
  constructor(options?: ApplicationConfig) {
    super(options);

    this.projectRoot = __dirname;

    // Add these two lines to your App
    this.component(CacheComponent);
    this.bind(CacheBindings.CACHE_STRATEGY).toProvider(CacheStrategyProvider);

    this.sequence(MySequence);
  }

  async start() {
    await super.start();

    const server = await this.getServer(RestServer);
    const port = await server.get(RestBindings.PORT);
    console.log(`REST server running on port: ${port}`);
  }
}

Contributors

See all contributors.