Git Product home page Git Product logo

cacheman-mongo's Introduction

cacheman-mongo

Build Status NPM version

MongoDB standalone caching library for Node.JS and also cache engine for cacheman.

Instalation

$ npm install cacheman-mongo

Usage

var CachemanMongo = require('cacheman-mongo');
var cache = new CachemanMongo();

// set the value
cache.set('my key', { foo: 'bar' }, function (error) {

  if (error) throw error;

  // get the value
  cache.get('my key', function (error, value) {

    if (error) throw error;

    console.log(value); //-> {foo:"bar"}

    // delete entry
    cache.del('my key', function (error){
      
      if (error) throw error;

      console.log('value deleted');
    });

  });
});

API

CachemanMongo([options])

Create cacheman-mongo instance. options are mongo valid options including port, host, database and collection.

var options = { 
  port: 9999,
  host: '127.0.0.1',
  username: 'beto',
  password: 'my-p@ssw0rd'
  database: 'my-cache-db',
  collection: 'my-collection',
  compression: false
};

var cache = new CachemanMongo(options);

You can also pass a valid mongodb connection string as first arguments like this:

var options = {
  collection: 'account'
};

var cache = new CachemanMongo('mongodb://127.0.0.1:27017/blog', options);

Or pass a mongodb db instance directly as client:

MongoClient.connect('mongodb://127.0.0.1:27017/blog', function (err, db) {
  var cache = new CachemanMongo(db, { collection: 'account' });

  // or
  cache = new CachemanMongo({ client: db, collection: 'account' });
});

Cache Value Compression

MongoDB has a 16MB document size limit, and, currently, does not have built in support for compression. You can enable cache value compression for large Buffers by setting the compression options to true, this will use the native node zlib module to compress with gzip. It only compresses Buffers because of the complexity in correctly decompressing and deserializing the variety of data structures and string encodings.

Thanks to @Jared314 for adding this feature.

var cache = new Cache({ compression: true });
cache.set('test1', new Buffer("something big"), function (err) {...});

cache.set(key, value, [ttl, [fn]])

Stores or updates a value.

cache.set('foo', { a: 'bar' }, function (err, value) {
  if (err) throw err;
  console.log(value); //-> {a:'bar'}
});

Or add a TTL(Time To Live) in seconds like this:

// key will expire in 60 seconds
cache.set('foo', { a: 'bar' }, 60, function (err, value) {
  if (err) throw err;
  console.log(value); //-> {a:'bar'}
});

cache.get(key, fn)

Retreives a value for a given key, if there is no value for the given key a null value will be returned.

cache.get('foo', function (err, value) {
  if (err) throw err;
  console.log(value);
});

cache.del(key, [fn])

Deletes a key out of the cache.

cache.del('foo', function (err) {
  if (err) throw err;
  // foo was deleted
});

cache.clear([fn])

Clear the cache entirely, throwing away all values.

cache.clear(function (err) {
  if (err) throw err;
  // cache is now clear
});

Run tests

$ make test

License

(The MIT License)

Copyright (c) 2015 Jonathan Brumley <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

cacheman-mongo's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

cacheman-mongo's Issues

set seems to work but get fails

When I do a cache.set('test', 'Test Value')

In mongo I see:

{
    "_id": {
        "$oid": "5549661e89332748a381b58d"
    },
    "key": "cache::test",
    "value": "Test Value",
    "expire": 1430873900201
}

When I do a

cache.get('test, function(err, data) {
   console.log(data, typeof data) // null object
}

Thoughts?

Log statements

Hi there,

Switched from Redis to MongoDB with Cacheman. However, there are a lot of the following statements in the console:

the options [engine] is not supported
the options [username] is not supported
the options [password] is not supported
the options [host] is not supported
the options [port] is not supported
the options [database] is not supported
the options [collection] is not supported
the options [compression] is not supported
the options [hosts] is not supported

Connection itself works and all is good, but wondering what we can do about these statements please.

Thank you.

How to configure for a mongo replication set

when configured with more than one host

such as when passing this type of configuration

{
    host: 'db-host-01, db-host-02, db-host-03',
    username: 'db-user',
    password: 'db-pasword',
    database: 'db',
    collection: 'cached',
    compression: false
}

Any call to set results in a error being raised with this message.

"missing delimiting slash between hosts and options:"

The configuration is identical to the normal mongo configuration (which works) except for the mongo db has an additional option in the config

{
    replica: 'replset-name'
}

and when there is more than one host the connection string has to be formed like this:

Expected format - according to Mongo Docs

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

The connection string right now comes out with the wrong format (just a simple concatenation) of the properties and hence the error

wrong format

mongodb://[username:password@]db-host-01, db-host-02, db-host-03[:port1][/[database][?options]]

Duplicate Key Entry in Cache Collection

I'm experiencing duplicated cache entries when using the MongoDB engine with cacheman.

Below is the partial output of the collection used by cacheman to persist the cache. Has you may notice the key cacheman:flows:User:77FhwgJtDQyPTSs9u is present twice.

/* 1 */
{
    "_id" : ObjectId("59cd076bd24488ac2138fc4a"),
    "key" : "cacheman:flows:User:77FhwgJtDQyPTSs9u",
    "value" : "{\"checkin\":{\"context\":{\"dates\":{\"date_start\":1506637800,\"date_end\":1506643199},\"state\":\"request\",\"type\":\"invalid_project\",\"flowData\":{\"records\":[{\"hours\":8}]}},\"active\":true,\"finished\":false}}",
    "expireAt" : ISODate("2017-10-28T14:30:03.441Z")
}

/* 2 */
{
    "_id" : ObjectId("59cd076bd24488ac2138fc49"),
    "key" : "cacheman:flows:User:77FhwgJtDQyPTSs9u",
    "value" : "{\"checkin\":{\"context\":{\"dates\":{\"date_start\":1506637800,\"date_end\":1506643199},\"state\":\"request\",\"type\":\"invalid_project\",\"flowData\":{\"records\":[{\"hours\":8}]}},\"active\":true,\"finished\":false}}",
    "expireAt" : ISODate("2017-10-28T14:30:03.436Z")
}

Is this a bug or a feature? ๐Ÿ˜„

mongodb native mode is no more supported after upgrade to 1.0.3

Hi

I have and issue after upgrade from 1.0.1 to 1.0.3.

TypeError: Object #<NativeConnection> has no method 'ensureIndex'

in version 1.0.3 there is new lines which calls ensureIndex. Since this does not exist in native mode above error get thrown

basically I try to run following code

var mongoose = require('mongoose');
var CachemanMongo = require('cacheman-mongo');

console.log('Setting up mongoose');

var mongoose_db;
var cache;
var connect = function() {
    var options = {
        server: {
            socketOptions: {
                keepAlive: 1
            }
        }
    };

    databaseURL = 'codolutionstest.com:27017/mydb';

    mongoose_db = mongoose.createConnection(databaseURL, options);
    cache = new CachemanMongo({
        client: mongoose_db,
        collection: 'cacheman'
    });
};


connect();


mongoose_db.on('error', console.log);
mongoose_db.on('disconnected', connect);


cache.get('cacheKey', function(err, data) {
    console.log(data);
});

Passing MongoDB Instance Directly as Client Does Not Work

I have a db.js helper file which is used to establish a persistent connection to my MongoDB database that I can reference elsewhere in my program by requiring db.js and calling getDB() which returns the connection string for connecting to my database.

If I pass this connection string in as follows, I get "the options [client] is not supported, the options [database] is not supported, the options [collection] is not supported, the options [hosts] is not supported:

let CachemanMongo = require('cacheman-mongo');
let db = require('../helpers/db).getDB();
var cache = new CachemanMongo(db, { collection: 'assetsList_cache' });

Also, specifying db as the client in the CachemanMongo options returns the same errors:

var cache = new CachemanMongo({ client: db, collection: 'assetsList_cache' });

And after throwing these errors, CachemanMongo defaults to trying to connect to a localhost MongoDB instance, which obviously fails since a local instance does not exists.

Passing in an established MongoDB instance in this way had previously worked just fine but no longer does for some reason, and I know that it is not a problem with my database or the way that I am calling the connection string, because I call getDB() to get the connection string in other places in my same program and can interact just fine with my database there. How can I use my existing database instance with CachemanMongo?

node/index.js getTime() error 1.0.1 - 1.0.3

When the version was switched from 1.0.1 to 1.0.3 I started receiving this error:

TypeError: Cannot read property 'getTime' of undefined at node_modules/cacheman-mongo/node/index.js:124:28

It is resolved when returning back to 1.0.1 package.

Help: Connection length?

What is the recommended process in terms of opening the connections.
Should I be starting a new instance of cacheman-mongo each request (express) or leave one open constantly?

Connection with MongoDB URI show's log messages

const cache = new CachemanMongo(sails.config.connections.mongocache, {
  collection: 'cache'
});

this connection style shows the following output:

the options [collection] is not supported

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.