Caching intensive processes in Drupal

Yes all of Drupal's pages are cached by ordinary page caching. However, some processes are so intensive that we want to cache them beyond Drupal's default. Good candidates for such caching are lists of cities within regions.
UPDATE: A more refined method using Drupal static and caching should be used
Here are the basics for caching the return of a heavy function.

<?php
//Some function that is going to read from cache or build something if it has not been cached
function regions_cities_blockdata (&$oEntity) {
    
//Check for cached version
     
$sCacheName = 'something_unique';
        
// $sCacheName is the unique identifier for this cached item
        
if (($cache = cache_get($sCacheName ,'cache')) && !empty($cache->data))
        
// if(1 == 0)  //swap this with the if above to disable caching while developing or debugging
   
{
       
//There is a cached version saved so use it.
       
$aReturn['aCities'] = $cache->data;
       
//Put in an indicator to report where it came from.
       
$aReturn['from-cache'] = $sCacheName;
    }
    else
    {
       
//there is no cached version, so build it
      
      
$aStuffToCache = array();
      
// Do some heavy process and build $aStuffToCache to what you want it to have
      // Do more heavy stuff ...
      
       // Save the results of the heavy process, $aStuffToCache to cache
      
cache_set($sCacheName ,$aStuffToCache ,'cache',CACHE_TEMPORARY);
      
$aReturn['aCities'] =  $aStuffToCache;
       
//Put in an indicator to report where it came from.
      
$aReturn['not-from-cache'] = $sCacheName;
    }
//end check for cache
      
return $aReturn;
// end regions_cities_blockdata
?>

CACHE_TEMPORARY just caches it until the next time all caches are cleared.

Documentation for cache_get()
Documentation for cache_set()

Helpful hint: For debugging purposes it is helpful to add a marker to the return that indicates whether the item is from cache or not.

caching HTML string: Append

or

caching an array or object: You may be able to add a ['CACHED'] YES or NO indicator to it. If your data structure will not allow, you may be able to handle it at output time.

If you want custom caching on a custom block, you may find it easier to just use Codit: Blocks.

section:

modules: