PHP code example of codemix / yiiredis

1. Go to this page and download the library: Download codemix/yiiredis library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codemix / yiiredis example snippets


"components" => array(
	"redis" => array(
		"class" => "packages.redis.ARedisConnection",
		"hostname" => "localhost",
		"port" => 6379,
		"database" => 1,
		"prefix" => "Yii.redis."
	),
	...
),

Yii::app()->redis->getClient()->set("myKey", "Your Value");
echo Yii::app()->redis->getClient()->get("myKey"); // outputs "Your Value"
Yii::app()->redis->getClient()->del("myKey"); // deletes the key

$list = new ARedisList("aNameForYourListGoesHere");
$list->add("cats");
$list->add("dogs");
$list->add("fish");
foreach($list as $i => $val) {
	echo $val."<br />";
}
$list->clear(); // delete the list

$set = new ARedisSet("aNameForYourSet");
$set->add(1);
$set->add(2);
$set->add(3);
echo $set->count; // outputs 3
$set->add(3);
echo $set->count; // still 3, cannot add the same value more than once
foreach($set as $val) {
	echo $val."<br />";
}

$sortedSet = new ARedisSortedSet("aNameForYourSortedSet");
$sortedSet->add("myValue", 0.4);
$sortedSet->add("myOtherValue", 0.8);
$sortedSet->add("myOtherOtherValue", 0.9);
foreach($sortedSet as $key => $score) {
	echo $key.": ".$score."<br />";
}

$hash = new ARedisHash("myHashNameHere");
$hash->whatever = "someValue";
$hash->greeting = "hello world";

echo $hash->count; // outputs 2

$channel = new ARedisChannel("myChan");
$channel->onReceiveMessage = function($redis, $channel, $message) {
	echo "Message Received: ".$message."\n";
}
$channel->publish("hello world"); // sends a messsage to the channel
$channel->subscribe(); // subscribes to the channel and listens to messages, blocks the process

$counter = new ARedisCounter("totalPageViews");
$counter->increment();
echo $counter->getValue();

$mutex = new ARedisMutex("someOperation");
$mutex->block(); // blocks execution until the resource becomes available
// do something
$mutex->unlock(); // release the lock

"components" => array(
	"cache" => array(
		"class" => "packages.redis.ARedisCache"
	),
	...
),

$record = ARedisRecord::model()->findByPk(1); // loads a record with a unique id of 1
$record->name = "a test name"; // sets the name attribute on the record
$record->somethingElse = "some other value";
$record->save(); // saves the record to redis
$record->delete(); // deletes the record from redis