Skip to content

Commit 4d6550c

Browse files
committed
LRU demo code update
1 parent e469a73 commit 4d6550c

File tree

2 files changed

+129
-9
lines changed

2 files changed

+129
-9
lines changed

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

src/js/index-page-example.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ obj.purge();
9999
output("After purge", 'collapse-purge');
100100
outputJSON(obj, 'collapse-purge');
101101

102-
// arrInit();
102+
// arrInit()
103103
output("Initialise an <code>users</code> key in the cache which is an array.", 'collapse-arrInit');
104104
obj.arrInit('users');
105105
output("Content of the cache.", 'collapse-arrInit');
@@ -201,8 +201,6 @@ outputJSON(obj.arrGet('users'), 'collapse-arrDeleteElems');
201201

202202
// purge()
203203
obj.purge();
204-
output("Content of the cache after purge.", 'collapse-oInit');
205-
outputJSON(obj, 'collapse-oInit');
206204

207205
// oInit();
208206
output("This will create a new object referred by <code>players</code> in the cache.", 'collapse-oInit');
@@ -255,8 +253,6 @@ outputJSON(obj.oGetAll('players'), 'collapse-oDel');
255253

256254
// purge()
257255
obj.purge();
258-
output("Content of the cache after purge.", 'collapse-stackInit');
259-
outputJSON(obj, 'collapse-stackInit');
260256

261257
// stackInit();
262258
output("Initialise stack referred by key <code>myStack</code> in the cache.", 'collapse-stackInit');
@@ -314,8 +310,6 @@ outputJSON(obj, 'collapse-stackDelete');
314310

315311
// purge()
316312
obj.purge();
317-
output("Content of the cache after purge.", 'collapse-queueInit');
318-
outputJSON(obj, 'collapse-queueInit');
319313

320314
// queueInit();
321315
output("Initialise queue referred by key <code>myQueue</code> in the cache.", 'collapse-queueInit');
@@ -375,4 +369,30 @@ outputJSON(obj, 'collapse-queuePurge');
375369
output("This will delete the queue referred by key <code>myQueue</code> in the cache.", 'collapse-queueDelete');
376370
outputJSON(obj.queueDelete('myQueue'), 'collapse-queueDelete');
377371
output("Content of the cache.", 'collapse-queueDelete');
378-
outputJSON(obj, 'collapse-queueDelete');
372+
outputJSON(obj, 'collapse-queueDelete');
373+
374+
// purge()
375+
obj.purge();
376+
377+
// LRUInit();
378+
output("This will create a new object for LRU referred by <code>myLRU</code> of size 3 in the cache.", 'collapse-LRUInit');
379+
obj.LRUInit('myLRU');
380+
output("Content of the cache.", 'collapse-LRUInit');
381+
outputJSON(obj, 'collapse-LRUInit');
382+
383+
// LRUSet();
384+
output("This will set 3 key-value pairs in the LRU object referred by <code>myLRU</code> in the cache.", 'collapse-LRUSet');
385+
obj.LRUSet("myLRU", "k1", 10);
386+
obj.LRUSet("myLRU", "k2", 20);
387+
obj.LRUSet("myLRU", "k3", 30);
388+
output("Content of the cache.", 'collapse-LRUSet');
389+
outputJSON(obj, 'collapse-LRUSet');
390+
391+
// LRUGet();
392+
output("Content of the cache before <code>LRUGet</code> operation.", 'collapse-LRUGet');
393+
outputJSON(obj, 'collapse-LRUGet');
394+
output("Fetching 'k1'", 'collapse-LRUGet');
395+
outputJSON(obj.LRUGet("myLRU", "k1"), 'collapse-LRUGet');
396+
output("Content of the cache after <code>LRUGet</code> operation.", 'collapse-LRUGet');
397+
outputJSON(obj, 'collapse-LRUGet');
398+
output("Note! 'k1' is moved from last index to 0th index in the queue after <code>LRUGet</code> operation.", 'collapse-LRUGet');

0 commit comments

Comments
 (0)