Giter Club home page Giter Club logo

node-cache's Introduction

memory-cache Build Status

A simple in-memory cache for node.js

Installation

npm install memory-cache --save

Usage

var cache = require('memory-cache');

// now just use the cache

cache.put('foo', 'bar');
console.log(cache.get('foo'));

// that wasn't too interesting, here's the good part

cache.put('houdini', 'disappear', 100, function(key, value) {
    console.log(key + ' did ' + value);
}); // Time in ms

console.log('Houdini will now ' + cache.get('houdini'));

setTimeout(function() {
    console.log('Houdini is ' + cache.get('houdini'));
}, 200);


// create new cache instance
var newCache = new cache.Cache();

newCache.put('foo', 'newbaz');

setTimeout(function() {
  console.log('foo in old cache is ' + cache.get('foo'));
  console.log('foo in new cache is ' + newCache.get('foo'));
}, 200);

which should print

bar
Houdini will now disappear
houdini did disappear
Houdini is null
foo in old cache is baz
foo in new cache is newbaz

API

put = function(key, value, time, timeoutCallback)

  • Simply stores a value
  • If time isn't passed in, it is stored forever
  • Will actually remove the value in the specified time in ms (via setTimeout)
  • timeoutCallback is optional function fired after entry has expired with key and value passed (function(key, value) {})
  • Returns the cached value

get = function(key)

  • Retrieves a value for a given key
  • If value isn't cached, returns null

del = function(key)

  • Deletes a key, returns a boolean specifying whether or not the key was deleted

clear = function()

  • Deletes all keys

size = function()

  • Returns the current number of entries in the cache

memsize = function()

  • Returns the number of entries taking up space in the cache
  • Will usually == size() unless a setTimeout removal went wrong

debug = function(bool)

  • Turns on or off debugging

hits = function()

  • Returns the number of cache hits (only monitored in debug mode)

misses = function()

  • Returns the number of cache misses (only monitored in debug mode)

keys = function()

  • Returns all the cache keys

exportJson = function()

  • Returns a JSON string representing all the cache data
  • Any timeoutCallbacks will be ignored

importJson = function(json: string, options: { skipDuplicates: boolean })

  • Merges all the data from a previous call to export into the cache
  • Any existing entries before an import will remain in the cache
  • Any duplicate keys will be overwritten, unless skipDuplicates is true
  • Any entries that would have expired since being exported will expire upon being imported (but their callbacks will not be invoked)
  • Available options:
    • skipDuplicates: If true, any duplicate keys will be ignored when importing them. Defaults to false.
  • Returns the new size of the cache

Cache = function()

  • Cache constructor
  • note that require('cache') would return the default instance of Cache
  • while require('cache').Cache is the actual class

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Send me a pull request.

node-cache's People

Contributors

adambalogh avatar alevicki avatar amilajack avatar austinpray avatar bradleyg avatar chrisantaki avatar clouds56 avatar cooperka avatar crabmusket avatar dblock avatar jasonrhodes avatar justincy avatar kemitchell avatar loris avatar lukehorvat avatar peterfriese avatar progame-ltd avatar ptarjan avatar ramonsnir avatar robertpallas avatar tamilsweet avatar vxst avatar wanasit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-cache's Issues

size and memsize gives always 0 result

we have check cached data exists after put but keys returns empty result
also size and memsize is zero always

require("memory-cache").size() => 0
require("memory-cache").memsize() => 0

require("memory-cache").keys() empty array

Does it work with objects?

Your examples are just showing plain text, does the node-cache work with objects? Or I have to stringfy the object

.get() for cached MongoDB data with Date() objects strips Date() of its methods

Attempted to call a native date function on a Date object from some cached MongoDB data, and received this error:

TypeError: Object 00:04:23 has no method 'getHours'

In my code, first the MongoDB query runs and displays the data fine, and then on reload it uses the cached raw data, and throws this error when it tries to run getHours on the cached Date object.

cache key length limit

I'm wondering if the cache key has a length limit? Is it the same with the length limit of String on V8? Will these long keys impact the performance?

How do I prevent objects from being rendered as [Object]

Hi @ptarjan first of all, thank you very much for your fantastic contribution with regards to this library. This is by far the easiest library to use which means it must have been the hardest to code as well

I have a situation where I store a JSON object of the following format

{
    "dotdList": [
    {
        "title": "...",
        "description": "...",
        "url": "...",
        "imageUrls": [
        {
            "url": "...",
            "resolutionType": "..."
        },
        {
            "url": "...",
            "resolutionType": "..."
        },
        {
            "url": "...",
            "resolutionType": "..."
        },
        {
            "url": "...",
            "resolutionType": "..."
        }
        ],
        "availability": "..."
    }
    ]
}

While storing everything works great, but while retrieving, this is what I retrieve. As you can see the nested imageUrl array has been mapped to its Object form. How do I expand it? Thank you for your answer in advance

[  
   {  
      title:"...",
      description:"...",
      url:"...",
      imageUrls:[  
         [  
            Object
         ],
         [  
            Object
         ],
         [  
            Object
         ],
         [  
            Object
         ]
      ],
      availability:"..."
   }
]

keys() writes checks that get() can't cash

I'd like to do something like this:

const values = cache.keys().map((key) => cache.get(key));

Based on the documentation, I expect:

  • that values does not contain any nulls if I had not inserted any into cache,
  • that cache does not delete elements while timers do not have a chance to run and
  • that a function named get does not delete elements.

Currently memory-cache deletes elements both with timers and as a side-effect within get among other methods. I think it would be helpful to write such design decisions high up in the README. The API description of get in the README does not explain this behavior. The uncertainty in the result of del is the only hint about side-effects.

License?

What is the license on this library? The project needs a license file or the license specified at the bottom of the README.

maxvalue for time

What's the max value for put? I put in 30 days in ms, 2592000000, and i only got cache misses.

How to refresh timer?

If I have already created a cache value like

cache.put('foo', 'bar, 10000);

How can I refresh the counter again if the function is called again?

Currently it seems that the function will erase after 10 seconds regardless if it was called every second.

LRU Option

I've been using node-cache for nearly a year now and I really appreciate it. I was wondering if you might be interested in adding an LRU option. I would love to be able to have confidence that node-cache will not use more memory I want it to.

To be clear, I'm not talking about limiting the number of keys, but the number of bytes.

What are your thoughts?

Issue in cluster environment

When run our node module in cluster environment each cluster creates its own cache instance in memory. This is not acceptable module should have the capability to share same cache instance within multiple clusters.

I'm using pm2 : process manager for cluster.

Any development on this?

.put() return value

Could we make .put() return the value being cached?

I'm using this module to cache the responses of a small Express app, and I've found that I seem to employ the following pattern a lot:

let data = [1, 2, 3, 4];
cache.put(key, data);
response.send(data);

It would be nice if you could do something like the following, so I don't need to have a separate caching step and data variable:

response.send(cache.put(key, [1, 2, 3, 4]));

Plus, it would also make it similar to object literal assignment:

obj[key] = value; // Returns value.

Does it auto clean

It says it will clear cache on get.

So if I have a million things and never get, they stay in ram don't timeout?

expire bug

original:

exports.put = function(key, value, time) {
if (debug) sys.puts('caching: '+key+' = '+value+' (@'+time+')');
var expire = time + now();
cache[key] = {value: value, expire: expire}

// clean up space
if (!isNaN(expire)) {
setTimeout(function() {
exports.del(key);
}, expire);
}
}

bug fix:

exports.put = function(key, value, time) {
if (debug) sys.puts('caching: '+key+' = '+value+' (@'+time+')');
var expire = time + now();
cache[key] = {value: value, expire: expire}

// clean up space
if (!isNaN(expire)) {
setTimeout(function() {
exports.del(key);
}, time);
}
}

cache must Immutable

const x = {a: 1};
cache.put("a", x);
const y = cache.get("a");
log(y);
//{ a: 1 }
y['t'] = "unwanted";
const z = cache.get("a");
log('z', z);
// z { a: 1, t: 'unwanted' }

expire

can we set expire time or ttl?
no mention in readme

nodeunit tests wait for the timeout function to finish

I'm not very sure if this can be considered "an issue".

Running the following test with nodeunit

exports['ttt'] =
    nodeunit.testCase(
        {
         test: function(test) {
             var cache = require('memory-cache');
             cache.put('key', 'value', 1000000);
             test.ok(true);
             test.done();
         }
        }
    );

won't show back the prompt until the cache has deleted the key, which is after 1000000 ms passed.

Shouldn't it be better if, instead of using a setTimeout function to delete the key, the absolute timeout parameter were saved with the key and then when trying to access it with get, if (time() > timeout) return null;

weird error since last update

hey there!
I was using version 0.1.2 and everything was working fine.
As I updated today to version 0.1.3 I cannot longer retrieve stored key value pairs.

This is what worked on 0.1.2 and is not working in 0.1.3 anymore:

//myAwesomeJSFile1.js
var myAwesomeCache = require("memory-cache");
myAwesomeCache.put("myKey","myValue");

//myAwesomeJSFile2.js
var myAwesomeCache = require("memory-cache");
console.log(myAwesomeCache.get("myKey")); 
//prints "myValue" in 0.1.2
//prints null in 0.1.3

does it maybe have something to do with the added parameter validation?

Best regards!

Create a package.json

Hello there,

I wonder, is it possibile to create a package.json file and add this project on npm?

Thanks

Time is not working properly

As per documentation, If time isn't passed in, it is stored forever.

In API, I am storing data from database into cache without passing time but after some time, when I am trying to get data from cache, I am getting null. Is this the expected behaviour or it should store data for lifetime. Please help.

Is this a bug about `this.del` function

if (!isNaN(oldRecord.expire) && oldRecord.expire < Date.now()) {

When a record is about to expire, we take the initiative to delete it. And due to the running time of the program, oldRecord.expire < Date.now() === true, so the record will not be deleted.

README typo

I follow the code in the Usage section, my result is bar not baz:

...
foo in old cache is bar 
...

No repository in package.json

While you're publishing packages to npm, can you please push a repository URL too? If you're using the package.json file in the repository, you could add:

"repository" :
  { "type" : "git"
  , "url" : "git://github.com/ptarjan/node-cache.git"
  }

Missing a size--

in exports.get, a size-- is missing when delete cache[key]; is being called (on line 79).

Cache.get returns reference to object rather than copy of object

If you use cache.get to retrieve an object from the cache, and then you extend the object that you retrieved with other variables, the underlying cache gets updated without needing to call cache.put. For example:

var a = cache.get('a')
a.user = 'Some Guy';
a.private = 'personal information for Some Guy';
// the cached version of a now has .user and .private

cache.get should return a deep copy of the object, or you can run into some very confusing use cases without realizing it.

Get metadata about the cache entries

Hi,

It's very useful already how we can call cache.size(). What would you think of also exposing something like cache.entries() that gives you the list of keys, when they were last updated, and when they expire?

Adding support for array values

I had an use case where I wanted to store array as a value and later push/pop some items.

I think it would make sense to add methods like push, pop, unshift, etc, to work with array values directly from cache object, instead of getting the value, manipulating and then putting it back in cache.

I would like to discuss this in more details and send a PR if you think this will be useful in general.

Graphql result sometimes broken

I'm trying this package,gotta say is awesome and saved me a LOT of MongoDB connections! Kudos! ๐Ÿ˜ That being said, when I do a GraphQL request, sometimes I get a strange result. Something like this

{ "data": { "__schema": { .... } } }

This happens rarely, and it seems to happen only when the query result has not been cached yet. But when it happens is of course annoying. I don't know what could be the cause for this, and as I said this happens rarely so is also hard to reproduce, but I've setup a repository with the code I'm using. Maybe is there something wrong in how I implemented or registered the package? :/
https://github.com/GimignanoF/Test-GraphQL

Okay for web browsers?

Right now, node-cache looks solid for browser use. Is it going to remain that way, or are there plans in the future to use node-specific features?

Edit: It seems that tinycache addresses this.

Increment hit count outside of debug mode.

Why only monitor hits and misses in debug mode? Maintaining two integers shouldn't have a negative impact on performance. It also seems strange to report that as part of the API even though its optional and off by default.

Singleton on cluster mode

I have an api gateway running in cluster mode (4), using this lib to cache requests, is there any way to share an instance of lib between the 4 processes?

captura de tela 2018-06-05 as 09 06 23

expire property necessary?

Since we have kept reference of timeout handle, is it necessary to add expire as a property and check it every time we call get method? Is not obvious that it exists means it is not expired? Any other uses ?

How big is memory-cache?

I was wondering if there's anyway we can know how much memory will memory-cache took? can we limit/control it somehow? Will the performance goes down if the size of memory-cache is too big?

max memory limit

Hi, what do you think about add this feature?
I mean just a simple way to control the cache grown.

Cache not shrinking with timeouts

Awesome just right library, thanks!

Running into some occasional out of memory issues. Timeout set to 5 minutes. So added some logging which indicated the cache delete was not always being fired. Looks like the issue is that there is a slight timing issue between when the timeout is called and oldRecord.expire < Date.now().

In this case, the timeout fires, but oldRecord.expire < Date.now() is still true, so the cache object is not deleted. Timeout has been cleared, so the code won't get called again.

I forked node-cache and hacked in the line:

if (!isNaN(record.expire)) {
     record.timeout = setTimeout(function() {
+      delete record.expire;
       exports.del(key);

While this solves the issue for my case, e.g. timeout always wins, i wouldn't know if it has unintended consequences, or what would be the better way to fix this.

The more direct approach is to assume if export.del is called, the user really wants to delete, regardless of expiration.

On second review, additionally, maybe the logic was supposed to be ">" not "<"? I assume the intent was to prevent deletion if the expiration date has not yet been reached?

if (!isNaN(oldRecord.expire) && oldRecord.expire > Date.now()) {
  canDelete = false;
}

How can I cache a third party json?

Hi,

I've created an API that is served through a static json on a separate domain. I would like to cache this json on my server and pass it through my app so that I don't have to hit my API with every visitor. I'm not very experienced, so I'm not sure how I would achieve this.

Currently, my routes are being cached, but they are still sending the http request to my third party json. Let's assume that my api url is https://exampleapi.com/item.json

This is what I currently have (none of my attempts at json caching has worked):

var cache = (duration) => {
  return (req, res, next) => {
    let key = '__express__' + req.originalUrl || req.url
    let cachedBody = mcache.get(key)
    if (cachedBody) {
      console.log('There is a cachedBody')
      res.send(cachedBody)
      return
    } else {
      console.log('There is not a cachedBody')
      res.sendResponse = res.send
      res.send = (body) => {
        mcache.put(key, body, duration *1000);
        res.sendResponse(body)
      }
      next()
    }
  }
}

app.get('/', cache(60), function(req, res) {
  setTimeout(() => {
    res.render('../dist/index', {
      req: req,
      res: res
    });
    console.timeEnd(`GET: ${req.originalUrl}`);
  }, 5000);
});

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.