Code Monkey home page Code Monkey logo

node-cache-manager-couchbase's Introduction

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)

node-cache-manager-couchbase's People

Contributors

davidepellegatta avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.