It's free and released under MIT License Copyright (c) 2018 Yusuf Shakeel
<!DOCTYPE html>
<html>
<head>
<title>dyCacheJS Demo</title>
</head>
<body>
<!-- some html goes here -->
<!-- script -->
<script src="path/to/dyCache.min.js"></script>
</body>
</html>
Now, instantiate an object of the dyCache
class.
var obj = new dyCache();
Note!
Output represents the state of the cache after the execution of the last method.
set(key, value)
This will create a key in the cache and will set a value.
The value
can be a number, string, array or object.
If the key
exists in the cache then it is overwritten.
obj.set('num', 10); // setting numeric value
obj.set('str', 'Yusuf Shakeel'); //setting string value
obj.set('user', { username: 'yusufshakeel', points: 10 }); // setting object value
obj.set('arr', [1, 2, 3]); // setting array value
get(key)
This will fetch the value of a given key in the cache.
If key
does not exists then it will return undefined
.
obj.get('num');
obj.get('str');
obj.get('user');
obj.get('arr');
length()
This will give the total number of keys in the cache.
Returns a number.
obj.length();
keys()
This will give all the keys in the cache.
Returns an array.
obj.keys();
del(key)
This will delete a key from the cache.
obj.del('num');
exists(key)
This checks if a given key exists in the cache.
Returns boolean value: true/false.
obj.exists('num');
obj.exists('str');
purge()
This will remove all the keys from the cache.
obj.purge();
arrInit(key)
This will create a new array referred by key
in the cache.
If array referred by key
exists in the cache then it is overwritten.
obj.arrInit('users'); // create a new array by the name users
arrPush(key, value)
This will create a key
(if not exists) in the cache
which is an array and will push the value
from the right side of the array.
value
can be a number, string, object or array.
obj.arrPush('users', {username: 'yusufshakeel', points: 10}); // push object
obj.arrPush('users', [1, 2, 3]); // push array
obj.arrPush('users', 9999); // push number
obj.arrPush('users', 'Hello World'); // push string
arrMPush(key, value)
This will push multiple values in an array referred by the
key
in the cache.
value
will be pushed from the right side.
If key
does not exists then it will be created.
value
is an array of elements. Where, an element can be a number, string, object or array.
obj.arrMPush('users', [100, 'superman', ['a1', 'b2', 200], { id: 10, points: 20}]);
arrLPush(key, value)
This will create a key
(if not exists) in the cache
which is an array and will push the value
from the left side of the array.
value
can be a number, string, object or array.
obj.arrLPush('users', {username: 'qwerty', points: 50});
arrMLPush(key, value)
This will push multiple values in an array referred by the
key
in the cache.
value
will be pushed from the left side.
If key
does not exists then it will be created.
value
is an array of elements. Where, an element can be a number, string, object or array.
obj.arrMLPush('users', [1, 'hi', [1, 3], { m: 1 }]);
arrGet(key, [index, end])
This will return all the elements in the array referred by
the given key
in the cache.
If the key
does not exists then return null
.
index
is optional. If set then return the element at the given index.
end
is optional. If set then return the element from start index
to
end
index.
Note! Array indexing starts from 0.
obj.arrGet('users'); // this will return all elements in the array referred by users key
obj.arrGet('unknown'); // this will return null
obj.arrGet('users', 1); // this will return element at index 1
obj.arrGet('users', 1, 3); // this will return element from index 1 to 3
arrLength(key)
This will return the total number of elements in the array
referred by key
in the cache.
If the key
does not exists in the cache then -1
is returned.
obj.arrLength('users');
obj.arrLength('unknown'); // this will return -1 as key does not exists in the cache
arrPop(key)
This will pop an element from the right side of an array referred by given
key
in the cache.
If the key
does not exists in the cache then null
is returned.
To avoid ambiguity check if the key exists and total number of elements in the array.
obj.arrPop('users');
arrLPop(key)
This will pop an element from the left side of an array referred by given
key
in the cache.
If the key
does not exists in the cache then null
is returned.
To avoid ambiguity check if the key exists and total number of elements in the array.
obj.arrLPop('users');
arrInsertAt(key, index, value)
This will insert a value
at a given index
in an array referred by key
in the cache.
value
can be a number, string, array or object.
On success return true. Otherwise false.
obj.arrInsertAt('users', 3, {username: 'foo', points: 99});
arrMInsertAt(key, index, value)
This will insert multiple value
at a given index
in an array referred by key
in the cache.
index
is the index at which the new elements are inserted.
value
is an array of elements.
Each element can be a number, string, array or object.
On success return true. Otherwise false.
obj.arrMInsertAt('users', 1, [ {id: 9999, name: 'yusufshakeel'}, 10, 'happy', [100] ]);
arrUpdateElem(key, index, value)
This will update the value of an element at given index
in the array referred by the key
in the cache.
On success return true. Otherwise false.
Note! Array indexing starts from 0.
obj.arrUpdateElem('users', 0, { username: 'tintin', points: 50 }); // this will return true
arrDeleteElem(key, index)
This will delete element at given index
of the array referred by the
key
in the cache.
On success it will return the deleted element in an array. Otherwise, false.
obj.arrDeleteElem('users', 0); // delete element at index 0
arrDeleteElems(key, start, [deleteCount])
This will delete deleteCount
number of elements from the
index start
of the array referred by the
key
in the cache.
deleteCount
is optional. If not present then delete till the end.
On success it will return the deleted elements in an array. Otherwise, false.
obj.arrDeleteElems('users', 2, 3); // delete 3 elements from index 2
obj.arrDeleteElems('users', 6); // delete all elements from index 6 till end
obj.arrDeleteElems('users', 0, 3); // delete 3 elements from the start index
oInit(key)
This will create a new object in the cache by the name key
.
If object referred by key
exists in the cache then it will be overwritten.
obj.oInit('players'); // creates a new object players
oSet(key, oKey, oValue)
This will create an object in the cache by the name key
and will add a property oKey
having value oValue
.
If oKey
exists in the key
then it will be overwritten.
oValue
can be a number, string, array or object.
obj.oSet('players', 'p1', { id: 'p1', username: 'yusufshakeel' });
obj.oSet('players', 'p2', { id: 'p2', username: 'dawoodshakeel' });
oGet(key, oKey)
This will fetch the value saved in the key oKey
inside the object referred by key
in the cache.
If key
does not exists then it will return undefined
.
If oKey
does not exists then it will return undefined
.
obj.oGet('players', 'p1');
obj.oGet('players', 'unknown'); // return undefined
oGetAll(key)
This will fetch all values saved in the
given key
of the cache.
If key
does not exists then return undefined
.
obj.oGetAll('players');
oGetKeys(key)
This will return all the oKey
in the object
referred by key
in the cache.
On success return an array containing all the oKey. Otherwise, false.
obj.oGetKeys('players');
oExists(key, oKey)
This will check if the oKey
exists in the object
referred by key
in the cache.
obj.oExists('players', 'p2'); // this will return true
obj.oExists('players', 'unknown'); // this will return false
oLength(key)
This will return the total number of oKey
in the
object referred by the key
in the cache.
obj.oLength('players');
obj.oLength('unknown'); // this will return -1
oDel(key, oKey)
This will delete the key oKey
from the object
referred by key
in the cache.
Returns boolean value.
On success return true. Otherwise false.
obj.oDel('players', 'p2'); // this will return true
stackInit(key)
This will initialise a stack referred by key
in the cache.
obj.stackInit('myStack');
stackExists(key)
This will check if the stack referred by key
exists in the cache.
Return true
if stack exists. Otherwise, false
.
obj.stackExists('myStack');
stackPush(key, value)
This will push a value
in the stack referred by
key
in the cache.
value
can be a number, string, array or object.
obj.stackPush('myStack', 10); // push number
obj.stackPush('myStack', 'Yusuf Shakeel'); // push string
obj.stackPush('myStack', [1, 2]); // push array
obj.stackPush('myStack', { a: 10 }); // push object
stackPeek(key)
This will return the top element of the stack referred by
key
in the cache.
Will return null
if stack does not exists.
obj.stackPeek('myStack');
stackPop(key)
This will pop the top element from the stack referred by
key
in the cache.
Will return undefined
if stack does not exists.
obj.stackPop('myStack');
stackLength(key)
This will return total number of elements in the stack referred by
key
in the cache.
On success return number of elements in the stack. Otherwise -1.
obj.stackLength('myStack');
stackIsEmpty(key)
This will check is the stack referred by
key
in the cache is empty.
If empty will return true
. Otherwise, false
.
If key
does not exists then return undefined
.
obj.stackIsEmpty('myStack');
stackPurge(key)
This will empty the stack referred by
key
in the cache.
Will return true
on success. Otherwise, false
.
obj.stackPurge('myStack');
stackDelete(key)
This will delete the stack referred by
key
from the cache.
Will return true
on success. Otherwise, false
.
obj.stackDelete('myStack');
queueInit(key)
This will initialise a queue referred by key
in the cache.
obj.queueInit('myQueue');
queueExists(key)
This will check if the queue referred by key
exists in the cache.
Return true
if queue exists. Otherwise, false
.
obj.queueExists('myQueue');
enqueue(key, value)
This will insert a value
from the right side
in the queue referred by key
in the cache.
value
can be a number, string, array or object.
obj.enqueue('myQueue', 10); // enqueue number
obj.enqueue('myQueue', 'Yusuf Shakeel'); // enqueue string
obj.enqueue('myQueue', [1, 2]); // enqueue array
obj.enqueue('myQueue', { a: 10 }); // enqueue object
dequeue(key)
This will remove the first element from the left side
of the queue referred by key
in the cache.
Will return null
if queue does not exists.
obj.dequeue('myQueue');
queueLPeek(key)
This will return the first element from the left side of the
queue referred by key
in the cache.
Will return null
if queue does not exists.
obj.queueLPeek('myQueue');
queueRPeek(key)
This will return the last element from the right side of the
queue referred by key
in the cache.
Will return null
if queue does not exists.
obj.queueRPeek('myQueue');
queueLength(key)
This will return total number of elements in the queue referred by
key
in the cache.
On success return number of elements in the queue. Otherwise -1.
obj.queueLength('myQueue');
queueIsEmpty(key)
This will check is the queue referred by
key
in the cache is empty.
If empty will return true
. Otherwise, false
.
If key
does not exists then return undefined
.
obj.queueIsEmpty('myQueue');
queuePurge(key)
This will empty the queue referred by
key
in the cache.
Will return true
on success. Otherwise, false
.
obj.queuePurge('myQueue');
queueDelete(key)
This will delete the queue referred by
key
from the cache.
Will return true
on success. Otherwise, false
.
obj.queueDelete('myQueue');
Copyright © Yusuf Shakeel
MIT License