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

redisgoose

v1.1.3

Published

A redis cache for mongoose

Downloads

2

Readme

Redisgoose

Use redis and mongoose together as efficiently as possible

I created this package because cachegoose and recachegoose were giving me some trouble

Made for Mongoose v6 and up

Installation

npm i redisgoose

Example Usage

const redisgoose = require('redisgoose');
const mongoose = require('mongoose');

// Redis is installed locally in this case
redisgoose('redis://127.0.0.1:6379');

const Sample = mongoose.model('sample', new mongoose.Schema({ name: String }));

const main = async function () {
    await mongoose.connect('mongodb://127.0.0.1/test');
    console.log('Connected to mongodb');

    // Find a sample with the name test
    // .cache(lifetime, ?key), key should be autogenerated or predefined

    let q = { name: 'test' };
    const r1 = await Sample.findOne(q).cache(500, Sample.makeCacheKey(q));
    console.log(r1);

    if (!r1) {
        // Creates a sample if doesn't exist
        await Sample.create(q);
    }

    const r2 = await Sample.findOne(q).cache(500, Sample.makeCacheKey(q));
    // If r1 is null then r2 will be null due to it being cached, this means it works
    console.log(r2);

    // To clear the cache just use this
    redisgoose.clearCache(Sample.makeCacheKey(q));
};

main();

Example initialisation

const redisgoose = require('redisgoose');
// If redis is installed locally and with default port
redisgoose();

// With redis url
// redis[s]://[[username][:password]@][host][:port][/db-number]
redisgoose('redis://127.0.0.1:6379');

// With host and port explicit
redisgoose({
    host: '127.0.0.1',
    port: '6379'
});

// With a preinitialised redis client
redisgoose(client);
redisgoose({ client: client });

API

This api section is partially defined

This module is equipped with typescript type definitions
The typescript has full documentation

redisgoose

redisgoose.clearCache(key?)

Clears the cached value for the given key

If no key is provided then all cached values will be uncached

Returns: Promise<void>

Queue

Queue.redisManager

The redis manager initialised with the process

Query.prototype.cache(lifetime, key?)

Indicates the lifetime and may indicate the key This is required to cache a query to Redis This enables cacheResult

lifetime:
The lifetime in seconds
Type: number

key:
The string to use as key (recommended)
Type: string

Returns: this

Query.prototype.force()

Sets checkCache to false
This bypasses checking Redis cache and requests MongoDB directly

Returns this

Query.prototype.noSave()

This prevents saving the query result to Redis
Sets cacheResult to false
Implemented for convenience/function chaining

Returns this

Query.prototype.setCacheKey(key)

Sets the cache key

The difference with .cache is that this doesn't enable cacheResult
This can be used if you need to checkCache but not cacheResult

key:
The string to use as key
Type: string

Returns: this

Queue.prototype.checkCache

Whether to check Redis cache when executing a query

Type: boolean

Default: true

Queue.prototype.cacheKey

The key to cache the result with
This will default to a stringification of the query data if no key is provided

Type: string

Default: null

Query.prototype.cacheLifetime

The lifetime for the cached result in Redis memory

Type: number

Default: null

Queue.prototype.cacheResult

Whether to cache the result to Redis when the query is completed

Type: boolean

Default: true

Example key generator

/**
 *
 * @param model A mongoose model
 * @param args The chaining arguments for the key
 *
 */
function makeCacheKey(model, ...args) {
    return `${model.modelName}:${args.join(':')}`
}