CodeIgniter Forums
A cached function call / a cache anything function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: A cached function call / a cache anything function (/thread-53406.html)



A cached function call / a cache anything function - El Forum - 07-23-2012

[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-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

--> 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.


A cached function call / a cache anything function - El Forum - 07-23-2012

[eluser]PhilTem[/eluser]
[quote author="Patrick Savalle" date="1343047665"]...
--> To the codeigniter team, please add this to the new release. Smile
...[/quote]

Make either an issue on GitHub or a pull request on the latter page. The CI team isn't here too often neither do they take anything serious that is posted here, as they also mention "(please post issues and pull requests to GitHub)" Wink


A cached function call / a cache anything function - El Forum - 07-23-2012

[eluser]Patrick Savalle[/eluser]
Yes, I tried that. Github is way above my mental capacities. Sorry, I am a software developer, not a rocket scientist Wink

But be my quest.




A cached function call / a cache anything function - El Forum - 07-23-2012

[eluser]Patrick Savalle[/eluser]
https://github.com/EllisLab/CodeIgniter/issues/1646