174 lines
4.9 KiB
PHP
174 lines
4.9 KiB
PHP
<?php
|
||
|
||
|
||
|
||
/**
|
||
* Copyright (c) BestWebService.ru
|
||
* Author: Gvozdikov Victor Antonovich
|
||
* Редактирование кода запрещено. В случае распространения кода обязательно должен сохраняться этот copyright.
|
||
* Код распространяется как есть и не даёт гаранитий 100% стабильности. Автор не несёт ответственности за выполнения кода. Все права на код принадлежат Гвоздикову Виктор Антоновичу.
|
||
*/
|
||
namespace core\cache;
|
||
|
||
//*
|
||
// Показывать ли все ошибки
|
||
error_reporting(E_ERROR);
|
||
ini_set('display_errors',true);
|
||
/*/
|
||
ini_set('display_errors','Off');
|
||
/**/
|
||
|
||
|
||
class credis {
|
||
static $redis;
|
||
static function init() {
|
||
if(!self::$redis) {
|
||
$start = microtime(true);
|
||
self::$redis = $r = new \Redis();
|
||
$x = $r->connect('127.0.0.1', 6379);
|
||
echo '<br><b>ПОдключение к Redis <Br> ' . (microtime(true) - $start) . '</b><br>';
|
||
}
|
||
}
|
||
static function agetone($key,$field){
|
||
return self::$redis->hGet($key,$field);
|
||
}
|
||
static function aget($key){
|
||
return self::$redis->hGetAll($key);
|
||
}
|
||
static function aset($key,$value){
|
||
return self::$redis->hMSet($key,$value);
|
||
}
|
||
static function get($key){
|
||
return self::$redis->get($key);
|
||
}
|
||
static function set($key,$value){
|
||
return self::$redis->set($key,$value);
|
||
}
|
||
}
|
||
credis::init();
|
||
|
||
/*
|
||
include_once( $path = \Core::$FOLDER .'/lib/php/predis/autoload.php');
|
||
|
||
\Predis\Autoloader::register();
|
||
|
||
class Redis {
|
||
|
||
static protected $prefix = '';
|
||
|
||
static public function __include(){}
|
||
static protected $redis = null;
|
||
|
||
|
||
static function update_prefix($x){return $x."_update";}
|
||
static function needUpdate($key){ $key = self::update_prefix($key);self::set($key,true); }
|
||
static function updated($key){ $key = self::update_prefix($key);self::set($key,false); }
|
||
static function isNeedUpdate($key){
|
||
$key = self::update_prefix($key);
|
||
if(self::is($key)) {
|
||
return boolval( self::get($key) );
|
||
} else return true;
|
||
}
|
||
static function update($key,$value){
|
||
Redis::set($key,$value);
|
||
Redis::set(self::update_prefix($key),false);
|
||
return $value;
|
||
}
|
||
|
||
|
||
static public function is($key){
|
||
$res = false;
|
||
try {
|
||
$res = self::$redis->exists(self::$prefix.$key);
|
||
} catch ( Exception $e ) {};
|
||
return $res;
|
||
}
|
||
static public function ais($key,$field){
|
||
$res = false;
|
||
try {
|
||
$res = self::$redis->hexists(self::$prefix.$key,$field);
|
||
} catch ( Exception $e ) {};
|
||
return $res;
|
||
}
|
||
static public function aset($key,$value = array()){
|
||
try{
|
||
$s = self::$redis->hmset(self::$prefix.$key,$value);
|
||
} catch ( Exception $e ) {
|
||
echo $e->getMessage();
|
||
}
|
||
return $value;
|
||
}
|
||
static public function aget($key){
|
||
$res = null;
|
||
try{
|
||
//if(self::is($key)) {
|
||
$res = self::$redis->hgetall(self::$prefix.$key);
|
||
//}
|
||
} catch (Exception $e){$res = null;}
|
||
return $res;
|
||
}
|
||
static public function del($key){
|
||
$res = null;
|
||
try{
|
||
if(self::is($key)) {
|
||
$res = self::$redis->del(self::$prefix.$key);
|
||
}
|
||
} catch (Exception $e){$res = null;}
|
||
return $res;
|
||
}
|
||
|
||
static public function set($key,$value){
|
||
try {
|
||
self::$redis->set(self::$prefix.$key,$value);
|
||
} catch (Exception $e) {
|
||
}
|
||
return $value;
|
||
}
|
||
static public function get($key){
|
||
$res = null;
|
||
try {
|
||
if(self::is($key)) {
|
||
$res = self::$redis->get(self::$prefix.$key);
|
||
}
|
||
} catch (Exception $e) {}
|
||
return $res;
|
||
}
|
||
|
||
public static function init(){
|
||
//echo 'init';
|
||
try {
|
||
self::$prefix = $_SERVER['HTTP_HOST'].":";
|
||
self::$prefix = '';
|
||
$r = self::$redis = new \Predis\Client();
|
||
|
||
$r->set('a','b');
|
||
|
||
/*
|
||
if (!$r->isConnected()) {
|
||
$r = null;
|
||
self::$redis = null;
|
||
}
|
||
/**/
|
||
|
||
// This connection is for a remote server
|
||
/*
|
||
$redis = new PredisClient(array(
|
||
"scheme" => "tcp",
|
||
"host" => "153.202.124.2",
|
||
"port" => 6379
|
||
));
|
||
|
||
//echo 'Удалось соединиться';
|
||
}
|
||
catch (Exception $e) {
|
||
//die(iconv("windows-1251","UTF-8",$e->getMessage()));
|
||
//echo 'Не удалось соединиться';
|
||
self::$redis = null;
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
Redis::init();
|
||
|
||
*/ |