Skip to content

Commit

Permalink
Performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
rajkumardusad committed Mar 27, 2023
1 parent 1710557 commit 083f73a
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 69 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ const cache = new Cache();
cache.set("a", 10);

// Check data exists in cache
cache.has("a");
cache.has("a"); // true

// Get data from cache
console.log(cache.get("a"));
console.log(cache.get("a")); // 10

// Get all data from cache
cache.forEach(function (data) {
console.log(data); // { a: 10 }
});

// Get all data to array
console.log(cache.toArray());
console.log(cache.toArray()); // [ { a: 10 } ]

// Delete data from cache
cache.delete("a");
Expand All @@ -62,25 +62,28 @@ cache.delete("a");
cache.clear();
```

## Create a new cache object
## Create a new cache

To create a new cache we need to create a new instance of cachejs. While creating a new cache we can set the configuration like eviction policy, cache max length and ttl, but it is not mandatory and if we not set any configuration then the default values are used.

Defination:

```js
const cache = new Cache(options);
```

Where options are the following:

- `evictionPolicy` : eviction policy is can be any valid cache eviction policy, supported eviction policy are FIFO, LIFO, LRU, MRU
- `maxLength` : max length is a cache max length, max length is a positive integer value. The default value is 0, if the value is 0 then it will not check the max length.
- `ttl` : is cache expires time in milliseconds, the default value is 0 and if value if 0 it will not check the ttl.
- `interval` : interval is the time interval in milliseconds, after every interval all the expired values are automatically removed. Default value is 0 and if value is 0 the it will not removes expired values automatically, but don't worry expired items are treated as missing, and deleted when they are fetched.
- `interval` : interval is the time interval in milliseconds, after every interval all the expired items are automatically removed. Default value is 0 and if value is 0 the it will not removes expired items automatically, but don't worry expired items are treated as missing, and deleted when they are fetched.
- `enableInterval` : enableInterval is a boolean value that is used to enable and disable the interval, the default value is false and if value is explicitly set false then it will not run the interval even if the interval time is set.

Cachejs support TTL, but it is not a TTL cache, and also does not make strong TTL guarantees. When interval is set expired values are removed from cache periodically.
Cachejs support TTL, but it is not a TTL cache, and also does not make strong TTL guarantees. When ttl interval is set, expired items are removed from cache periodically.

Example:

```js
const Cache = require("@opensnip/cachejs");

Expand All @@ -97,7 +100,7 @@ const cache = new Cache({

In cachejs any value (both objects and primitive values) may be used as either a key or a value, duplicate keys not allowed and if duplicate item is inserted it will be replaced by the new item.

```js
````js
// Add new data in cache
cache.set("a", 10);

Expand All @@ -114,7 +117,7 @@ By default the configuration TTL value is used for every item, but we can set TT
```js
// Add new data in cache
cache.set("b", 10, { ttl: 200 }); // Expires after 200 ms
```
````
## Get data from cache
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@opensnip/cachejs",
"version": "1.0.0",
"version": "1.0.1",
"description": "Fast and lightweight caching library for javascript",
"main": "index.mjs",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion src/cache.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ module.exports = class Cache {

this.#config.intervalId = setInterval(
function (cache) {
if (cache.length == 0) return;
if (cache.length === 0) return;
cache.forEach(function (data) {
// Automatically invalidate expired cache
});
Expand Down
2 changes: 1 addition & 1 deletion src/cache.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export default class Cache {

this.#config.intervalId = setInterval(
function (cache) {
if (cache.length == 0) return;
if (cache.length === 0) return;
cache.forEach(function (data) {
// Automatically invalidate expired cache
});
Expand Down
10 changes: 5 additions & 5 deletions src/fifo.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = class FIFO {
// Insert a new node at head
const existingNode = this.#cache.get(key);
// Update node data if node is already exists
if (existingNode instanceof Node) {
if (typeof existingNode !== "undefined") {
existingNode.value = nodeValue;
} else {
// Remove node if cache is full
Expand All @@ -91,7 +91,7 @@ module.exports = class FIFO {

const node = this.#cache.get(key);

if (node instanceof Node) {
if (typeof node !== "undefined") {
// Check node is live or not
if (this.#isStale(node)) {
this.delete(key);
Expand All @@ -118,15 +118,15 @@ module.exports = class FIFO {
delete(key) {
const node = this.#cache.get(key);

if (node instanceof Node) {
if (typeof node !== "undefined") {
this.#linkedList.delete(node);
// Delete node
this.#cache.delete(key);
}
}

#evict() {
if (this.#linkedList.tail == null) return;
if (this.#linkedList.tail === null) return;
if (this.length !== this.#maxLength) return;
this.delete(this.#linkedList.tail.value.key);
}
Expand All @@ -140,7 +140,7 @@ module.exports = class FIFO {
has(key) {
const node = this.#cache.get(key);

if (node instanceof Node) {
if (typeof node !== "undefined") {
// Check node is live or not
if (this.#isStale(node)) {
this.delete(key);
Expand Down
10 changes: 5 additions & 5 deletions src/fifo.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default class FIFO {
// Insert a new node at head
const existingNode = this.#cache.get(key);
// Update node data if node is already exists
if (existingNode instanceof Node) {
if (typeof existingNode !== "undefined") {
existingNode.value = nodeValue;
} else {
// Remove node if cache is full
Expand All @@ -91,7 +91,7 @@ export default class FIFO {

const node = this.#cache.get(key);

if (node instanceof Node) {
if (typeof node !== "undefined") {
// Check node is live or not
if (this.#isStale(node)) {
this.delete(key);
Expand All @@ -118,15 +118,15 @@ export default class FIFO {
delete(key) {
const node = this.#cache.get(key);

if (node instanceof Node) {
if (typeof node !== "undefined") {
this.#linkedList.delete(node);
// Delete node
this.#cache.delete(key);
}
}

#evict() {
if (this.#linkedList.tail == null) return;
if (this.#linkedList.tail === null) return;
if (this.length !== this.#maxLength) return;
this.delete(this.#linkedList.tail.value.key);
}
Expand All @@ -140,7 +140,7 @@ export default class FIFO {
has(key) {
const node = this.#cache.get(key);

if (node instanceof Node) {
if (typeof node !== "undefined") {
// Check node is live or not
if (this.#isStale(node)) {
this.delete(key);
Expand Down
10 changes: 5 additions & 5 deletions src/lifo.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = class LIFO {
// Insert a new node at head
const existingNode = this.#cache.get(key);
// Update node data if node is already exists
if (existingNode instanceof Node) {
if (typeof existingNode !== "undefined") {
existingNode.value = nodeValue;
} else {
// Remove node if cache is full
Expand All @@ -91,7 +91,7 @@ module.exports = class LIFO {

const node = this.#cache.get(key);

if (node instanceof Node) {
if (typeof node !== "undefined") {
// Check node is live or not
if (this.#isStale(node)) {
this.delete(key);
Expand All @@ -118,15 +118,15 @@ module.exports = class LIFO {
delete(key) {
const node = this.#cache.get(key);

if (node instanceof Node) {
if (typeof node !== "undefined") {
this.#linkedList.delete(node);
// Delete node
this.#cache.delete(key);
}
}

#evict() {
if (this.#linkedList.head == null) return;
if (this.#linkedList.head === null) return;
if (this.length !== this.#maxLength) return;
this.delete(this.#linkedList.head.value.key);
}
Expand All @@ -140,7 +140,7 @@ module.exports = class LIFO {
has(key) {
const node = this.#cache.get(key);

if (node instanceof Node) {
if (typeof node !== "undefined") {
// Check node is live or not
if (this.#isStale(node)) {
this.delete(key);
Expand Down
10 changes: 5 additions & 5 deletions src/lifo.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default class LIFO {
// Insert a new node at head
const existingNode = this.#cache.get(key);
// Update node data if node is already exists
if (existingNode instanceof Node) {
if (typeof existingNode !== "undefined") {
existingNode.value = nodeValue;
} else {
// Remove node if cache is full
Expand All @@ -91,7 +91,7 @@ export default class LIFO {

const node = this.#cache.get(key);

if (node instanceof Node) {
if (typeof node !== "undefined") {
// Check node is live or not
if (this.#isStale(node)) {
this.delete(key);
Expand All @@ -118,15 +118,15 @@ export default class LIFO {
delete(key) {
const node = this.#cache.get(key);

if (node instanceof Node) {
if (typeof node !== "undefined") {
this.#linkedList.delete(node);
// Delete node
this.#cache.delete(key);
}
}

#evict() {
if (this.#linkedList.head == null) return;
if (this.#linkedList.head === null) return;
if (this.length !== this.#maxLength) return;
this.delete(this.#linkedList.head.value.key);
}
Expand All @@ -140,7 +140,7 @@ export default class LIFO {
has(key) {
const node = this.#cache.get(key);

if (node instanceof Node) {
if (typeof node !== "undefined") {
// Check node is live or not
if (this.#isStale(node)) {
this.delete(key);
Expand Down
15 changes: 6 additions & 9 deletions src/linkedlist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ module.exports = class LinkedList {
}
tmpRight.prev = leftNode.prev;
tmpRight.next = leftNode.next;
if (leftNode == this.#head) this.#head = tmpRight;
if (leftNode == this.#tail) this.#tail = tmpRight;
if (leftNode === this.#head) this.#head = tmpRight;
if (leftNode === this.#tail) this.#tail = tmpRight;

// Replace right node with left node
let tmpLeft = new Node(leftNode.value);
Expand All @@ -137,8 +137,8 @@ module.exports = class LinkedList {
}
tmpLeft.prev = rightNode.prev;
tmpLeft.next = rightNode.next;
if (rightNode == this.#head) this.#head = tmpLeft;
if (rightNode == this.#tail) this.#tail = tmpLeft;
if (rightNode === this.#head) this.#head = tmpLeft;
if (rightNode === this.#tail) this.#tail = tmpLeft;

delete leftNode.next;
delete leftNode.prev;
Expand All @@ -150,7 +150,7 @@ module.exports = class LinkedList {
}

deleteHead() {
if (this.#head == null) return;
if (this.#head === null) return;
if (this.#head.next != null) {
this.#head.next.prev = null;
}
Expand All @@ -160,7 +160,7 @@ module.exports = class LinkedList {
}

deleteTail() {
if (this.#tail == null) return;
if (this.#tail === null) return;
if (this.#tail.prev != null) {
this.#tail.prev.next = null;
}
Expand All @@ -170,9 +170,6 @@ module.exports = class LinkedList {
}

delete(node) {
if (!(node instanceof Node)) {
throw new TypeError("node should be a valid Node instance");
}
this.detach(node);
delete node.prev;
delete node.next;
Expand Down
15 changes: 6 additions & 9 deletions src/linkedlist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ export default class LinkedList {
}
tmpRight.prev = leftNode.prev;
tmpRight.next = leftNode.next;
if (leftNode == this.#head) this.#head = tmpRight;
if (leftNode == this.#tail) this.#tail = tmpRight;
if (leftNode === this.#head) this.#head = tmpRight;
if (leftNode === this.#tail) this.#tail = tmpRight;

// Replace right node with left node
let tmpLeft = new Node(leftNode.value);
Expand All @@ -137,8 +137,8 @@ export default class LinkedList {
}
tmpLeft.prev = rightNode.prev;
tmpLeft.next = rightNode.next;
if (rightNode == this.#head) this.#head = tmpLeft;
if (rightNode == this.#tail) this.#tail = tmpLeft;
if (rightNode === this.#head) this.#head = tmpLeft;
if (rightNode === this.#tail) this.#tail = tmpLeft;

delete leftNode.next;
delete leftNode.prev;
Expand All @@ -150,7 +150,7 @@ export default class LinkedList {
}

deleteHead() {
if (this.#head == null) return;
if (this.#head === null) return;
if (this.#head.next != null) {
this.#head.next.prev = null;
}
Expand All @@ -160,7 +160,7 @@ export default class LinkedList {
}

deleteTail() {
if (this.#tail == null) return;
if (this.#tail === null) return;
if (this.#tail.prev != null) {
this.#tail.prev.next = null;
}
Expand All @@ -170,9 +170,6 @@ export default class LinkedList {
}

delete(node) {
if (!(node instanceof Node)) {
throw new TypeError("node should be a valid Node instance");
}
this.detach(node);
delete node.prev;
delete node.next;
Expand Down
Loading

0 comments on commit 083f73a

Please sign in to comment.