-
Notifications
You must be signed in to change notification settings - Fork 0
/
Memcache.php
55 lines (55 loc) · 1.79 KB
/
Memcache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
* lsys database
* @author Lonely <shan.liu@msn.com>
* @copyright (c) 2017 Lonely <shan.liu@msn.com>
* @license http://www.apache.org/licenses/LICENSE-2.0
*/
namespace LSYS\Database\SlaveQueryCheck\Cache;
use LSYS\Database\SlaveQueryCheck\Cache;
class Memcache implements Cache{
protected $memcache;
protected $prefix;
protected $delayed;
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){
$memcache=$this->memcache();
if (!is_object($memcache)) return true;
foreach ($table as $v){
if(intval($memcache->get($this->prefix.$v))>time())return true;
}
}
public function save(array $table){
$memcache=$this->memcache();
if (!is_object($memcache)) return;
$delayed=$this->delayed();
foreach ($table as $v){
$memcache->set($this->prefix.$v,time()+$delayed,0,$delayed);
}
}
public function delayed(){
return $this->delayed;
}
}