Simple Redis Abstraction Layer
Install library via composer
composer require tsk/simple-redis-dal
Simple initialization
$db = new hooli("127.0.0.0", "password");
Simple check
$db->ping();
Set a value
$key = "username";
$db->setvalue($key, "MaryPoppins");
Get the value
$username = $db->getvalue($key);
Delete a value
$key = "username";
$db->delete($key);
Cache a value
$key = "session_id";
$value = "12345";
$time = 20 // Time is in minutes
$db->cachevalue($key, $value, $time);
Expire a value
$key = "session_id";
$time = 1 // Time in minutes
$db->expire($key, $time);
Create a hash / Store data into a hash (record types structured as collections of field-value pairs)
$data = [
"firstName" => "Mary",
"lastName" => "Poppins",
"email" => "marypoppins@email.com",
"location" => "USA, New York"
];
$HashName = 'marypoppins@email.com';
$db->createhash($HashName, $data);
Check if a value exists in a hash
$HashName = 'marypoppins@email.com';
$db->checkhash($HashName, "firstName");
Update / replace a single value in a hash
$HashName = 'marypoppins@email.com';
$db->replacehash($HashName, "location", "Nigeria, Lagos");
Insert new value into hash
$HashName = 'marypoppins@email.com';
$db->inserthash($HashName, "age", 32);
Fetch all values in a single hash
$HashName = 'marypoppins@email.com';
$db->gethash($HashName);
or using return types
$db->json()->gethash($HashName);
Count the keys in a hash
$HashName = 'marypoppins@email.com';
$count = $db->countkeys($HashName);
Increase a value in a hash
$HashName = 'marypoppins@email.com';
$key = 'age'; // The value of this key must be an integer
$increaseBy = 4;
$db->increase($HashName, $key); // This method increases the value by one
$db->increaseby($HashName, $key, $increaseBy); // This method increases the value by the specified number
Insert value into a list
$List = "users";
$db->insertlist($List, "something@email.com");
search for a value inside a list
$List = "users";
if($db->search_list($List, "something@email.com")){
echo 'value exists';
}
Count number of values /entries in a List
$List = "users";
$db->countlist($List);
Delete a List
$List = "users";
$db->deletelist($List);
Return all values in a List as JSON return type
$List = "users";
$db->json()->getlist($List);
Get a specific number of elements from a list
$List = "users";
$num = 10;
$users = $db->getlistnum($List, $num); // Returns an array
Save hashes to list (HashList) while mapping each value in list to key in hash
$data = [
"firstName" => "Mary",
"lastName" => "Poppins",
"email" => "marypoppins@email.com",
"location" => "USA, New York"
];
$list = "users";
$userID = "mary123";
$db->inserthashlist($userID, $data, $list);
Return all data in a HashList as json
$list = "users";
echo $db->gethashlist($list);
list Name of the Redis list num is asigned the number of keys to be fetched
echo $db->getlistnum($list, $num)
list Name of the Redis list num reperesents the number of hashes to fetch from a list
echo $db->gethashlistnum($list, $num)
Tim3k |
Joshua Kusaasira |
Marshud |