Skip to content

Instantly share code, notes, and snippets.

@Charlie-Lucas
Last active January 11, 2016 08:48
Show Gist options
  • Save Charlie-Lucas/15a4fae81912b583b200 to your computer and use it in GitHub Desktop.
Save Charlie-Lucas/15a4fae81912b583b200 to your computer and use it in GitHub Desktop.
<?php
new Snc\RedisBundle\SncRedisBundle(),
snc_redis:
clients:
victoire:
type: predis
alias: victoire
dsn: redis://localhost
<?php
public function create(MonObjet $object)
{
$hash = "reference:" . $object->getId();
if($this->redis->exists($hash))
{
throw new \Exception('An redis hash "' . $hash . ' already exist');
}
$values = $this->transformObjectToArray($object);
$valuesReadyToRedis = $this->redislizeArray($array);
$this->redis->hmset($hash, $valuesReadytoRedy);
}
<?php
public function getById($id)
{
$hash = "reference:" . $id;
return $this->unredislizeArray($this->redis->hgetall($hash));
}
<?php
public function redislize($data)
{
// Only serialize if it's not a string or a integer
if(!is_string($data) && !is_integer($data))
{
return urlencode(serialize($data));
}
// Encode string to escape wrong saves
return urlencode($data);
}
<?php
public function unredislize($data)
{
$data = urldecode($data);
// unserialize data if we can
$unserializedData = @unserialize($data);
if($unserializedData !== false)
{
return $unserializedData;
}
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment