Jump to content

Redis in PHP - Guide and Examples + Requests


FastPoint Gaming

Recommended Posts

Hi Everyone,

As there is not a great deal of option when it comes to managing your redis data (Hence why we are building a custom database management panel - as well as a few other people) I'm going to show you how to both get information from your redis database and manipulate it.


This will be helpful for those who wish to build a unique managment system for their servers. Be that very simple or very complex.

I will also be taking request and offering help to people who wish to get certain results from their queries.



===================================

Requirments:
PHP capable enviroment (I suggest MAMP/Xampp/Wamp to create a local web server)
Redis PHP Client (I will be using TinyRedisClient as it is very lightweight)
General knowledge of PHP language as well as good arithmetic
Redis Database

===================================

Example One - Get the total amount of players

function uniqueUsers() {
	$client = new TinyRedisClient( 'YourIP:YourPort' );
	$value = $client->GET( 'PLAYERS:YourPort' );
	$object = json_decode($value, true);
	$number = count($object);
	return $number;
}

Breakdown

Declare a fucntion and name it uniqueUsers

function uniqueUsers() {
}

Connect to Redis Database

function uniqueUsers() {
	$client = new TinyRedisClient( 'YourIP:YourPort' );
}

Perform a GET request for the Key Called YourIP:Port

function uniqueUsers() {
	$client = new TinyRedisClient( 'YourIP:YourPort' );
	$value = $client->GET( 'PLAYERS:YourPort' );
}

This will return a JSON string. We now need to decode this using the json_decode() function like so

function uniqueUsers() {
	$client = new TinyRedisClient( 'YourIP:YourPort' );
	$value = $client->GET( 'PLAYERS:YourPort' );
	$object = json_decode($value, true);
}

This will seperate the JSON string into an array called $object. Now we count how many items are in the $objects array to get the total amount of players

function uniqueUsers() {
	$client = new TinyRedisClient( 'YourIP:YourPort' );
	$value = $client->GET( 'PLAYERS:YourPort' );
	$object = json_decode($value, true);
	$number = count($object);
}

Now all that is left to do is return the value of $number to a variable that we stored the returned value from the function in.

function uniqueUsers() {
	$client = new TinyRedisClient( 'YourIP:YourPort' );
	$value = $client->GET( 'PLAYERS:YourPort' );
	$object = json_decode($value, true);
	$number = count($object);
        return $number;
}

NOTE: To assign the returned value to a variable you must declare the variable and call the function like so:

$uniquePlayers = uniqueUsers();

===================================

Example Two - Get Most Common Vehicle (Class Name)


function mostPopularVehicle () {
	$client = new TinyRedisClient( 'YourIP:YourPort' );
	$value = $client->KEYS( 'Vehicle:YourPort*' );
	$number = count($value);
	for ($x = 0; $x <= $number; $x++) {
		$value = $client->GET( 'Vehicle:YourPort:'.$x );
		if ($value != '') {
			$vehicle = json_decode($value, true);
			if (isset($vehicle[0])) {
				$vehicles[$x] = $vehicle[0];
			}
		}
	}
	$counted = array_count_values($vehicles);
 	arsort($counted);
 	return(key($counted));
}

Breakdown

 

  Declare mostPopularVehicle function

function mostPopularVehicle () {

}

Connect to Redis database

function mostPopularVehicle () {
	$client = new TinyRedisClient( 'YourIP:YourPort' );
}

Get all KEYS that match the pattern "Vehicle:YourPort:"

function mostPopularVehicle () {
	$client = new TinyRedisClient( 'YourIP:YourPort' );
	$value = $client->KEYS( 'Vehicle:YourPort*' );
}

Count the result to get the total amount of vehicles

function mostPopularVehicle () {
	$client = new TinyRedisClient( 'YourIP:YourPort' );
	$value = $client->KEYS( 'Vehicle:YourPort*' );
	$number = count($value);
}

Loop for the number of vehicles

function mostPopularVehicle () {
	$client = new TinyRedisClient( 'YourIP:YourPort' );
	$value = $client->KEYS( 'Vehicle:YourPort*' );
	$number = count($value);
	for ($x = 0; $x <= $number; $x++) {

        }
}

GET the vehicle number $x (For each number in the total number of cars)

function mostPopularVehicle () {
	$client = new TinyRedisClient( 'YourIP:YourPort' );
	$value = $client->KEYS( 'Vehicle:YourPort*' );
	$number = count($value);
	for ($x = 0; $x <= $number; $x++) {
		$value = $client->GET( 'Vehicle:YourPort:'.$x );
        }
}

Check if the car value is not blank

function mostPopularVehicle () {
	$client = new TinyRedisClient( 'YourIP:YourPort' );
	$value = $client->KEYS( 'Vehicle:YourPort*' );
	$number = count($value);
	for ($x = 0; $x <= $number; $x++) {
		$value = $client->GET( 'Vehicle:YourPort:'.$x );
		if ($value != '') {

		}
	}
}

Decode the JSON string

function mostPopularVehicle () {
	$client = new TinyRedisClient( 'YourIP:YourPort' );
	$value = $client->KEYS( 'Vehicle:YourPort*' );
	$number = count($value);
	for ($x = 0; $x <= $number; $x++) {
		$value = $client->GET( 'Vehicle:YourPort:'.$x );
		if ($value != '') {
			$vehicle = json_decode($value, true);
		}
	}
}

Check if the decoded JSON string Index 0 is set

function mostPopularVehicle () {
	$client = new TinyRedisClient( 'YourIP:YourPort' );
	$value = $client->KEYS( 'Vehicle:YourPort*' );
	$number = count($value);
	for ($x = 0; $x <= $number; $x++) {
		$value = $client->GET( 'Vehicle:YourPort:'.$x );
		if ($value != '') {
			$vehicle = json_decode($value, true);
			if (isset($vehicle[0])) {

			}
		}
	}
}

If the index is set, vehicles index of $x variables (Each car for the total of cars) = the vehicle class name (Index 0 of the JSON string)

function mostPopularVehicle () {
	$client = new TinyRedisClient( 'YourIP:YourPort' );
	$value = $client->KEYS( 'Vehicle:YourPort*' );
	$number = count($value);
	for ($x = 0; $x <= $number; $x++) {
		$value = $client->GET( 'Vehicle:YourPort:'.$x );
		if ($value != '') {
			$vehicle = json_decode($value, true);
			if (isset($vehicle[0])) {
				$vehicles[$x] = $vehicle[0];
			}
		}
	}
}

Count all the values in the $vehicles array

function mostPopularVehicle () {
	$client = new TinyRedisClient( 'YourIP:YourPort' );
	$value = $client->KEYS( 'Vehicle:YourPort*' );
	$number = count($value);
	for ($x = 0; $x <= $number; $x++) {
		$value = $client->GET( 'Vehicle:YourPort:'.$x );
		if ($value != '') {
			$vehicle = json_decode($value, true);
			if (isset($vehicle[0])) {
				$vehicles[$x] = $vehicle[0];
			}
		}
	}
	$counted = array_count_values($vehicles);
}

Sort the counted array but maintain index association 

function mostPopularVehicle () {
	$client = new TinyRedisClient( 'YourIP:YourPort' );
	$value = $client->KEYS( 'Vehicle:YourPort*' );
	$number = count($value);
	for ($x = 0; $x <= $number; $x++) {
		$value = $client->GET( 'Vehicle:YourPort:'.$x );
		if ($value != '') {
			$vehicle = json_decode($value, true);
			if (isset($vehicle[0])) {
				$vehicles[$x] = $vehicle[0];
			}
		}
	}
	$counted = array_count_values($vehicles);
 	arsort($counted);
}

Return the value, in this case is the most frequent occurring vehicle in the databse

function mostPopularVehicle () {
	$client = new TinyRedisClient( 'YourIP:YourPort' );
	$value = $client->KEYS( 'Vehicle:YourPort*' );
	$number = count($value);
	for ($x = 0; $x <= $number; $x++) {
		$value = $client->GET( 'Vehicle:YourPort:'.$x );
		if ($value != '') {
			$vehicle = json_decode($value, true);
			if (isset($vehicle[0])) {
				$vehicles[$x] = $vehicle[0];
			}
		}
	}
	$counted = array_count_values($vehicles);
 	arsort($counted);
 	return(key($counted));
}

===================================

 

More coming Soon

Link to comment
Share on other sites

  • 2 weeks later...

I use Windows Server 2008, not linux.

 

@FastPointGaming.com

 

I did this:

            <?php
            function uniqueUsers() {
                $client = new TinyRedisClient( 'localhost:6379' );
                $value = $client->GET( 'PLAYERS:6379' );
                $object = json_decode($value, true);
                $number = count($object);
                    return $number;
            }
            $uniquePlayers = uniqueUsers();
            echo $uniquePlayers;
            ?>

and thats it. Changed nothing in TinyRedisClient.php

Link to comment
Share on other sites

I use Windows Server 2008, not linux.

 

@FastPointGaming.com

 

I did this:

            <?php
            function uniqueUsers() {
                $client = new TinyRedisClient( 'localhost:6379' );
                $value = $client->GET( 'PLAYERS:6379' );
                $object = json_decode($value, true);
                $number = count($object);
                    return $number;
            }
            $uniquePlayers = uniqueUsers();
            echo $uniquePlayers;
            ?>

and thats it. Changed nothing in TinyRedisClient.php

 

Can you send me the contents of your redis.conf as well?

Link to comment
Share on other sites

 

sure:

bind 127.0.0.1
maxmemory 4gb
save 900 1
save 300 10
save 60 1000
requirepass MYPASSWORD

 

Try this:

            <?php
            function uniqueUsers() {
                $client = new TinyRedisClient( 'localhost:6379' );
                $auth = $client->AUTH( 'MYPASSWORD' );
                $value = $client->GET( 'PLAYERS:6379' );
                $object = json_decode($value, true);
                $number = count($object);
                    return $number;
            }
            $uniquePlayers = uniqueUsers();
            echo $uniquePlayers;
            ?>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Discord

×
×
  • Create New...