Welcome Guest, Not a member yet? Register   Sign In
A cached function call / a cache anything function
#1

[eluser]Patrick Savalle[/eluser]
Copy /system/libraries/Cache/Cache.php to /application/libraries/Cache and add the method below to the class.

You now have function that can cache every other function's result. Use it only for TRUE / context-free functions, i.e. functions whose result only depend on their arguments.

It uses the standard codeigniter caching mechanism:

http://ellislab.com/codeigniter/user-gui...ching.html

It is equivalent to the standard PHP function call_user_func_array:

http://php.net/manual/en/function.call-u...-array.php

--> To the codeigniter team, please add this to the new release. Smile

Code:
/**
      * call_user_func_array_cached
      *
      * @param   function, variable args
      * @return  object
      */
          
     function call_user_func_array_cached( $function, $param_arr )
     {
         // ----------------------------------------------------------
         // Cached version of call_user_func_array, use only for TRUE
         // function, i.e. function whose result only depend on args
         // ----------------------------------------------------------

         if ( is_array( $function ) )
         {
             if ( is_object( $function[0] ) )
             {
                 $mangled_name = 'obj'. get_class( $function[0] ) .
$function[1] . serialize( $param_arr );
             }
             else
             {
                 $mangled_name = $function[0] . $function[1] .
serialize( $param_arr );
             }
         }
         else
         {
             $mangled_name = $function . serialize( $param_arr );
         }

         $cache_key = md5( __CLASS__ . $mangled_name );

         // ------------------------------------------------------------------
         // Mangle function signature and try to get from cache
         // ------------------------------------------------------------------

         $result = $this->get( $cache_key );
         if ( empty( $result) )
         {
             // ------------------------------------------------------------------
             // If not, call the function and get the result, we can only
             // cachify methods of subclasses of this class
             // ------------------------------------------------------------------

             $result = call_user_func_array( $function, $param_arr );

             // ------------------------------------------------------------------
             // Store the result in the cache for next time
             // ------------------------------------------------------------------

             $this->save( $cache_key, $result );
         }
         return $result;
     }

Example of usage:

Code:
$cache_adapter = 'apc';
$this->load->driver( 'cache', array( 'adapter' => $cache_adapter, 'backup' => 'dummy' ) );
$this->cache->{$cache_adapter}->is_supported( );

...

$result = $this->cache->call_user_func_array_cached( 'your_function', array( $arg1, $arg2, ... ) );

Don't overuse this function. Only cache costly operations that form a performance bottleneck and never rely on the cache to actually store anything.


Messages In This Thread
A cached function call / a cache anything function - by El Forum - 07-23-2012, 05:47 AM
A cached function call / a cache anything function - by El Forum - 07-23-2012, 06:04 AM
A cached function call / a cache anything function - by El Forum - 07-23-2012, 06:14 AM
A cached function call / a cache anything function - by El Forum - 07-23-2012, 06:18 AM



Theme © iAndrew 2016 - Forum software by © MyBB