Skip to content

Commit c96756c

Browse files
authored
Merge pull request #9 from yusufshakeel/dev
v1.6.0
2 parents 644086f + 1c272e0 commit c96756c

File tree

11 files changed

+736
-26
lines changed

11 files changed

+736
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Cache data using JavaScript in the browser.
55
### Status
66

77
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/yusufshakeel/dyCacheJS)
8-
[![npm version](https://img.shields.io/badge/npm-1.5.0-blue.svg)](https://www.npmjs.com/package/dycachejs)
8+
[![npm version](https://img.shields.io/badge/npm-1.6.0-blue.svg)](https://www.npmjs.com/package/dycachejs)
99
[![Build Status](https://travis-ci.org/yusufshakeel/dyCacheJS.svg?branch=master)](https://travis-ci.org/yusufshakeel/dyCacheJS)
1010
[![](https://data.jsdelivr.com/v1/package/npm/dycachejs/badge)](https://www.jsdelivr.com/package/npm/dycachejs)
1111

dist/js/dyCache.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ <h3 class="text-center">Content</h3>
9797
<li><a href="#object-methods">Object Methods</a></li>
9898
<li><a href="#stack-methods">Stack Methods</a></li>
9999
<li><a href="#queue-methods">Queue Methods</a></li>
100+
<li><a href="#lru-methods">LRU Methods</a></li>
100101
</ul>
101102

102103
<hr>
@@ -952,7 +953,7 @@ <h4><code>stackInit(key)</code></h4>
952953
<h4><code>stackExists(key)</code></h4>
953954

954955
<p>This will check if the stack referred by <code>key</code>
955-
exists in the cache.</p>
956+
exists in the cache.</p>
956957

957958
<p>Return <code>true</code> if stack exists. Otherwise, <code>false</code>.</p>
958959

@@ -1426,6 +1427,105 @@ <h4><code>queueDelete(key)</code></h4>
14261427

14271428
<hr>
14281429

1430+
<a name="lru-methods"></a>
1431+
<h4 class="text-center"><a href="#lru-methods">LRU Methods</a></h4>
1432+
1433+
<p class="text-center">LRU - Least Recently Used</p>
1434+
1435+
<br>
1436+
1437+
<!-- LRUInit() -->
1438+
<a name="LRUInit"></a>
1439+
<h4><code>LRUInit(name, size)</code></h4>
1440+
1441+
<p>This will create a new object for the LRU in the cache by the name <code>name</code>
1442+
having the given <code>size</code>.</p>
1443+
1444+
<p>If object referred by <code>name</code> exists in the cache then it will be overwritten.</p>
1445+
1446+
<p>If <code>size</code> is not set then LRU object of queue size 3 is created.</p>
1447+
1448+
<pre><code>obj.LRUInit('myLRU', 3); // creates a new LRU object myLRU</code></pre>
1449+
1450+
<div>
1451+
<a class="btn btn-primary"
1452+
role="button"
1453+
data-toggle="collapse"
1454+
href="#collapse-LRUInit"
1455+
aria-expanded="false"
1456+
aria-controls="collapse-LRUInit">Output</a>
1457+
</div>
1458+
<div class="collapse" id="collapse-LRUInit">
1459+
</div>
1460+
1461+
<!-- LRUInit() ends here -->
1462+
1463+
<hr>
1464+
1465+
<!-- LRUSet() -->
1466+
<a name="LRUSet"></a>
1467+
<h4><code>LRUSet(name, key, value)</code></h4>
1468+
1469+
<p>This will set <code>key</code>-<code>value</code> pair in the LRU object referred
1470+
by <code>name</code> in the cache.</p>
1471+
1472+
<p><code>key</code> can be a string or number.</p>
1473+
1474+
<p><code>value</code> can be a number, string, array or object.</p>
1475+
1476+
<p>Returns <code>true</code> on success</p>
1477+
1478+
<p>Returns <code>false</code> if LRU object does not exists in the cache.</p>
1479+
1480+
<pre><code>obj.LRUSet('myLRU', "k1", 10); // k1: 10
1481+
obj.LRUSet('myLRU', "k2", 20); // k2: 20
1482+
obj.LRUSet('myLRU', "k3", 30); // k3: 30</code></pre>
1483+
1484+
<div>
1485+
<a class="btn btn-primary"
1486+
role="button"
1487+
data-toggle="collapse"
1488+
href="#collapse-LRUSet"
1489+
aria-expanded="false"
1490+
aria-controls="collapse-LRUSet">Output</a>
1491+
</div>
1492+
<div class="collapse" id="collapse-LRUSet">
1493+
</div>
1494+
1495+
<!-- LRUSet() ends here -->
1496+
1497+
<hr>
1498+
1499+
<!-- LRUGet() -->
1500+
<a name="LRUGet"></a>
1501+
<h4><code>LRUGet(name, key)</code></h4>
1502+
1503+
<p>This will fetch key-value pair for the given <code>key</code> from the LRU object
1504+
referred by <code>name</code> in the cache.</p>
1505+
1506+
<p>Returns <code>{ key: value }</code> on success.</p>
1507+
1508+
<p>Returns <code>{}</code> if <code>key</code> is not found in the LRU object.</p>
1509+
1510+
<p>Returns <code>false</code> if the LRU object does not exists in the cache.</p>
1511+
1512+
<pre><code>obj.LRUGet('myLRU', "k1");</code></pre>
1513+
1514+
<div>
1515+
<a class="btn btn-primary"
1516+
role="button"
1517+
data-toggle="collapse"
1518+
href="#collapse-LRUGet"
1519+
aria-expanded="false"
1520+
aria-controls="collapse-LRUGet">Output</a>
1521+
</div>
1522+
<div class="collapse" id="collapse-LRUGet">
1523+
</div>
1524+
1525+
<!-- LRUGet() ends here -->
1526+
1527+
<hr>
1528+
14291529

14301530
</div>
14311531

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dycachejs",
3-
"version": "1.5.0",
3+
"version": "1.6.0",
44
"description": "Cache data using JavaScript in the browser.",
55
"main": "index.html",
66
"scripts": {

src/js/dyCache.forTest.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,94 @@ var dyCache = /** @class */ (function () {
726726
}
727727
return null;
728728
};
729+
/**
730+
* This will initialise a LRU (Least Recently Used) new object in the cache
731+
* and will refer it by 'name' and will also set the 'size' of the LRU cache.
732+
*
733+
* If size is not set then a LRU cache of size 3 is created.
734+
*
735+
* @param {string} name
736+
* @param {number} size
737+
* @constructor
738+
*/
739+
dyCache.prototype.LRUInit = function (name, size) {
740+
if (size === void 0) { size = 3; }
741+
this._cache[name] = {
742+
_size: size,
743+
_data: {},
744+
_queue: []
745+
};
746+
};
747+
/**
748+
* This will insert key-value pair in the LRU data referred by 'name' in the
749+
* cache.
750+
*
751+
* Return true on successful insert of key-value pair in the LRU, false otherwise.
752+
*
753+
* @param {string} name
754+
* @param key
755+
* @param value
756+
* @returns {boolean}
757+
* @constructor
758+
*/
759+
dyCache.prototype.LRUSet = function (name, key, value) {
760+
// if LRU doesn't exists
761+
if (!this.exists(name)) {
762+
return false;
763+
}
764+
// if queue has space
765+
if (this._cache[name]._size > this._cache[name]._queue.length) {
766+
// add new key-value pair in the data set
767+
this._cache[name]._data[key] = value;
768+
// and insert the key in the queue
769+
this._cache[name]._queue.unshift(key);
770+
}
771+
else {
772+
// remove the least recently used key from the back of the queue
773+
var removedKey = this._cache[name]._queue.pop();
774+
// remove the least recently used key-value pair from the data set
775+
delete this._cache[name]._data[removedKey];
776+
// now insert the new key-value in the data set
777+
this._cache[name]._data[key] = value;
778+
// and insert the new key in the queue
779+
this._cache[name]._queue.unshift(key);
780+
}
781+
return true;
782+
};
783+
/**
784+
* This will fetch the cached data in the LRU.
785+
*
786+
* If key exists in the LRU then it is returned in { key: value } form.
787+
* Otherwise, {} is returned.
788+
*
789+
* false is returned if the LRU doesn't exists.
790+
*
791+
* @param {string} name
792+
* @param key
793+
* @returns {any}
794+
* @constructor
795+
*/
796+
dyCache.prototype.LRUGet = function (name, key) {
797+
// if LRU doesn't exists
798+
if (!this.exists(name)) {
799+
return false;
800+
}
801+
// if key found
802+
if (this._cache[name]._queue.indexOf(key) > -1) {
803+
// remove the key from the queue from its current index
804+
this._cache[name]._queue.splice(this._cache[name]._queue.indexOf(key), 1);
805+
// now add the key at the front of the queue
806+
this._cache[name]._queue.unshift(key);
807+
// now prepare the value
808+
var data = {};
809+
data[key] = this._cache[name]._data[key];
810+
// and return the key-value pair in { key: value } form
811+
return data;
812+
}
813+
else {
814+
return {};
815+
}
816+
};
729817
return dyCache;
730818
}());
731819

0 commit comments

Comments
 (0)