Created
May 25, 2018 13:50
-
-
Save jonathansanchez/e2bfcefdd86ec98b91d0c405b4fb1aa3 to your computer and use it in GitHub Desktop.
Basic Example Predis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require "predis/autoload.php"; | |
PredisAutoloader::register(); | |
try { | |
$redis = new PredisClient(); | |
/** | |
* Por defecto conecta así, por lo que no es necesario agregar esta info. | |
* | |
$redis = new PredisClient(array( | |
"scheme" => "tcp", | |
"host" => "127.0.0.1", | |
"port" => 6379)); | |
*/ | |
//Por ejemplo una llamada para obtener los post | |
if( !$posts = unserialize($redis->get("post:posts")) ) { | |
$posts = $this->postRepository->findAll(); //Obtengo todos los post de DB, Web Service etc. | |
if ( empty($posts) ) { | |
throw new \Exception('Posts no encontrados!'); | |
} | |
$redis->set("post:posts", serialize($posts)); //Serializamos y guardamos en Redis | |
$redis->expire("post:posts", 90); //Los almacenamos por 90 segundos (o lo que queramos) | |
} | |
return $posts; | |
} catch (\Exception $e) { | |
echo "No conectado a Redis!"; | |
echo $e->getMessage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment