Commit 7abb515a authored by Shawn Barratt's avatar Shawn Barratt
Browse files

init

parent cd15b665
{
"name": "ethereal/simpleCache",
"type": "project",
"require": {
"php": ">=5.3.9",
},
"require-dev": {
"php": ">=5.3.9",
"phpunit/phpunit": "~4.8",
},
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"Ethereal\\": "lib"
}
}
}
<?php
namespace Ethereal;
use Predis\Client;
class Cache extends Predis\Client
{
protected $namespace = '';
public function set($key, $val, $exp = null)
{
if (!is_string($key)) {
throw new \Exception("Key must be a string");
}
$key = "{$this->namespace}{$key}";
if (!is_string($val)) {
$val = json_encode($val);
}
if ($exp && is_numeric($exp)) {
return parent::set($key, $val, "EX {$exp}");
}
return parent::set($key, $val);
}
public function get($key)
{
if (!is_string($key)) {
throw new \Exception("Key must be a string");
}
$key = "{$this->namespace}{$key}";
$value = parent::get($key);
if (is_null($value)) {
return null;
}
if ($decode = json_decode($value)) {
return $decode;
}
return $value;
}
public function setNamespace($namespace)
{
$this->namespace = "{$namespace}";
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment