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

memoli

v1.1.0

Published

Memorize async/sync function result with cache

Downloads

15

Readme

memoli.js

Cache the results of async/sync functions. When you call the function with the same parameters, memoli will fetch the previous result for you. Saving time has never been easier..

memoli uses redis and node-cache(for in-memory) caching solutions in background. Which one do you prefer is your choose. memoli provides a interface for all options of redis and node-cache.

memoli is compatible with v4 and higher version of redis and v5 and higher version of node-cache

Also, memoli.cache provides cache API's like: get, set, del, .. Therefore, you can use caching functionality directly.

Installation

Install memoli with prefered cache solution:

npm i memoli redis
npm i memoli node-cache

Add below to compilerOptions in tsconfig.json for decorator usages (OPTIONAL):

"experimentalDecorators": true

Usage

First of all, initialize memoli:

const memoli = await Memoli.initialize({
  cacheSource: 'redis', // 'redis' or 'in-memory'
  // redisOptions: Includes Redis' own options
  redisOptions: {
    url: 'redis://<username><password>@<host>:<port>/<database>',
    ttl: 5, // 5 sec
  },
});

memoli have memolize function for wrapping the function calls and cache them.

Singular Functions

For example, you have following function declaration:

async function getHospitalFromHospitalService(id: number): Promise<Hospital> {
  return hospitalService.getHospital(id); // async function call
}

And calling this function like this:

const hospital = await getHospitalFromHospitalService(1);

if you wrap this function call with memolize function, you can get result from cache for next function calls:

const hospital = await memoli.memolize({
  fn: getHospitalFromHospitalService,
  args: [1],
});

memoli creates a cache key from your function name and given function parameters. Then, stores your function call result with this cache key. Thus, next function call with same parameters will be returned from cache.

Class Functions

if you are using class functions like this:

class HospitalServiceCaller {
  // ...
  async getHospitalFromHospitalService(id: number): Promise<Hospital> {
    return hospitalService.getHospital(id); // async function call
  }
  // ...
}

You need to give one more parameter called klass to memolize function:

const hospitalServiceCaller = new HospitalServiceCaller();

const hospital = await memoli.memolize({
  klass: hospitalServiceCaller,
  fn: hospitalServiceCaller.getHospitalFromHospitalService,
  args: [1],
});

Decorator Usage for Class Functions

memoli provides memolize decorator to make things easier:

class HospitalServiceCaller {
  // ...
  @memolize()
  async getHospitalFromHospitalService(id: number): Promise<Hospital> {
    return hospitalService.getHospital(id); // async function call
  }
  // ...
}

With memolize decorator, you can use function calls as you used before:

const hospital = await hospitalServiceCaller.getHospitalFromHospitalService(1);

Sync Functions

memoli supports sync function result caching as well.

Cache API

memoli provides usage of cache functionality directly with cache property:

const memoli = await Memoli.initialize({
  cacheSource: 'in-memory',
  inMemoryCacheOptions: {
    stdTTL: 30, // 30 sec
    checkperiod: 30, // each 30 sec
  },
});

const cache = memoli.cache!;

const result1 = await cache.set('key-1', { value: 'value-1' });
console.log(result1); // { value: 'value-1' }

const result2 = await cache.get('key-1');
console.log(result2); // { value: 'value-1' }

const result3 = await cache.del('key-1');
console.log(result3); // true

const result4 = await cache.get('key-1');
console.log(result4); // undefined

await memoli.quit();

Examples

To reach all provided code samples, visit examples

License

Copyright © 2024 Dedeloper.

This project is licensed under the MIT License - see the LICENSE file for details.