Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezcontentcache.php
Go to the documentation of this file.
1 <?php
17 class eZContentCache
18 {
19 // The timestamp for the cache format, will expire
20 // cache which differs from this.
21 const CODE_DATE = 1064816011;
22
23 static function cachePathInfo( $siteDesign, $nodeID, $viewMode, $language, $offset, $roleList, $discountList, $layout, $cacheTTL = false,
24 $parameters = array() )
25 {
26 $md5Input = array( $nodeID, $viewMode, $language );
27 $md5Input[] = $offset;
28 $md5Input = array_merge( $md5Input, $layout );
29 sort( $roleList );
30 $md5Input = array_merge( $md5Input, $roleList );
31 sort( $discountList );
32 $md5Input = array_merge( $md5Input, $discountList );
33 if ( $cacheTTL == true )
34 $md5Input = array_merge( $md5Input, "cache_ttl" );
35 if ( isset( $parameters['view_parameters'] ) )
36 {
37 $viewParameters = $parameters['view_parameters'];
38 ksort( $viewParameters );
39 foreach ( $viewParameters as $viewParameterName => $viewParameter )
40 {
41 if ( !$viewParameter )
42 continue;
43 $md5Input = array_merge( $md5Input, 'vp:' . $viewParameterName . '=' . $viewParameter );
44 }
45 }
46 $md5Text = md5( implode( '-', $md5Input ) );
47 $cacheFile = $nodeID . '-' . $md5Text . '.cache';
48 $extraPath = eZDir::filenamePath( "$nodeID" );
49 $ini = eZINI::instance();
50 $currentSiteAccess = $GLOBALS['eZCurrentAccess']['name'];
51 $cacheDir = eZDir::path( array( eZSys::cacheDirectory(), $ini->variable( 'ContentSettings', 'CacheDir' ), $currentSiteAccess, $extraPath ) );
52 $cachePath = eZDir::path( array( $cacheDir, $cacheFile ) );
53 return array( 'dir' => $cacheDir,
54 'file' => $cacheFile,
55 'path' => $cachePath );
56 }
57
58 static function exists( $siteDesign, $nodeID, $viewMode, $language, $offset, $roleList, $discountList, $layout,
59 $parameters = array() )
60 {
61 $cachePathInfo = eZContentCache::cachePathInfo( $siteDesign, $nodeID, $viewMode, $language, $offset, $roleList, $discountList,
62 $layout, false, $parameters );
63 $cacheFile = eZClusterFileHandler::instance( $cachePathInfo['path'] );
64
65 if ( $cacheFile->exists() )
66 {
67 $timestamp = $cacheFile->mtime();
68 if ( eZContentObject::isCacheExpired( $timestamp ) )
69 {
70 eZDebugSetting::writeDebug( 'kernel-content-view-cache', 'cache expired #1' );
71 return false;
72 }
73 eZDebugSetting::writeDebug( 'kernel-content-view-cache', "checking viewmode '$viewMode' #1" );
74 if ( eZContentObject::isComplexViewModeCacheExpired( $viewMode, $timestamp ) )
75 {
76 eZDebugSetting::writeDebug( 'kernel-content-view-cache', "viewmode '$viewMode' cache expired #1" );
77 return false;
78 }
79
80 return true;
81 }
82
83 eZDebugSetting::writeDebug( 'kernel-content-view-cache', 'cache used #1' );
84 return false;
85 }
86
87 static function restore( $siteDesign, $nodeID, $viewMode, $language, $offset, $roleList, $discountList, $layout,
88 $parameters = array() )
89 {
90 $result = array();
91 $cachePathInfo = eZContentCache::cachePathInfo( $siteDesign, $nodeID, $viewMode, $language, $offset, $roleList, $discountList,
92 $layout, false, $parameters );
93 $cacheDir = $cachePathInfo['dir'];
94 $cacheFile = $cachePathInfo['file'];
95 $cachePath = $cachePathInfo['path'];
96 $timestamp = false;
97
98 $cacheFile = eZClusterFileHandler::instance( $cachePath );
99
100 if ( $cacheFile->exists() )
101 {
102 $timestamp = $cacheFile->mtime();
103 if ( eZContentObject::isCacheExpired( $timestamp ) )
104 {
105 eZDebugSetting::writeDebug( 'kernel-content-view-cache', 'cache expired #2' );
106 return false;
107 }
108 eZDebugSetting::writeDebug( 'kernel-content-view-cache', "checking viewmode '$viewMode' #1" );
109 if ( eZContentObject::isComplexViewModeCacheExpired( $viewMode, $timestamp ) )
110 {
111 eZDebugSetting::writeDebug( 'kernel-content-view-cache', "viewmode '$viewMode' cache expired #2" );
112 return false;
113 }
114
115 }
116
117 if ( $viewMode == 'pdf' )
118 {
119 return $cachePath;
120 }
121
122 eZDebugSetting::writeDebug( 'kernel-content-view-cache', 'cache used #2' );
123
124 $fileName = $cacheDir . "/" . $cacheFile;
125
126 $cacheFile = eZClusterFileHandler::instance( $fileName );
127 $contents = $cacheFile->fetchContents();
128
129 $cachedArray = unserialize( $contents );
130
131 $cacheTTL = $cachedArray['cache_ttl'];
132
133 // Check if cache has expired
134 if ( $cacheTTL > 0 )
135 {
136 $expiryTime = $timestamp + $cacheTTL;
137 if ( time() > $expiryTime )
138 {
139 return false;
140 }
141 }
142
143 // Check for template language timestamp
144 $cacheCodeDate = $cachedArray['cache_code_date'];
145 if ( $cacheCodeDate != self::CODE_DATE )
146 return false;
147
148 $viewMode = $cachedArray['content_info']['viewmode'];
149
150 $res = eZTemplateDesignResource::instance();
151 $res->setKeys( array( array( 'node', $nodeID ),
152 array( 'view_offset', $offset ),
153 array( 'viewmode', $viewMode ),
154 array( 'section', $cachedArray['section_id'] )
155 ) );
156 $result['content_info'] = $cachedArray['content_info'];
157 $result['content'] = $cachedArray['content'];
158
159 $result['view_parameters'] = $cachedArray['content_info']['view_parameters'];
160
161 foreach ( array( 'path', 'node_id', 'section_id', 'navigation_part' ) as $item )
162 {
163 if ( isset( $cachedArray[$item] ) )
164 {
165 $result[$item] = $cachedArray[$item];
166 }
167 }
168
169 return $result;
170 }
171
172 static function store( $siteDesign, $objectID, $classID, $classIdentifier,
173 $nodeID, $parentNodeID, $nodeDepth, $urlAlias, $viewMode, $sectionID,
174 $language, $offset, $roleList, $discountList, $layout, $navigationPartIdentifier,
175 $result, $cacheTTL = -1,
176 $parameters = array() )
177 {
178 $cachePathInfo = eZContentCache::cachePathInfo( $siteDesign, $nodeID, $viewMode, $language, $offset, $roleList, $discountList,
179 $layout, false, $parameters );
180 $cacheDir = $cachePathInfo['dir'];
181 $cacheFile = $cachePathInfo['file'];
182
183 $serializeArray = array();
184
185 if ( isset( $parameters['view_parameters']['offset'] ) )
186 {
187 $offset = $parameters['view_parameters']['offset'];
188 }
189 $viewParameters = false;
190 if ( isset( $parameters['view_parameters'] ) )
191 {
192 $viewParameters = $parameters['view_parameters'];
193 }
194 $contentInfo = array( 'site_design' => $siteDesign,
195 'node_id' => $nodeID,
196 'parent_node_id' => $parentNodeID,
197 'node_depth' => $nodeDepth,
198 'url_alias' => $urlAlias,
199 'object_id' => $objectID,
200 'class_id' => $classID,
201 'class_identifier' => $classIdentifier,
202 'navigation_part' => $navigationPartIdentifier,
203 'viewmode' => $viewMode,
204 'language' => $language,
205 'offset' => $offset,
206 'view_parameters' => $viewParameters,
207 'role_list' => $roleList,
208 'discount_list' => $discountList,
209 'section_id' => $result['section_id'] );
210
211 $serializeArray['content_info'] = $contentInfo;
212
213 foreach ( array( 'path', 'node_id', 'section_id', 'navigation_part' ) as $item )
214 {
215 if ( isset( $result[$item] ) )
216 {
217 $serializeArray[$item] = $result[$item];
218 }
219 }
220
221 $serializeArray['cache_ttl'] = $cacheTTL;
222
223 $serializeArray['cache_code_date'] = self::CODE_DATE;
224 $serializeArray['content'] = $result['content'];
225
226 $serializeString = serialize( $serializeArray );
227
228 if ( !file_exists( $cacheDir ) )
229 {
230 eZDir::mkdir( $cacheDir, false, true );
231 }
232
233 $path = $cacheDir . '/' . $cacheFile;
234 $uniqid = md5( uniqid( 'ezpcache'. getmypid(), true ) );
235
236 $file = eZClusterFileHandler::instance( "$cacheDir/$uniqid" );
237 $file->storeContents( $serializeString, 'viewcache', 'pdf' );
238 $file->move( $path );
239
240 return true;
241 }
242
243 static function calculateCleanupValue( $nodeCount )
244 {
245 return $nodeCount;
246 }
247
248 static function inCleanupThresholdRange( $value )
249 {
250 $ini = eZINI::instance();
251 $threshold = $ini->variable( 'ContentSettings', 'CacheThreshold' );
252 return ( $value < $threshold );
253 }
254
255 static function cleanup( $nodeList, $userId = false )
256 {
257 // The view-cache has a different storage structure than before:
258 // var/cache/content/<siteaccess>/<extra-path>/<nodeID>-<hash>.cache
259 // Also it uses the cluster file handler to delete files using a wildcard (glob style).
260 $ini = eZINI::instance();
261 $extraCacheName = '';
262 $cacheBaseDir = eZDir::path( array( eZSys::cacheDirectory(), $ini->variable( 'ContentSettings', 'CacheDir' ) ) );
263 $fileHandler = eZClusterFileHandler::instance();
264
265 if ( $userId !== false && is_numeric( $userId ) )
266 {
267 $extraCacheName = $userId . '-';
268 }
269
270 // Figure out the siteaccess which are related, first using the new
271 // INI setting RelatedSiteAccessList then the old existing one
272 // AvailableSiteAccessList
273 if ( $ini->hasVariable( 'SiteAccessSettings', 'RelatedSiteAccessList' ) &&
274 $relatedSiteAccessList = $ini->variable( 'SiteAccessSettings', 'RelatedSiteAccessList' ) )
275 {
276 if ( !is_array( $relatedSiteAccessList ) )
277 {
278 $relatedSiteAccessList = array( $relatedSiteAccessList );
279 }
280 $relatedSiteAccessList[] = $GLOBALS['eZCurrentAccess']['name'];
281 $siteAccesses = array_unique( $relatedSiteAccessList );
282 }
283 else
284 {
285 $siteAccesses = $ini->variable( 'SiteAccessSettings', 'AvailableSiteAccessList' );
286 }
287 if ( !$siteAccesses )
288 {
289 return;
290 }
291
292 foreach ( $nodeList as $nodeID )
293 {
294 $extraPath = eZDir::filenamePath( $nodeID );
295 $fileHandler->fileDeleteByDirList( $siteAccesses, $cacheBaseDir, "$extraPath$nodeID-$extraCacheName" );
296 }
297 }
298 }
299
300 ?>
eZClusterFileHandler\instance
static instance( $filename=false)
Definition: ezclusterfilehandler.php:21
eZContentCache
The class eZContentCache does.
Definition: ezcontentcache.php:18
eZContentCache\CODE_DATE
const CODE_DATE
Definition: ezcontentcache.php:21
eZContentCache\cachePathInfo
static cachePathInfo( $siteDesign, $nodeID, $viewMode, $language, $offset, $roleList, $discountList, $layout, $cacheTTL=false, $parameters=array())
Definition: ezcontentcache.php:23
eZContentCache\inCleanupThresholdRange
static inCleanupThresholdRange( $value)
Definition: ezcontentcache.php:248
eZContentCache\calculateCleanupValue
static calculateCleanupValue( $nodeCount)
Definition: ezcontentcache.php:243
eZContentCache\restore
static restore( $siteDesign, $nodeID, $viewMode, $language, $offset, $roleList, $discountList, $layout, $parameters=array())
Definition: ezcontentcache.php:87
eZContentCache\cleanup
static cleanup( $nodeList, $userId=false)
Definition: ezcontentcache.php:255
eZContentCache\store
static store( $siteDesign, $objectID, $classID, $classIdentifier, $nodeID, $parentNodeID, $nodeDepth, $urlAlias, $viewMode, $sectionID, $language, $offset, $roleList, $discountList, $layout, $navigationPartIdentifier, $result, $cacheTTL=-1, $parameters=array())
Definition: ezcontentcache.php:172
eZContentCache\exists
static exists( $siteDesign, $nodeID, $viewMode, $language, $offset, $roleList, $discountList, $layout, $parameters=array())
Definition: ezcontentcache.php:58
eZContentObject\isComplexViewModeCacheExpired
static isComplexViewModeCacheExpired( $viewMode, $timestamp)
Definition: ezcontentobject.php:6019
eZContentObject\isCacheExpired
static isCacheExpired( $timestamp)
Definition: ezcontentobject.php:5988
eZDebugSetting\writeDebug
static writeDebug( $conditionName, $string, $label="")
Definition: ezdebugsetting.php:127
eZDir\mkdir
static mkdir( $dir, $perm=false, $recursive=false)
Definition: ezdir.php:73
eZDir\filenamePath
static filenamePath( $filename, $maxCharLen=2)
Definition: ezdir.php:55
eZDir\path
static path( $names, $includeEndSeparator=false, $type=self::SEPARATOR_UNIX)
Definition: ezdir.php:237
eZSys\cacheDirectory
static cacheDirectory()
Definition: ezsys.php:527
eZTemplateDesignResource\instance
static instance()
Definition: eztemplatedesignresource.php:1026
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.