118 $this->Security =
get_package(
'security' ,
'last' , __FILE__ );
119 $PageName = $this->Security->get_gp(
'page_name' ,
'command' ,
'default' );
121 $this->Cache = @file_get_contents( dirname( __FILE__ ).
'/data/'.$PageName.
'.cache' );
123 $this->TableOfContents = @file_get_contents( dirname( __FILE__ ).
'/data/'.$PageName.
'.table' );
125 if( $this->TableOfContents !==
false )
127 $this->TableOfContents = unserialize( $this->TableOfContents );
132 catch( Exception $e )
156 $Conf = file_get_contents( dirname( __FILE__ ).
'/conf/cf_cache' );
157 $Conf = explode(
';' , $Conf );
158 $Conf[ 0 ] = explode(
'=' , $Conf[ 0 ] );
159 $Conf[ 1 ] = explode(
'=' , $Conf[ 1 ] );
160 $this->CacheSwitch = $Conf[ 0 ][ 1 ] ==
'on';
161 $this->CacheTimeout = intval( $Conf[ 1 ][ 1 ] );
163 catch( Exception $e )
191 private function get_header_parameters( $md5SectionOriginName )
195 $HeaderSize = 32 + 1 + 32 + 1;
197 $ReadCursor = $this->TableOfContents[ $md5SectionOriginName ][
'read_cursor' ];
199 $SectionSize = $this->TableOfContents[ $md5SectionOriginName ][
'section_size' ];
201 return( array( $HeaderSize , $ReadCursor , $SectionSize ) );
203 catch( Exception $e )
231 private function fetch_data( $md5SectionOriginName )
235 list( $HeaderSize , $Cursor , $SectionSize ) = $this->get_header_parameters( $md5SectionOriginName );
237 return( substr( $this->Cache , $Cursor + $HeaderSize , $SectionSize - $HeaderSize ) );
239 catch( Exception $e )
271 if( $this->TableOfContents ===
false )
276 $md5SectionOriginName = md5( $SectionOriginName );
277 if( isset( $this->TableOfContents[ $md5SectionOriginName ] ) ===
false )
282 $CreateDate = $this->TableOfContents[ $md5SectionOriginName ][
'tags' ][
'_timeout' ];
283 if( time() - $CreateDate > $this->CacheTimeout )
289 return( $this->fetch_data( $md5SectionOriginName ) );
291 catch( Exception $e )
319 private function update_cache( $md5SectionOriginName , $Data )
323 list( $HeaderSize , $Cursor , $SectionSize ) = $this->get_header_parameters( $md5SectionOriginName );
325 $NewDataSize = strlen( $Data );
327 $this->Cache = substr_replace(
329 $md5SectionOriginName.
':'.sprintf(
"%032d" , $NewDataSize + $HeaderSize ).
':'.$Data ,
334 catch( Exception $e )
362 private function update_table( $md5SectionOriginName , $Data )
366 list( $HeaderSize , $Cursor , $SectionSize ) = $this->get_header_parameters( $md5SectionOriginName );
368 $NewDataSize = strlen( $Data );
371 foreach( $this->TableOfContents as $Key => $Value )
373 if( $Value[
'read_cursor' ] === $Cursor )
375 $Value[
'section_size' ] = $HeaderSize + $NewDataSize;
377 elseif( $Value[
'read_cursor' ] > $Cursor )
379 $Value[
'read_cursor' ] += $HeaderSize + $NewDataSize - $SectionSize;
381 $Value[
'tags' ][
'_timeout' ] = time();
382 $NewTable[ $Key ] = $Value;
384 $this->TableOfContents = $NewTable;
386 catch( Exception $e )
418 if( $this->TableOfContents ===
false )
420 throw(
new Exception(
'Cache is empty' ) );
422 $md5SectionOriginName = md5( $SectionOriginName );
423 if( isset( $this->TableOfContents[ $md5SectionOriginName ] ) )
425 $this->update_cache( $md5SectionOriginName , $Data );
427 $this->update_table( $md5SectionOriginName , $Data );
429 if( $this->CacheSwitch ===
true )
431 $this->CacheWasUpdated =
true;
436 throw(
new Exception(
'Section '.$SectionOriginName.
' was not found' ) );
439 catch( Exception $e )
475 function add_data( $SectionOriginName , $Data , $Tags = array() )
480 $md5SectionOriginName = md5( $SectionOriginName );
481 $SectionSize = 32 + 1 + 32 + 1 + strlen( $Data );
482 $this->TableOfContents[ $md5SectionOriginName ] = array(
483 'section_size' => $SectionSize ,
'read_cursor' => strlen( $this->Cache )
485 $this->Cache .= $md5SectionOriginName.
":".sprintf(
"%032d" , $SectionSize ).
":$Data";
486 $this->TableOfContents[ $md5SectionOriginName ][
'tags' ] = array_merge(
487 $Tags , array(
'_cache_sys' ,
'_timeout' => time() )
490 if( $this->CacheSwitch ===
true )
492 $this->CacheWasUpdated =
true;
495 catch( Exception $e )
523 if( $this->CacheSwitch ===
false )
527 $this->delete_section( md5( $SectionOriginName ) );
529 catch( Exception $e )
564 if( $this->TableOfContents ===
false )
570 return( isset( $this->TableOfContents[ md5( $SectionOriginName ) ] ) );
573 catch( Exception $e )
605 private function compile_new_table_of_contents( $ReadCursor , $SectionSize )
611 foreach( $this->TableOfContents as $Key => $Value )
613 if( $Value[
'read_cursor' ] === $ReadCursor )
619 if( $Value[
'read_cursor' ] > $ReadCursor )
621 $Value[
'read_cursor' ] -= $SectionSize;
623 $NewTable[ $Key ] = $Value;
629 catch( Exception $e )
653 private function delete_section( $SectionName )
657 if( $this->TableOfContents !==
false && isset( $this->TableOfContents[ $SectionName ] ) )
659 $ReadCursor = $this->TableOfContents[ $SectionName ][
'read_cursor' ];
660 $SectionSize = $this->TableOfContents[ $SectionName ][
'section_size' ];
661 $this->Cache = substr_replace( $this->Cache ,
'' , $ReadCursor , $SectionSize );
666 $this->TableOfContents = $this->compile_new_table_of_contents( $ReadCursor , $SectionSize );
668 $this->CacheWasUpdated =
true;
671 catch( Exception $e )
699 if( $this->CacheSwitch ===
false )
705 foreach( $this->TableOfContents as $Key => $Value )
707 if( array_search( $Tag , $Value[
'tags' ] ) !==
false )
713 foreach( $Sections as $Key => $Value )
715 $this->delete_section( $Value );
718 catch( Exception $e )
742 if( $this->CacheWasUpdated && $this->CacheSwitch ===
true )
746 $PageName = $this->Security->get_gp(
'page_name' ,
'command' ,
'default' );
750 file_put_contents( dirname( __FILE__ ).
'/data/'.$PageName.
'.cache' , $this->Cache );
755 dirname( __FILE__ ).
'/data/'.$PageName.
'.table' , serialize( $this->TableOfContents )
759 catch( Exception $e )
783 $PageName = $this->Security->get_gp(
'page_name' ,
'command' ,
'default' );
785 @unlink( dirname( __FILE__ ).
'./data/'.$PageName.
'.cache' );
786 @unlink( dirname( __FILE__ ).
'./data/'.$PageName.
'.table' );
788 catch( Exception $e )
812 if( $this->CacheSwitch ===
false )
819 catch( Exception $e )