-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMemCache.php
More file actions
76 lines (73 loc) · 2.37 KB
/
Copy pathMemCache.php
File metadata and controls
76 lines (73 loc) · 2.37 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* This file contains MemCache class
*
* @author KoolPHP Inc (support@koolphp.net)
* @link https://www.koolphp.net
* @copyright KoolPHP Inc
* @license https://www.koolreport.com/license
*/
namespace koolreport\cache;
use \koolreport\core\Utility;
trait MemCache
{
private $_memKeyCache;
private $_memCache;
public function __constructMemCache()
{
$this->_memKeyCache = md5(Utility::getClassPath($this).json_encode($this->params));
$settings = array();
if(method_exists($this,"cacheSettings"))
{
$settings = $this->cacheSettings();
}
$ttl = Utility::get($settings,"ttl",5*60);
$servers = Utility::get($settings,"servers");
if($servers==null)
{
throw new \Exception("Please added servers");
}
$this->_memCache = new \Memcached();
foreach($servers as $host=>$port)
{
$this->_memCache->addServer($host,$port);
}
if($this->_memCache->get($this->_memKeyCache))
{
$this->registerEvent("OnBeforeRun",function(){
$data = $this->_memCache->get($this->_memKeyCache);
foreach($this->dataStores as $name=>&$dataStore)
{
if(isset($data[$name]))
{
$dataStore->meta($data[$name]["meta"]);
$dataStore->data($data[$name]["data"]);
// This to avoid widget to initiate requestDataSending()
$dataStore->setEnded(true);
}
}
return false;
});
}
else
{
$this->registerEvent("OnRunEnd",function(){
$settings = array();
if(method_exists($this,"cacheSettings"))
{
$settings = $this->cacheSettings();
}
$ttl = Utility::get($settings,"ttl",5*60);
$data = array();
foreach($this->dataStores as $name=>$dataStore)
{
$data[$name] = array(
"meta"=>$dataStore->meta(),
"data"=>$dataStore->data(),
);
}
$this->_memCache->add($this->_memKeyCache,$data,$ttl);
});
}
}
}