<?php
namespace eapie\engine;
class redis extends \eapie\engine\init {
static private $_redis = NULL;
public function __construct(){
if( empty(self::$_redis) ){
if( !class_exists("Redis") ){
self::$_redis = NULL;
return;
}
self::$_redis = new \Redis();
self::$_redis->connect('127.0.0.1', 6379);
self::$_redis->auth('')
}
}
public function __destruct () {
if( !empty(self::$_redis) ){
self::$_redis->close();
}
}
public function set_redis_key($project = "", $key = ""){
if( empty(self::$_redis) ){
return false;
}
if( empty($project) ||
(!is_string($project) && !is_numeric($project) ) ||
empty($key) ||
(!is_string($key) && !is_numeric($key)) ){
return false;
}
$project_keys = self::$_redis->get($project);
$project_keys = cmd(array($project_keys), 'json decode');
if( !empty($project_keys) && is_array($project_keys)){
if( in_array($key, $project_keys) ){
return true }else{
$project_keys[] = $key;
}
}else{
$project_keys = array($key);
}
return self::$_redis->set($project, cmd(array($project_keys), 'json encode') );
}
public function get_redis_key($project = ""){
return $this->get_array($project);
}
public function redis_object(){
if( empty(self::$_redis) ){
return false;
}
return self::$_redis;
}
public function setex_array( $key = "", $s = 30, $value = array() ){
if( empty(self::$_redis) ){
return false;
}
if( empty($key) ||
!is_string($key) ||
empty($value) ||
!is_array($value) ){
return false;
}
return self::$_redis->setex($key, $s, cmd(array($value), 'json encode'));
}
public function set_array( $key = "", $value = array() ){
if( empty(self::$_redis) ){
return false;
}
if( empty($key) ||
!is_string($key) ||
empty($value) ||
!is_array($value) ){
return false;
}
return self::$_redis->set($key, cmd(array($value), 'json encode'));
}
public function get_array($key = ""){
if( empty(self::$_redis) ){
return false;
}
if( empty($key) ||
(!is_string($key) && !is_numeric($key)) ){
return false;
}
$value = self::$_redis->get($key);
$value = cmd(array($value), 'json decode');
if( empty($value) || !is_array($value) ){
return false;
}
return $value;
}
public function psetex_array( $key = "", $ms = 30, $value = ""){
if( empty(self::$_redis) ){
return false;
}
if( empty($value) || !is_array($value) ){
return false;
}
return self::$_redis->psetex($key, $ms, cmd(array($value), 'json encode'));
}
public function delete($key = ""){
set_time_limit(0) ini_set('memory_limit', '-1')
if( empty(self::$_redis) ){
return false;
}
if( (!is_array($key) && !is_string($key) && !is_numeric($key)) ||
( is_string($key) && "" == $key ) ||
( is_array($key) && empty($key)) ){
self::$_redis->flushAll();
}else{
self::$_redis->del($key);
}
return true;
}
}
?>