All-in-One Wrapper for APC, EAccelerator, xCache and Disk Cache

Web-hosting business can now be started for just $19.95 with unlimited resources. Start your own.

This library lists apc_* family of functions for eAccelerator, xCache or Disk Cache operations when APC is not available on server. When APC is installed and enabled, this library does nothing (because the desired functions are already available) - resulting in zero wrapper call overhead since all calls go directly to internal APC api.

In simple words, whether or not your PHP installation has a caching engine, you can always use apc_store, apc_fetch and apc_delete for your cache management.

This wrapper was written to abstract out or automate the cache engine selection. This wraps three basic read/write/delete functions of three common caching engines and disk caching as well. I named all the functions after APC api so that whatever caching engine I have on my server, my application does not require a change, and in case my server has APC installed and enabled, I get the additional benefit of Zero overhead on internal function calls.

The Code

  1. $apc_cache_dir  = 'cache/data/';
  2.  
  3. if( !function_exists( 'apc_store' ) ) {
  4.     if( function_exists( 'eaccelerator_put' ) ) {
  5.         function apc_store( $k , $d , $t = 0 ) {
  6.             return eaccelerator_put( $k , $d , $t );
  7.         }
  8.        
  9.         function apc_fetch( $k ) {
  10.             return eaccelerator_get( $k );
  11.         }
  12.        
  13.         function apc_delete( $k ) {
  14.             return eaccelerator_rm( $k );
  15.         }
  16.        
  17.     } else if( function_exists( 'xcache_set' ) ) {
  18.         function apc_store( $k , $d , $t = 0 ) {
  19.             return xcache_set( $k , $d , $t );
  20.         }
  21.        
  22.         function apc_fetch( $k ) {
  23.             return xcache_get( $k );
  24.         }
  25.        
  26.         function apc_delete( $k ) {
  27.             return xcache_unset( $k );
  28.         }
  29.        
  30.     } else {
  31.         function apc_store( $k , $d , $t = 0 ) {
  32.             $to_file    = $GLOBALS['apc_cache_dir'] . md5( $k );
  33.            
  34.             if( touch( $to_file ) && is_writable( $to_file ) ) {
  35.                 return ( bool ) file_put_contents( $to_file , serialize( $d ) );
  36.             }
  37.             return false;
  38.         }
  39.        
  40.         function apc_fetch( $k ) {
  41.             $from_file  = $GLOBALS['apc_cache_dir'] . md5( $k );
  42.            
  43.             if( file_exists( $from_file ) ) {
  44.                 return unserialize( file_get_contents( $from_file ) );
  45.             }
  46.             return false;
  47.         }
  48.        
  49.         function apc_delete( $k ) {
  50.             $to_file    = $GLOBALS['apc_cache_dir'] . md5( $k );
  51.            
  52.             if( file_exists( $to_file ) && is_writable( $to_file ) ) {
  53.                 return unlink( $to_file );
  54.             }
  55.             return false;
  56.         }
  57.        
  58.     }
  59. }
PHP Code

Installation & Configuration

Save the code as apc.php and include in your script. You have to define a valid cache path for Disk Caching.
  1. include( $base . 'lib/apc.php' );
  2.  
  3. //Setting disk cache path
  4. $apc_cache_dir  = $app_base . '/cache/data/';
PHP Code
Additionally, you would have to set appropriate permissions for your cache directory
$apc_cache_dir
to make it writable.

Storing, Fetching & Deleting Key-Value Pairs

  1. $key    = 'app::entry::' . $entry_id;
  2. $value  = $obj->get_entry( $entry_id );
  3.  
  4. //$value will be stored through the best available among APC, eAccelerator, xCache & Disk
  5. apc_store( $key , $value , $ttl = 0 );
  6.  
  7. //$value1 is now same as $value
  8. $value1 = apc_fetch( $key );
  9.  
  10. apc_delete( $key );
PHP Code



  1. High Performance Hosting is now $3.96 a month


© 2012-2017 PageConfig.com - Scripts - Twitter