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

timed-cache

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

timed-cache - npm Package Compare versions

Comparing version 1.1.5 to 2.0.0

.deepsource.toml

165

cache.js

@@ -11,60 +11,31 @@ /**

/**
* Exporting the `Cache` module appropriately given
* the environment (AMD, Node.js and the browser).
*/
(function (name, definition) {
/* istanbul ignore next */
if (typeof define === 'function' && define.amd) {
// Defining the module in an AMD fashion.
define(definition);
} else if (typeof module !== 'undefined' && module.exports) {
// Exporting the module for Node.js/io.js.
module.exports = definition();
} else {
var gl = this;
var instance = definition();
var old = gl[name];
/**
* Shortcut function for checking if an object has
* a given property directly on itself.
*/
const has = (obj, key) => obj !== null && Object.prototype.hasOwnProperty.call(obj, key);
/**
* Allowing to scope the module
* avoiding global namespace pollution.
*/
instance.noConflict = function () {
gl[name] = old;
return instance;
};
// Exporting the module in the global
// namespace in a browser context.
gl[name] = instance;
}
})('Cache', function () {
/**
* A prefix used to forbid access to internal properties
* of the object storage.
*/
const prefix = '__cache__';
/**
* We use an object literal ads the
* internal storage.
*/
var cache = {};
/**
* If the key is an object, we serialize it, so it
* can be cached transparently.
*/
const serialize = function (key) {
if (typeof key !== 'string') {
return (prefix + JSON.stringify(key));
}
return (prefix + key);
};
/**
* Shortcut function for checking if an object has
* a given property directly on itself
* (in other words, not on a prototype).
*/
var has = function (obj, key) {
return (obj !== null && Object.prototype.hasOwnProperty.call(obj, key));
};
/**
* The `timed-cache` implementation.
*/
class Cache {
/**
* If the key is an object, we serialize it, so it
* can be cached transparently.
*/
var serialize = function (key) {
if (typeof key !== 'string') {
return (this.prefix + JSON.stringify(key));
}
return (this.prefix + key);
};
/**
* Cache constructor.

@@ -74,3 +45,5 @@ * @param {*} options the `options` object

*/
var Cache = function (options) {
constructor(options = { defaultTtl: 60 * 1000 }) {
// The cache storage.
this.cache = {};
// The default cached objects expiration

@@ -81,8 +54,4 @@ // delay is expressed in milliseconds and

// constructor.
this.defaultTtl = (options && options.defaultTtl) ?
options.defaultTtl : 60 * 1000;
// A prefix used to forbid access to internal properties
// of the object storage.
this.prefix = '__cache__';
};
this.defaultTtl = options.defaultTtl || 60 * 1000;
}

@@ -92,12 +61,11 @@ /**

*/
Cache.prototype.put = function (key, value, options) {
var ttl = (options ? options.ttl : undefined) || this.defaultTtl;
var cb = (options ? options.callback : undefined) || function () {};
var key_ = serialize.call(this, key);
var self = this;
put(key, value, options) {
const ttl = (options ? options.ttl : undefined) || this.defaultTtl;
const callback = (options ? options.callback : undefined) || function () {};
const key_ = serialize(key);
// Checking whether the given key already
// has a value.
var v = cache[key_];
const v = this.cache[key_];
if (v) {

@@ -108,13 +76,11 @@ // We clear the timeout associated with

}
// We then create a new timeout function for
// the new value.
var handle = setTimeout(function () {
return (self.remove(key));
}, ttl);
const handle = setTimeout(() => this.remove(key), ttl);
// And we save the value into the cache storage
// with the handle.
cache[key_] = { handle: handle, data: value, callback: cb };
};
this.cache[key_] = { handle, data: value, callback };
}

@@ -126,15 +92,6 @@ /**

*/
Cache.prototype.get = function (key, fn, opts) {
var value = cache[serialize.call(this, key)];
// If the value does not exist, but there's a resolver
// function, we invoke it for a result.
if (!value && (typeof fn === 'function')) {
var data = fn.apply(this);
// Inserting the returned value in the cache.
this.put(key, data, opts);
return (data);
}
get(key) {
const value = this.cache[serialize(key)];
return (value && value.data);
};
}

@@ -145,12 +102,12 @@ /**

*/
Cache.prototype.remove = function (key) {
var key_ = serialize.call(this, key);
var value = cache[key_];
remove(key) {
const key_ = serialize(key);
const value = this.cache[key_];
if (value) {
clearTimeout(value.handle);
delete cache[key_];
delete this.cache[key_];
value.callback(key, value.data);
}
};
}

@@ -160,10 +117,10 @@ /**

*/
Cache.prototype.clear = function () {
for (var entry in cache) {
if (has(cache, entry)) {
clearTimeout(cache[entry].handle);
clear() {
for (const entry in this.cache) {
if (has(this.cache, entry)) {
clearTimeout(this.cache[entry].handle);
}
}
cache = {};
};
this.cache = {};
}

@@ -174,7 +131,7 @@ /**

*/
Cache.prototype.size = function () {
return Object.keys(cache).length;
};
size() {
return (Object.keys(this.cache).length);
}
}
return (Cache);
});
export default Cache;
{
"name": "timed-cache",
"version": "1.1.5",
"version": "2.0.0",
"description": "A minimalist time-based caching system.",
"main": "cache.js",
"type": "module",
"repository": {

@@ -11,4 +12,5 @@ "type": "git",

"scripts": {
"test": "npx grunt test",
"build": "npx grunt",
"build": "npx rollup -c",
"test": "npx cross-env NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles",
"lint": "npx eslint .",
"prepublishOnly": "npm run build"

@@ -27,13 +29,7 @@ },

"devDependencies": {
"expect": "^27.0.2",
"grunt": "^1.4.1",
"grunt-contrib-clean": "^2.0.0",
"grunt-contrib-copy": "^1.0.0",
"grunt-contrib-jshint": "^3.0.0",
"grunt-contrib-uglify": "^5.0.1",
"grunt-mocha-test": "^0.13.3",
"load-grunt-tasks": "^5.1.0",
"mocha": "^8.4.0",
"time-grunt": "^2.0.0"
"eslint": "^8.14.0",
"jest": "^28.0.3",
"rollup": "^2.71.1",
"rollup-plugin-terser": "^7.0.2"
}
}

@@ -1,10 +0,10 @@

<p align="center">
<br /><br /><br /><br /><p align="center">
<img width="200" src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/49/Applications-database.svg/240px-Applications-database.svg.png" />
</p>
</p><br /><br /><br />
## Cache storage
[![Build Status](https://travis-ci.org/HQarroum/timed-cache.svg?branch=master)](https://travis-ci.org/HQarroum/timed-cache)
[![Build Status](https://app.travis-ci.com/HQarroum/timed-cache.svg?branch=master)](https://app.travis-ci.com/HQarroum/timed-cache)
[![CodeFactor](https://www.codefactor.io/repository/github/hqarroum/timed-cache/badge)](https://www.codefactor.io/repository/github/hqarroum/timed-cache)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FHQarroum%2Ftimed-cache.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2FHQarroum%2Ftimed-cache?ref=badge_shield)
[![DeepSource](https://deepsource.io/gh/HQarroum/timed-cache.svg/?label=active+issues&show_trend=true&token=3BI4ee76_7OTVBAzTvku0WQj)](https://deepsource.io/gh/HQarroum/timed-cache/?ref=repository-badge)

@@ -15,10 +15,6 @@ A minimalist time-based caching system.

Current version: **1.1.5**
Current version: **2.0.0**
Lead Maintainer: [Halim Qarroum](mailto:hqm.post@gmail.com)
## Install
##### Using NPM
```bash

@@ -28,13 +24,13 @@ npm install --save timed-cache

##### Using Bower
## Usage
```bash
bower install --save timed-cache
```
### Import the cache module
## Usage
`timed-cache` is distributed as an ESM module that you can import in your implementation.
You will first have to require the `timed-cache` module in your application in order to use it.
```javascript
import Cache from 'timed-cache';
```
The module can be required in an AMD manner, using node's `require` or using the `Cache` variable exported in the global namespace in the context of a browser.
### Creating the cache module

@@ -46,4 +42,5 @@ Basic operations you can perform on an instance of a `Cache` are insertion, retrieval and removal of key/value pairs.

```javascript
var cache = new Cache();
const cache = new Cache();
```
Note that by default, a key/value pair will be held by the cache storage for `60` seconds before being evicted.

@@ -56,3 +53,3 @@

// In this case it will be equal to `5` minutes.
var cache = new Cache({ defaultTtl: 300 * 1000 });
const cache = new Cache({ defaultTtl: 300 * 1000 });
```

@@ -104,5 +101,3 @@

ttl: 5 * 1000,
callback: function (key, value) {
console.log(key, value, 'evicted !');
}
callback: (key, value) => console.log(`${key} ${value} evicted !`)
});

@@ -117,6 +112,5 @@ ```

cache.put('foo', 'bar', {
callback: function (key, value) {
console.log(key, value, 'removed !');
}
callback: (key, value) => console.log(`${key} ${value} removed !`)
});
cache.remove('foo');

@@ -127,40 +121,3 @@ ```

### Building
This project uses `Grunt` as its build system and `Bower` amd `NPM` as dependency management systems.
Grunt uses the `Gruntfile.js` file to actually build the project, and will as a *default* task copy the produced binaries in the `dist/` folder.
Grunt relies on `Node.js` to execute the tasks required to build the project, so you will need to ensure that it is available on your build machine.
To install Grunt, its modules, and fetch the Bower dependencies of the project you will need to run the following command :
```bash
# This will install Grunt tasks and fetch the
# required Bower module as a postinstall task.
npm install
```
To run a build using the default task, simply run the following :
```bash
grunt
```
## Tests
Tests are available in the `tests/` directory.
You can either trigger them using `Jasmine JS` and its HTML presenter by opening `tests/index.html` in a browser, or trigger the following commands :
```bash
# Using grunt
grunt test
# Using NPM
npm test
```
## License
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FHQarroum%2Ftimed-cache.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2FHQarroum%2Ftimed-cache?ref=badge_large)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FHQarroum%2Ftimed-cache.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2FHQarroum%2Ftimed-cache?ref=badge_large)

Sorry, the diff of this file is not supported yet

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