A cached function call / a cache anything function (code) #1646

Closed
patricksavalle opened this Issue Jul 23, 2012 · 5 comments

Projects

None yet

4 participants

@patricksavalle

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://codeigniter.com/user_guide/libraries/caching.html

It is equivalent to the standard PHP function call_user_func_array:

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

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

—> To the codeigniter team, please add this to the new release. :)

      * 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:

    $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, ... ) ); 
@timw4mail

Please submit this as a proper pull request.

@patricksavalle

No. I'm sorry. I would rather try to understand the Higgs-field then use Github, way too complex for me. ;)

@timw4mail

Those are the rules, I don't make them. There's a guide on https://github.com/EllisLab/CodeIgniter.

@marcosgdf

@patricksavalle Got 15 minutes and want to learn Git? http://try.github.com/levels/1/challenges/1

@narfbg narfbg closed this Oct 26, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment