Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ttl-cache

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ttl-cache - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

41

lib/cache.js

@@ -6,2 +6,3 @@ var events = require('events'),

function bind(f, to) {
if (!f) console.log("No function", f);
return function () {

@@ -20,4 +21,4 @@ return f.apply(to, arguments);

function start(cache) {
if (cache.options.clean_interval > 0) {
cache.timer = setTimeout(cache.clean, cache.options.clean_interval * 1000);
if (cache.options.interval > 0) {
cache.timer = setTimeout(cache.clean, cache.options.interval * 1000);
}

@@ -31,9 +32,10 @@ }

if (ttl && ttl < now) {
this.stats.keys--;
cache.stats.keys--;
delete cache.data[key];
cache.emit("expired", key);
delete cache.ttls[key];
cache.emit("expire", key);
return;
}
return this.data[key];
return cache.data[key];
}

@@ -43,3 +45,5 @@

function Cache(options) {
this.options = options || {};
if (!(this instanceof Cache)) {
return new Cache(options);
}

@@ -59,10 +63,8 @@ this.get = bind(this.get, this);

this.options = {
ttl: 300, // Default TTL 5 minutes.
clean_interval: 60 // Clean every minute.
ttl: 300, // Default TTL 5 minutes.
interval: 60 // Clean every minute.
};
options.forEach(function(v,k) {
if (this.options.hasOwnProperty(k))
this.options[k] = v;
});
if (options && options.ttl) this.options.ttl = options.ttl;
if (options && options.interval) this.options.interval = options.interval;

@@ -75,3 +77,3 @@ this.stats = {

this.check_data();
start(this);
}

@@ -83,3 +85,3 @@

Cache.prototype.get = function (key) {
var v = this.check(key);
var v = fetch(this, key);

@@ -121,3 +123,3 @@ if ('undefined' !== typeof v) {

if ('undefined' === typeof val) {
del(key);
this.del(key);
return exists;

@@ -175,2 +177,3 @@ }

this.data = {};
this.ttls = {};
this.stats = {

@@ -196,10 +199,10 @@ hits: 0,

if (data.hasOwnProperty(key)) {
ttl = cache.ttls[key]
ttl = this.ttls[key]
if (ttl && ttl < now) {
this.stats.keys--;
delete cache.data[key];
delete cache.ttls[key];
delete this.data[key];
delete this.ttls[key];
c++;
cache.emit("expired", key);
this.emit("expire", key);
}

@@ -206,0 +209,0 @@ }

{
"name": "ttl-cache",
"version": "1.0.1",
"version": "1.0.2",
"description": "Simple in-memory object cache with TTL based per-item expiry",

@@ -5,0 +5,0 @@ "main": "lib/cache.js",

@@ -12,3 +12,4 @@ node-ttl-cache

var cache = require('ttl-cache')();
var Cache = require('ttl-cache'),
cache = new Cache();

@@ -22,2 +23,9 @@ old_value = cache.set(key, value); // Set a value (returns old)

## Options:
var cache({
ttl: 300, // Number of seconds to keep entries
interval: 60 // Cleaning interval
});
## License:

@@ -24,0 +32,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc