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

cache-manager-couchbase

v0.1.5

Published

Couchbase store for node-cache-manager

Downloads

7

Readme

Couchbase store for node cache manager

Couchbase cache store for node-cache-manager.

Installation

npm install cache-manager-couchbase --save

or

yarn add cache-manager-couchbase

Usage Examples

See examples below on how to implement the Redis cache store.

Single store

var cacheManager = require('cache-manager');
var couchbaseStore = require('cache-manager-couchbase');

var config = {
  connectionString: 'couchbase://127.0.0.1:8091',
  connectionOptions: {
    username: 'Administrator',
    password: 'couchbase'
  },
  bucket: 'test-bucket',
  ttl: 2
};

var couchbaseCache = cacheManager.caching({
    connectionString: config.connectionString,
    store: couchbaseStore,
    connectionOptions: config.connectionOptions,
    scope: config.scope,
    bucket: config.bucket,
    collection: config.collection,
    ttl: 60
  });

var ttl = 2;

couchbaseCache.set('foo', 'bar', { ttl: ttl }, (err) => {
  if (err) {
    throw err;
  }

  couchbaseCache.get('foo', (err, result) => {
    console.log(result);
    // >> 'bar'
    couchbaseCache.del('foo', (err) => {
    });
  });
});

function getUser(id, cb) {
  setTimeout(() => {
    console.log("Returning user from slow database.");
    cb(null, { id: id, name: 'Bob' });
  }, 100);
}

var userId = 123;
var key = `user_${userId}`;

// Note: ttl is optional in wrap()
couchbaseCache.wrap(key, (cb) => {
  getUser(userId, cb);
}, { ttl: ttl }, (err, user) => {
  console.log(user);

  // Second time fetches user from redisCache
  couchbaseCache
    .wrap(key, () => getUser(userId))
    .then(console.log)
    .catch(err => {
      // handle error
    });
});

Couchbase Buckets

Couchbase use buckets, scopes and collection to segment customer. The cache-manager-couchbase configuration let you choose between all of them.

Example for couchbase pre-7.x (no scopes and collection):

var config = {
  connectionString: 'couchbase://127.0.0.1:8091',
  connectionOptions: {
    username: 'Administrator',
    password: 'your-secret-p@ssw0rd'
  },
  bucket: 'your-bucket'
};

Example for couchbase >7.x:

var config = {
  connectionString: 'couchbase://127.0.0.1:8091',
  connectionOptions: {
    username: 'Administrator',
    password: 'your-secret-p@ssw0rd'
  },
  bucket: 'your-bucket',
  scope: 'your-scope',
  collection: 'your-collection',
  ttl: 2
};

Couchbase Connection

The configuration let you customise how the plugin connects to your couchbase cluster.

var config = {
  connectionString: 'couchbases://node1.example.com,node2.example.com',
  connectionOptions: {
    trustStorePath: '/path/to/ca/certificates.pem',
    username: 'Administrator',
    password: 'couchbase'
  },
  bucket: 'your-bucket'
};

connectionString let you provide one or multiple seed nodes and connectionOptions are passed right away to the underlying couchbase nodejs SDK so that you can use all options provided here on the couchbase documentation

Important! Setting up your bucket for cache-manager keys method

Once your couchbase bucket is set up you will need to create an index so that the keys method can effectively look up for your keys.

Here an example:

CREATE INDEX `all_keys` ON `your-bucket`((meta().`id`))

If you see an error like the following:

error properties: Object({ cause: Error: libcouchbase error 401, context: QueryErrorContext({ first_error_code: 4000, first_error_message: 'No index available on keyspace test-bucket that matches your query. Use CREATE INDEX or CREATE PRIMARY INDEX to create an index, or check that your expected index is online.', statement: 'SELECT RAW meta(b).id FROM  `test-bucket` b  WHERE REGEXP_CONTAINS(META(b).id, $PATTERN)', client_context_id: 'c1ed4896bea27e6c', parameters: '', http_response_code: 404, http_response_body: '' }) })

It means the above mentioned index is missing!

Keys method

The 'keys' method let you selectively extract keys. This selection on couchbase is implemented through a SQL query using the REGEXP_CONTAINS so tha you can use a regexp to select your keys.

Example:

couchbaseCache.keys('f*')

Next Implementations

  • auto provision keys index
  • Improve connection error handling
  • Improve keys method performances
  • Verify ttl method compliancy
  • Upgrade Couchbase Cluster connection to a non-blocking mode (To Be Checked)