InfraCache.php
4.16 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?
/**
* @package infra_php
*/
abstract class InfraCache {
private $objMemcache = null;
public function __construct(){
$this->objMemcache = new Memcache();
$this->objMemcache->connect($this->getStrServidor(), $this->getNumPorta(), 10);
//$this->objMemcache->connect('127.0.0.1', 11211);
//$this->objMemcache->flush();
}
public abstract function getStrServidor();
public abstract function getNumPorta();
public function getNumTimeout(){
//[RANCHER] - Mudança para contornar problema no Rancher em Produção: valor padrao 1
return getenv('CACHE_TIMEOUT') + 0;
}
public abstract function getObjInfraSessao();
public function setAtributo($strChave, $varValor, $numTempo){
if (InfraDebug::isBolProcessar()) {
InfraDebug::getInstance()->gravarInfra('[InfraCache->setAtributo] ' . $strChave);
}
$strChave = trim($strChave);
if ($strChave==''){
throw new InfraException('Chave do atributo não informada.');
}
if (strlen($strChave) > 250){
throw new InfraException('Chave do atributo não pode ter mais de 250 caracteres.');
}
if ($numTempo < 0 || $numTempo > 2592000){
throw new InfraException('Tempo de armazenagem na cache inválido para o atributo '.$strChave .'.');
}
if( $this->objMemcache->replace($this->formatarChave($strChave), $varValor, 0, $numTempo) === false ){
if ($this->objMemcache->set($this->formatarChave($strChave), $varValor, 0, $numTempo) === false){
throw new InfraException('Erro configurando atributo '.$strChave.' na cache.');
}
}
$arr = $this->objMemcache->get($this->getStrChaveSistema());
$arr = ($arr === false) ? array() : unserialize($arr);
$arr[$strChave] = array(time(),$numTempo);
if( $this->objMemcache->replace($this->getStrChaveSistema(), serialize($arr), 0, 2592000) === false ){
if ($this->objMemcache->set($this->getStrChaveSistema(), serialize($arr), 0, 2592000) === false){
throw new InfraException('Erro adicionando chave ['.$strChave .'] no índice.');
}
}
}
public function getAtributo($strChave){
if (InfraDebug::isBolProcessar()) {
InfraDebug::getInstance()->gravarInfra('[InfraCache->getAtributo] ' . $strChave);
}
$strChave = trim($strChave);
if ($strChave==''){
throw new InfraException('Chave do atributo não informada.');
}
return $this->objMemcache->get($this->formatarChave($strChave));
}
public function removerAtributo($strChave){
if (InfraDebug::isBolProcessar()) {
InfraDebug::getInstance()->gravarInfra('[InfraCache->remover] ' . $strChave);
}
$strChave = trim($strChave);
if ($strChave==''){
throw new InfraException('Chave do atributo não informada.');
}
$ret = $this->objMemcache->delete($this->formatarChave($strChave),0);
$arr = $this->objMemcache->get($this->getStrChaveSistema());
$arr = ($arr === false) ? array() : unserialize($arr);
if (array_key_exists($strChave, $arr)){
unset($arr[$strChave]);
if ($this->objMemcache->replace($this->getStrChaveSistema(), serialize($arr), 0, 2592000) === false){
throw new InfraException('Erro removendo chave ['.$strChave .'] do índice.');
}
}
return $ret;
}
public function listarAtributos(){
if (InfraDebug::isBolProcessar()) {
InfraDebug::getInstance()->gravarInfra('[InfraCache->listarAtributos]');
}
$arr = $this->objMemcache->get($this->getStrChaveSistema());
$arr = ($arr === false) ? array() : unserialize($arr);
$ret = array();
foreach($arr as $strChave => $tempo){
if ((time() - $tempo[0]) < $tempo[1]){
$ret[] = $strChave;
}
}
return $ret;
}
private function getStrChaveSistema(){
return $this->getObjInfraSessao()->getStrSiglaOrgaoSistema().'.'.$this->getObjInfraSessao()->getStrSiglaSistema();
}
private function formatarChave($strChave){
return $this->getStrChaveSistema().'.'.$strChave;
}
}
?>