Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
shanliu committed Nov 29, 2019
1 parent 61fb66c commit 246bf0c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/driver-mysqli/classes/LSYS/Database/MYSQLi.php
Expand Up @@ -192,7 +192,7 @@ public function query($sql,array $value=[],array $value_type=[]){
*/
public function exec($sql,array $value=[],array $value_type=[]){
try{
return parent::query($sql,$value,$value_type);
return parent::exec($sql,$value,$value_type);
}catch (Exception $e){//unlink connect reset transaction status
if ($this->getConnectManager()->isUnConnect($e->getCode())) {
$this->in_transaction=false;
Expand Down
Expand Up @@ -11,22 +11,42 @@ class Memcache implements Cache{
protected $memcache;
protected $prefix;
protected $delayed;
public function __construct($delayed=10,\LSYS\Memcache $memcache=null,$prefix='db_master'){
$this->memcache=$memcache?$memcache:\LSYS\Memcache\DI::get()->memcache();
protected $log;
/**
* @param number $delayed
* @param \LSYS\Memcache $memcache
* @param string $prefix
* @param callable $log error report callback (\LSYS\Exception $e)
*/
public function __construct($delayed=10,\LSYS\Memcache $memcache=null,$prefix='db_master',callable $log=null){
$this->memcache=$memcache;
$this->prefix=$prefix;
$this->delayed=$delayed;
$this->log=$log;
}
protected function memcache(){
if (!is_object($this->memcache))$this->memcache=\LSYS\Memcache\DI::get()->memcached();
try{
$this->memcache->configServers();
}catch (\LSYS\Exception $e){
is_callable($this->log)&&call_user_func($this->log,$e);
return;
}
return $this->memcache;
}
public function time(array $table){
$this->memcache->configServers();
$memcache=$this->memcache();
if (!is_object($memcache)) return true;
foreach ($table as $v){
if(intval($this->memcache->get($this->prefix.$v))>time())return true;
if(intval($memcache->get($this->prefix.$v))>time())return true;
}
}
public function save(array $table){
$this->memcache->configServers();
$memcache=$this->memcache();
if (!is_object($memcache)) return;
$delayed=$this->delayed();
foreach ($table as $v){
$this->memcache->set($this->prefix.$v,time()+$delayed,0,$delayed);
$memcache->set($this->prefix.$v,time()+$delayed,0,$delayed);
}
}
public function delayed(){
Expand Down
Expand Up @@ -11,22 +11,42 @@ class Memcached implements Cache{
protected $memcache;
protected $prefix;
protected $delayed;
public function __construct($delayed=10,\LSYS\Memcached $memcache=null,$prefix='db_master'){
$this->memcache=$memcache?$memcache:\LSYS\Memcached\DI::get()->memcached();
protected $log;
/**
* @param number $delayed
* @param \LSYS\Memcached $memcache
* @param string $prefix
* @param callable $log @param callable $log error report callback (\LSYS\Exception $e)
*/
public function __construct($delayed=10,\LSYS\Memcached $memcache=null,$prefix='db_master',callable $log=null){
$this->memcache=$memcache;
$this->prefix=$prefix;
$this->delayed=$delayed;
$this->log=$log;
}
protected function memcache(){
if (!is_object($this->memcache))$this->memcache=\LSYS\Memcached\DI::get()->memcached();
try{
$this->memcache->configServers();
}catch (\LSYS\Exception $e){
is_callable($this->log)&&call_user_func($this->log,$e);
return;
}
return $this->memcache;
}
public function time(array $table){
$this->memcache->configServers();
$memcache=$this->memcache();
if (!is_object($memcache)) return true;
foreach ($table as $v){
if(intval($this->memcache->get($this->prefix.$v))>time())return true;
if(intval($memcache->get($this->prefix.$v))>time())return true;
}
}
public function save(array $table){
$this->memcache->configServers();
$memcache=$this->memcache();
if (!is_object($memcache)) return;
$delayed=$this->delayed();
foreach ($table as $v){
$this->memcache->set($this->prefix.$v,time()+$delayed,$delayed);
$memcache->set($this->prefix.$v,time()+$delayed,$delayed);
}
}
public function delayed(){
Expand Down
Expand Up @@ -11,24 +11,44 @@ class Redis implements Cache{
protected $redis;
protected $key;
protected $delayed;
public function __construct($delayed=10,\LSYS\Redis $redis=null,$key='db_master'){
protected $log;
/**
* @param number $delayed
* @param \LSYS\Redis $redis
* @param string $key
* @param callable $log error report callback (\LSYS\Exception $e)
*/
public function __construct($delayed=10,\LSYS\Redis $redis=null,$key='db_master',callable $log=null){
$this->delayed=$delayed;
$this->redis=$redis?$redis:\LSYS\Redis\DI::get()->redis();
$this->key=$key;
$this->redis=$redis;
$this->log=$log;
}
protected function redis(){
if (!is_object($this->redis))$this->redis=\LSYS\Redis\DI::get()->redis();
try{
$this->redis->configConnect();
}catch (\LSYS\Exception $e){
is_callable($this->log)&&call_user_func($this->log,$e);
return;
}
return $this->redis;
}
public function time(array $table){
$this->redis->configConnect();
$val=$this->redis->hMGet($this->key,$table);
$redis=$this->redis();
if(!is_object($redis))return true;
$val=$redis->hMGet($this->key,$table);
if (is_array($val)){
foreach ($val as $v){
if (intval($v)>time())return true;
}
}
}
public function save(array $table){
$this->redis->configConnect();
$redis=$this->redis();
if(!is_object($redis))return ;
$data=array_combine($table, array_fill(0, count($table), time()+$this->delayed()));
return $this->redis->hmSet($this->key,$data);
return $redis->hmSet($this->key,$data);
}
public function delayed(){
return $this->delayed;
Expand Down

0 comments on commit 246bf0c

Please sign in to comment.