Skip to content

Commit eca67e7

Browse files
committed
LRUResize example code
1 parent bcfb377 commit eca67e7

File tree

3 files changed

+128
-9
lines changed

3 files changed

+128
-9
lines changed

index.html

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,41 @@ <h4><code>LRUGet(name, key)</code></h4>
15521552

15531553
<hr>
15541554

1555+
<!-- LRUResize() -->
1556+
<a name="LRUResize"></a>
1557+
<h4><code>LRUResize(name, size)</code></h4>
1558+
1559+
<p>This will resize the LRU object
1560+
referred by <code>name</code> in the cache.</p>
1561+
1562+
<p>If <code>size</code> is not mentioned then LRU object is resized to 3.</p>
1563+
1564+
<p>If new <code>size</code> is less than the current size then,
1565+
first 'size'-th key is kept in the queue and data set and rest of the keys are removed.</p>
1566+
1567+
<p>For example, if current size is 10 and new size is 4,
1568+
then first 4 key is retained in the data set and queue while the
1569+
remaining keys are removed.</p>
1570+
1571+
<p>Returns <code>true</code> on success, <code>false</code> otherwise.</p>
1572+
1573+
<pre><code>obj.LRUResize('myLRU', 2);</code></pre>
1574+
1575+
<div>
1576+
<a class="btn btn-primary"
1577+
role="button"
1578+
data-toggle="collapse"
1579+
href="#collapse-LRUResize"
1580+
aria-expanded="false"
1581+
aria-controls="collapse-LRUResize">Output</a>
1582+
</div>
1583+
<div class="collapse" id="collapse-LRUResize">
1584+
</div>
1585+
1586+
<!-- LRUResize() ends here -->
1587+
1588+
<hr>
1589+
15551590
<!-- LRUPurge() -->
15561591
<a name="LRUPurge"></a>
15571592
<h4><code>LRUPurge(name)</code></h4>

src/js/index-page-example.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ outputJSON(obj, 'collapse-set');
5050

5151
// set('user', { username: 'yusufshakeel', points: 10 });
5252
output("Adding a <code>user</code> key having string value <code>{ username: 'yusufshakeel', points: 10 }</code>.", 'collapse-set');
53-
obj.set('user', { username: 'yusufshakeel', points: 10 });
53+
obj.set('user', {username: 'yusufshakeel', points: 10});
5454
outputJSON(obj, 'collapse-set');
5555

5656
// set('arr', [1, 2, 3]);
@@ -116,7 +116,7 @@ outputJSON(obj, 'collapse-arrPush');
116116

117117
// arrMPush();
118118
output("Push multiple values in an array referred by <code>users</code> key in the cache from the right", 'collapse-arrMPush');
119-
obj.arrMPush('users', [100, 'superman', ['a1', 'b2', 200], { id: 10, points: 20}]);
119+
obj.arrMPush('users', [100, 'superman', ['a1', 'b2', 200], {id: 10, points: 20}]);
120120
output("Content of the cache.", 'collapse-arrMPush');
121121
outputJSON(obj, 'collapse-arrMPush');
122122

@@ -128,7 +128,7 @@ outputJSON(obj, 'collapse-arrLPush');
128128

129129
// arrMLPush();
130130
output("Push multiple values in an array referred by <code>users</code> key in the cache from the left", 'collapse-arrMLPush');
131-
obj.arrMLPush('users', [1, 'hi', [1, 3], { m: 1 }]);
131+
obj.arrMLPush('users', [1, 'hi', [1, 3], {m: 1}]);
132132
output("Content of the cache.", 'collapse-arrMLPush');
133133
outputJSON(obj, 'collapse-arrMLPush');
134134

@@ -169,13 +169,16 @@ outputJSON(obj.arrGet('users'), 'collapse-arrInsertAt');
169169

170170
// arrMInsertAt();
171171
output("Insert multiple values at given <code>index</code> in a array referred by key <code>users</code> in the cache.", 'collapse-arrMInsertAt');
172-
outputJSON(obj.arrMInsertAt('users', 1, [ {id: 9999, name: 'yusufshakeel'}, 10, 'happy', [100] ]), 'collapse-arrMInsertAt');
172+
outputJSON(obj.arrMInsertAt('users', 1, [{
173+
id: 9999,
174+
name: 'yusufshakeel'
175+
}, 10, 'happy', [100]]), 'collapse-arrMInsertAt');
173176
output("Fetch all the elements of the array referred by <code>users</code> key in the cache.", 'collapse-arrMInsertAt');
174177
outputJSON(obj.arrGet('users'), 'collapse-arrMInsertAt');
175178

176179
// arrUpdateElem();
177180
output("Update value of an element at index 0 in the array referred by key <code>users</code> in the cache.", 'collapse-arrUpdateElem');
178-
outputJSON(obj.arrUpdateElem('users', 0, { username: 'tintin', points: 50 }), 'collapse-arrUpdateElem');
181+
outputJSON(obj.arrUpdateElem('users', 0, {username: 'tintin', points: 50}), 'collapse-arrUpdateElem');
179182
output("Fetch all the elements of the array referred by <code>users</code> key in the cache.", 'collapse-arrUpdateElem');
180183
outputJSON(obj.arrGet('users'), 'collapse-arrUpdateElem');
181184

@@ -210,11 +213,11 @@ outputJSON(obj, 'collapse-oInit');
210213

211214
// oSet();
212215
output("This will add <code>p1</code> oKey having value <code>{ id: 'p1', username: 'yusufshakeel' }</code> in the key <code>players</code> of the cache.", 'collapse-oSet');
213-
obj.oSet('players', 'p1', { id: 'p1', username: 'yusufshakeel' });
216+
obj.oSet('players', 'p1', {id: 'p1', username: 'yusufshakeel'});
214217
output("Content of the cache.", 'collapse-oSet');
215218
outputJSON(obj, 'collapse-oSet');
216219
output("This will add <code>p2</code> oKey having value <code>{ id: 'p2', username: 'dawoodshakeel' }</code> in the key <code>players</code> of the cache.", 'collapse-oSet');
217-
obj.oSet('players', 'p2', { id: 'p2', username: 'dawoodshakeel' });
220+
obj.oSet('players', 'p2', {id: 'p2', username: 'dawoodshakeel'});
218221
output("Content of the cache.", 'collapse-oSet');
219222
outputJSON(obj, 'collapse-oSet');
220223

@@ -272,7 +275,7 @@ obj.stackPush('myStack', 'Yusuf Shakeel');
272275
output("Push array value in stack referred by key <code>myStack</code> in the cache.", 'collapse-stackPush');
273276
obj.stackPush('myStack', [1, 2]);
274277
output("Push object value in stack referred by key <code>myStack</code> in the cache.", 'collapse-stackPush');
275-
obj.stackPush('myStack', { a:10 });
278+
obj.stackPush('myStack', {a: 10});
276279
output("Content of the cache.", 'collapse-stackPush');
277280
outputJSON(obj, 'collapse-stackPush');
278281

@@ -329,7 +332,7 @@ obj.enqueue('myQueue', 'Yusuf Shakeel');
329332
output("Enqueue array value from the right side in queue referred by key <code>myQueue</code> in the cache.", 'collapse-enqueue');
330333
obj.enqueue('myQueue', [1, 2]);
331334
output("Enqueue object value from the right side in queue referred by key <code>myQueue</code> in the cache.", 'collapse-enqueue');
332-
obj.enqueue('myQueue', { a:10 });
335+
obj.enqueue('myQueue', {a: 10});
333336
output("Content of the cache.", 'collapse-enqueue');
334337
outputJSON(obj, 'collapse-enqueue');
335338

@@ -401,6 +404,13 @@ output("Content of the cache after <code>LRUGet</code> operation.", 'collapse-LR
401404
outputJSON(obj, 'collapse-LRUGet');
402405
output("Note! 'k1' is moved from last index to 0th index in the queue after <code>LRUGet</code> operation.", 'collapse-LRUGet');
403406

407+
// LRUResize();
408+
output("Content of the cache before <code>LRUResize</code> operation.", 'collapse-LRUResize');
409+
outputJSON(obj, 'collapse-LRUResize');
410+
obj.LRUResize('myLRU', 2);
411+
output("Content of the cache after <code>LRUResize</code> operation.", 'collapse-LRUResize');
412+
outputJSON(obj, 'collapse-LRUResize');
413+
404414
// LRUPurge();
405415
output("Content of the cache before <code>LRUPurge</code> operation.", 'collapse-LRUPurge');
406416
outputJSON(obj, 'collapse-LRUPurge');

src/js/test.web.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,80 @@ describe('Testing dyCacheJS', function () {
13021302

13031303
});
13041304

1305+
describe('Testing LRUResize()', () => {
1306+
1307+
it('should return false when resizing LRU that does not exists in the cache', function () {
1308+
1309+
let result = obj.LRUResize("unknownLRU", 10);
1310+
assert.isFalse(result);
1311+
1312+
});
1313+
1314+
it('should return true when resizing LRU referred by myLRU in the cache', function () {
1315+
1316+
obj.LRUInit('myLRU', 3);
1317+
let result = obj.LRUResize("myLRU", 20);
1318+
assert.isTrue(result);
1319+
1320+
});
1321+
1322+
it('should resize LRU from size 3 to size 10 referred by myLRU in the cache', function () {
1323+
1324+
let expected = {
1325+
"myLRU": {
1326+
"_size": 10,
1327+
"_data": {},
1328+
"_queue": []
1329+
}
1330+
};
1331+
1332+
// init
1333+
obj.LRUInit('myLRU', 3);
1334+
1335+
// resize
1336+
obj.LRUResize('myLRU', 10);
1337+
1338+
assert.deepEqual(obj._cache, expected);
1339+
1340+
});
1341+
1342+
it('should resize LRU from size 5 to size 3 referred by myLRU in the cache', function () {
1343+
1344+
let expected = {
1345+
"myLRU": {
1346+
"_size": 3,
1347+
"_data": {
1348+
"k3": 30,
1349+
"k4": 40,
1350+
"k5": 50
1351+
},
1352+
"_queue": [
1353+
"k5",
1354+
"k4",
1355+
"k3"
1356+
]
1357+
}
1358+
};
1359+
1360+
// init
1361+
obj.LRUInit('myLRU', 5);
1362+
1363+
// set
1364+
obj.LRUSet('myLRU', 'k1', 10);
1365+
obj.LRUSet('myLRU', 'k2', 20);
1366+
obj.LRUSet('myLRU', 'k3', 30);
1367+
obj.LRUSet('myLRU', 'k4', 40);
1368+
obj.LRUSet('myLRU', 'k5', 50);
1369+
1370+
// resize
1371+
obj.LRUResize('myLRU', 3);
1372+
1373+
assert.deepEqual(obj._cache, expected);
1374+
1375+
});
1376+
1377+
});
1378+
13051379
});
13061380

13071381
});

0 commit comments

Comments
 (0)