Skip to content

Commit

Permalink
Added addOrUpdate()
Browse files Browse the repository at this point in the history
  • Loading branch information
mansoor-omrani committed Jul 8, 2021
1 parent b54ffac commit dd761b5
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 1 deletion.
30 changes: 30 additions & 0 deletions index.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ var CacheBase = /*#__PURE__*/function () {
value: function setItem(key, value, duration) {
(0, _locustjsException.throwNotImplementedException)('CacheBase.setItem');
}
}, {
key: "addOrUpdate",
value: function addOrUpdate(key, fnUpdate, value, duration) {
(0, _locustjsException.throwNotImplementedException)('CacheBase.addOrUpdate');
}
}, {
key: "getOrSet",
value: function getOrSet(key, value, duration) {
Expand Down Expand Up @@ -246,6 +251,26 @@ var CacheDefault = /*#__PURE__*/function (_CacheBase) {

return value;
}
}, {
key: "addOrUpdate",
value: function addOrUpdate(key, fnUpdate, value, duration) {
var entry = this.getEntry(key);
var result;

if (entry == null) {
var _duration = this.getDuration(duration);

this._data.push(new CacheItem(key, value, _duration));

result = value;
} else {
var newValue = (0, _locustjsBase.isFunction)(fnUpdate) ? fnUpdate(entry.value) : value;
entry.setValue(newValue);
result = newValue;
}

return result;
}
}, {
key: "exists",
value: function exists(key) {
Expand Down Expand Up @@ -435,6 +460,11 @@ var CacheNull = /*#__PURE__*/function (_CacheBase2) {
}, {
key: "setItem",
value: function setItem(key, value, duration) {}
}, {
key: "addOrUpdate",
value: function addOrUpdate(key, fnUpdate, value, duration) {
return null;
}
}, {
key: "exists",
value: function exists(key) {
Expand Down
26 changes: 26 additions & 0 deletions index.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class CacheBase {
setItem(key, value, duration) {
throwNotImplementedException('CacheBase.setItem');
}
addOrUpdate(key, fnUpdate, value, duration) {
throwNotImplementedException('CacheBase.addOrUpdate');
}
getOrSet(key, value, duration) {
throwNotImplementedException('CacheBase.getOrSet');
}
Expand Down Expand Up @@ -149,6 +152,26 @@ class CacheDefault extends CacheBase {

return value;
}
addOrUpdate(key, fnUpdate, value, duration) {
const entry = this.getEntry(key);
let result;

if (entry == null) {
const _duration = this.getDuration(duration);

this._data.push(new CacheItem(key, value, _duration))

result = value;
} else {
const newValue = isFunction(fnUpdate) ? fnUpdate(entry.value): value;

entry.setValue(newValue);

result = newValue;
}

return result;
}
exists(key) {
const entry = this.getEntry(key);

Expand Down Expand Up @@ -296,6 +319,9 @@ class CacheNull extends CacheBase {
return null;
}
setItem(key, value, duration) { }
addOrUpdate(key, fnUpdate, value, duration) {
return null;
}
exists(key) {
return false;
}
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": "locustjs-cache",
"version": "1.1.2",
"version": "1.2.0",
"description": "This library provides a simple caching class.",
"main": "index.cjs.js",
"module": "index.esm.js",
Expand Down
25 changes: 25 additions & 0 deletions tests/Cache.cjs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,31 @@ const { CacheDefault } = require('../index.cjs');
expect(x).toBe(34)
});

// --------------------- addOrUpdate -------------------

test('addOrUpdate: add', () => {
const cache = config.factory();

cache.addOrUpdate('key1', x => x + 1, 30);

const x = cache.getItem('key1');

expect(x).toBe(30)
});

// --------------------- addOrUpdate -------------------

test('addOrUpdate: update', () => {
const cache = config.factory();

cache.setItem('key1', 24);
cache.addOrUpdate('key1', x => x + 1, 30);

const x = cache.getItem('key1');

expect(x).toBe(25)
});

// --------------------- exists -------------------

test('exists: checks whether an item is inside in cahce based on its key and returns true/false <existing>', () => {
Expand Down
25 changes: 25 additions & 0 deletions tests/Cache.esm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,31 @@ import { CacheDefault } from '../index.esm';
expect(x).toBe(34)
});

// --------------------- addOrUpdate -------------------

test('addOrUpdate: add', () => {
const cache = config.factory();

cache.addOrUpdate('key1', x => x + 1, 30);

const x = cache.getItem('key1');

expect(x).toBe(30)
});

// --------------------- addOrUpdate -------------------

test('addOrUpdate: update', () => {
const cache = config.factory();

cache.setItem('key1', 24);
cache.addOrUpdate('key1', x => x + 1, 30);

const x = cache.getItem('key1');

expect(x).toBe(25)
});

// --------------------- exists -------------------

test('exists: checks whether an item is inside in cahce based on its key and returns true/false <existing>', () => {
Expand Down

0 comments on commit dd761b5

Please sign in to comment.