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

@sourceloop/cache

v2.0.0

Published

This package provides a mixin which works with redis to cache GET request responses

Downloads

360

Readme

@sourceloop/cache

Overview

The @sourceloop/cache package offers a flexible solution for implementing caching in LoopBack. It introduces a mixin for LoopBack's DefaultCrudRepository and SequelizeCrudRepository, allowing you to configure caching behavior. This mixin extends and overrides the find, findById, and findOne methods of the target repository. By leveraging Redis as the caching datasource, it enables the caching of GET request responses.

Philosophy

Caching can prove to be quite beneficial in improving application efficency and performance by storing a subset of data in high speed data storage layer. Some benefits that cache can provide:

  • It allows you to efficiently reuse previously retrieved or computed data.
  • It reduces latency.
  • It helps to avoids network congestion.
  • In the event of outages, it can save the day by serving end users with the cached content.

However, cache can't be used anywhere. You must consider the following:

  • It is only beneficial in case same data is frequently requested (i.e. cache hit rate should be high). If this is not the case then caching will prove to be an overhead for your application
  • In case of caching editable data, you must be prepared to receive stale data from cache. However, you can configure the ttl according to whatever is acceptable for your application.

Installation

npm i @sourceloop/cache

Note: This package has loopback-connector-kv-redis as a peer dependency and only works with Redis for now.

Usage

Component Binding

Configure and bind CachePluginComponent to the application constructor as shown below.

import {
  CachePluginComponent,
  CachePluginComponentOptions,
  DEFAULT_CACHE_PLUGIN_OPTIONS,
  CachePluginComponentBindings,
} from '@sourceloop/cache';
// ...
export class MyApplication extends BootMixin(
  ServiceMixin(RepositoryMixin(RestApplication)),
) {
  constructor(options: ApplicationConfig = {}) {
    // ...
    const cacheOptions: CachePluginComponentOptions = {
      cacheProvider: CacheStrategyTypes.Redis,
      prefix: process.env.CACHE_PREFIX ?? DEFAULT_CACHE_PLUGIN_OPTIONS.prefix,
      ttl: 3000,
    };
    this.configure(CachePluginComponentBindings.COMPONENT).to(cacheOptions);
    this.component(CachePluginComponent);
  }
}

As shown above, you can configure the cache properties at a Global level. You can also provide these properties at the repository level. Options passed at the repository level will override the global options.

Configuration Options

The following options can be passed:

cacheProvider specifies the cache provider to be used. CacheStrategyTypes.Redis indicates that Redis is the chosen cache provider.

NOTE : The caching is implemented as a key value pair. The key is in the form of {{prefix}}_{{id}}_{{filter}}. The format of the key plays a very important role in the cache hit ratio. For example: Consider a situation in which you are sending user id in filter with every request. This will generate a new key for every user. So even if same data is returned to all users, caching will not be beneficial until and unless the same user makes the same request multiple times.

Apply Mixin to Repository

To use the caching functionality, you need to apply CacheRespositoryMixin provided in CacheManager class exported by this package to your repository.

Alongside You must inject the getter of the cache datasource with variable name getCacheDataSource in your repository's constructor, like below:

export class ProductRepository extends CacheManager.CacheRepositoryMixin<
  Product,
  typeof Product.prototype.id,
  ProductRelations,
  Constructor<
    DefaultCrudRepository<
      Product,
      typeof Product.prototype.id,
      ProductRelations
    >
  >
>(DefaultCrudRepository, {
  prefix: 'product',
  ttl: 50000,
}) {
  redisDataSource: RedisDataSource;
  constructor(
    @inject('datasources.expt') dataSource: ExptDataSource,
    @inject.getter('datasources.CacheDB')
    public getCacheDataSource: Getter<RedisDataSource>,
  ) {
    super(Product, dataSource);
  }
}

Using It With Sequelize

CacheRepositoryMixin starting v0.5.0 supports the SequelizeCrudRepository.

Checkout @loopback/sequelize for more information on how to use sequelize in your project.

Force Update

To forcefully update entries in cache, you can set forceUpdate to true in options parameter while invoking find, findById or findOne. Forcefully update will always return data from the original source and update the cache with the new data.

this.productRepository.findById(3, {}, {forceUpdate: true});

On updating forcefully the ttl option gets reset.

Skip Cache

There are situations where disabling the cache becomes necessary, such as in a test environment or when bypassing it for any specific reason. In such cases, you can set the environment variable SKIP_CACHE to true to skip the cache functionality altogether.

Clear Cache

To delete all cached entries for a repository, you can use the clearCache function on the repository, like below:

this.productRepository.clearCache();

It uses the prefix to find matching entries to delete.

License

Sourceloop is MIT licensed.