Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
managedcache.php
1
<?php
9
namespace
Bitrix\Main\Data
;
10
11
use
Bitrix\Main
;
12
13
class
ManagedCache
14
{
18
protected
$cache
= array();
19
protected
$cache_init
= array();
20
protected
$cachePath
= array();
21
protected
$vars
= array();
22
protected
$ttl
= array();
23
24
public
function
__construct
()
25
{
26
}
27
28
protected
static
function
getDbType
()
29
{
30
static
$type =
null
;
31
if
($type ===
null
)
32
{
33
$type = Main\Application::getInstance()->getConnection()->getType();
34
$type = strtoupper($type);
35
}
36
return
$type;
37
}
38
39
// Tries to read cached variable value from the file
40
// Returns true on success
41
// otherwise returns false
42
public
function
read
(
$ttl
, $uniqueId, $tableId =
false
)
43
{
44
if
(!isset($this->cache_init[$uniqueId]))
45
{
46
$this->cache[$uniqueId] =
Cache::createInstance
();
47
$this->cachePath[$uniqueId] = static::getDbType().($tableId ===
false
?
""
:
"/"
.$tableId);
48
$this->ttl[$uniqueId] =
$ttl
;
49
$this->cache_init[$uniqueId] = $this->cache[$uniqueId]->initCache(
$ttl
, $uniqueId, $this->cachePath[$uniqueId],
"managed_cache"
);
50
}
51
return
$this->cache_init[$uniqueId] || array_key_exists($uniqueId, $this->vars);
52
}
53
54
public
function
getImmediate
(
$ttl
, $uniqueId, $tableId =
false
)
55
{
56
$cache
=
Cache::createInstance
();
57
$cachePath
= static::getDbType().($tableId ===
false
?
""
:
"/"
.$tableId);
58
59
if
(
$cache
->initCache(
$ttl
, $uniqueId,
$cachePath
,
"managed_cache"
))
60
{
61
return
$cache
->getVars();
62
}
63
return
false
;
64
}
65
73
public
function
get
($uniqueId)
74
{
75
if
(array_key_exists($uniqueId, $this->vars))
76
{
77
return
$this->vars[$uniqueId];
78
}
79
elseif (isset($this->cache_init[$uniqueId]) && $this->cache_init[$uniqueId])
80
{
81
return
$this->cache[$uniqueId]->getVars();
82
}
83
else
84
{
85
return
false
;
86
}
87
}
88
89
// Sets new value to the variable
90
public
function
set
($uniqueId, $val)
91
{
92
if
(isset($this->cache[$uniqueId]))
93
{
94
$this->vars[$uniqueId] = $val;
95
}
96
}
97
98
public
function
setImmediate
($uniqueId, $val)
99
{
100
if
(isset($this->cache[$uniqueId]))
101
{
102
$obCache =
Cache::createInstance
();
103
$obCache->noOutput();
104
$obCache->startDataCache($this->ttl[$uniqueId], $uniqueId, $this->cachePath[$uniqueId], $val,
"managed_cache"
);
105
$obCache->endDataCache();
106
107
unset($this->cache[$uniqueId]);
108
unset($this->cache_init[$uniqueId]);
109
unset($this->cachePath[$uniqueId]);
110
unset($this->vars[$uniqueId]);
111
}
112
}
113
114
// Marks cache entry as invalid
115
public
function
clean
($uniqueId, $tableId =
false
)
116
{
117
$obCache =
Cache::createInstance
();
118
$obCache->clean(
119
$uniqueId,
120
static::getDbType().($tableId ===
false
?
""
:
"/"
.$tableId),
121
"managed_cache"
122
);
123
if
(isset($this->cache[$uniqueId]))
124
{
125
unset($this->cache[$uniqueId]);
126
unset($this->cache_init[$uniqueId]);
127
unset($this->cachePath[$uniqueId]);
128
unset($this->vars[$uniqueId]);
129
}
130
}
131
132
// Marks cache entries associated with the table as invalid
133
public
function
cleanDir
($tableId)
134
{
135
$dbType = static::getDbType();
136
$strPath = $dbType.
"/"
.$tableId;
137
foreach
($this->cachePath as $uniqueId => $Path)
138
{
139
if
($Path == $strPath)
140
{
141
unset($this->cache[$uniqueId]);
142
unset($this->cache_init[$uniqueId]);
143
unset($this->cachePath[$uniqueId]);
144
unset($this->vars[$uniqueId]);
145
}
146
}
147
$obCache =
Cache::createInstance
();
148
$obCache->cleanDir($dbType.
"/"
.$tableId,
"managed_cache"
);
149
}
150
151
// Clears all managed_cache
152
public
function
cleanAll
()
153
{
154
$this->cache = array();
155
$this->cache_init = array();
156
$this->cachePath = array();
157
$this->vars = array();
158
$this->ttl = array();
159
160
$obCache =
Cache::createInstance
();
161
$obCache->cleanDir(
false
,
"managed_cache"
);
162
}
163
164
// Use it to flush cache to the files.
165
// Causion: only at the end of all operations!
166
public
static
function
finalize
()
167
{
168
$cacheManager = Main\Application::getInstance()->getManagedCache();
169
$cache
=
Cache::createInstance
();
170
foreach
($cacheManager->cache as $uniqueId => $val)
171
{
172
if
(array_key_exists($uniqueId, $cacheManager->vars))
173
{
174
$cache
->startDataCache($cacheManager->ttl[$uniqueId], $uniqueId, $cacheManager->cachePath[$uniqueId], $cacheManager->vars[$uniqueId],
"managed_cache"
);
175
$cache
->endDataCache();
176
}
177
}
178
}
179
180
public
function
getCompCachePath
($relativePath)
181
{
182
// TODO: global var!
183
global $BX_STATE;
184
185
if
($BX_STATE ===
"WA"
)
186
{
187
$salt =
Cache::getSalt
();
188
}
189
else
190
{
191
$salt =
"/"
.mb_substr(md5($BX_STATE), 0, 3);
192
}
193
194
$path =
"/"
.SITE_ID.$relativePath.$salt;
195
return
$path;
196
}
197
}
Bitrix\Main\Data\Cache\createInstance
static createInstance($params=[])
Definition
cache.php:134
Bitrix\Main\Data\Cache\getSalt
static getSalt()
Definition
cache.php:173
Bitrix\Main\Data\ManagedCache
Definition
managedcache.php:14
Bitrix\Main\Data\ManagedCache\clean
clean($uniqueId, $tableId=false)
Definition
managedcache.php:115
Bitrix\Main\Data\ManagedCache\setImmediate
setImmediate($uniqueId, $val)
Definition
managedcache.php:98
Bitrix\Main\Data\ManagedCache\__construct
__construct()
Definition
managedcache.php:24
Bitrix\Main\Data\ManagedCache\$vars
$vars
Definition
managedcache.php:21
Bitrix\Main\Data\ManagedCache\cleanAll
cleanAll()
Definition
managedcache.php:152
Bitrix\Main\Data\ManagedCache\finalize
static finalize()
Definition
managedcache.php:166
Bitrix\Main\Data\ManagedCache\getCompCachePath
getCompCachePath($relativePath)
Definition
managedcache.php:180
Bitrix\Main\Data\ManagedCache\$ttl
$ttl
Definition
managedcache.php:22
Bitrix\Main\Data\ManagedCache\$cachePath
$cachePath
Definition
managedcache.php:20
Bitrix\Main\Data\ManagedCache\cleanDir
cleanDir($tableId)
Definition
managedcache.php:133
Bitrix\Main\Data\ManagedCache\getDbType
static getDbType()
Definition
managedcache.php:28
Bitrix\Main\Data\ManagedCache\$cache_init
$cache_init
Definition
managedcache.php:19
Bitrix\Main\Data\ManagedCache\getImmediate
getImmediate($ttl, $uniqueId, $tableId=false)
Definition
managedcache.php:54
Bitrix\Main\Data\ManagedCache\$cache
$cache
Definition
managedcache.php:18
Bitrix\Main\Data\ManagedCache\read
read($ttl, $uniqueId, $tableId=false)
Definition
managedcache.php:42
Bitrix\Main\Data
Definition
aliases.php:105
Bitrix\Main
modules
main
lib
data
managedcache.php
Создано системой
1.10.0