FAlbum suddenly not displaying albums?

Up to this week I've had no trouble with FAlbum (ver 0.7.1). It's a very useful WordPress plugin for quickly integrating your Flickr gallery into your site.

I have been using it drive a client's photo gallery for the last 3 months when this week it decides to stop working. It still showed Recent photos without any trouble, and it could also show a list of photosets, but If i tried to go to any page of photosets/albums or try to view photos in a photoset/album it would just display a blank gallery.

I looked at the code all morning. I cleared FAlbum's cache and watched it generate a new page. First it would call the flickr api to try to figure out which photoset to display, then it would return the correct photoset_id and sanitized photoset/album title. After this it should have been able to display the photos correctly. For some reason however, a second call for the same information would go through, this time it would hit the FAlbum cache and it did not return the correct photoset_id, instead it returned a sanitized version of the photoset/album title. It was infuriating trying to find why FAlbum made the second call to the cache even though it had the correct information from the first time round. And why oh why was the cache not giving back the correct information.

I decided that the implementation of the cache was rubbish and rewired the cache methods in falbum/wp/FAlbum_WP.class.php to use the WP Object Cache instead. Problem solved.

If anyone is having the same problem, here are the two methods rewritten to use the WordPress Object Cache. Remember to make sure that the cache is enabled in wp-config.php with this line define('ENABLE_CACHE', true);.

PHP:
  1. function _get_cached_data($key, $cache_option = FALBUM_CACHE_EXPIRE_SHORT) {
  2.         $this->logger->debug("getting wp-cache: $key -> ".md5($key));
  3.         $data = wp_cache_get(md5($key), 'falbum');
  4.         if(!$data){
  5.             $this->logger->debug("MISS wp-cache: $key");
  6.             $data = null;
  7.         }
  8.         else{
  9.             $this->logger->debug("HIT wp-cache: $key\n result: $data");
  10.         }
  11.         return $data;
  12.     }
  13.  
  14.     /* Function to store the data in the wp-cache */
  15.     function _set_cached_data($key, $data, $cache_option = FALBUM_CACHE_EXPIRE_SHORT) {
  16.         if(!wp_cache_get(md5($key),'falbum')){
  17.             wp_cache_add(md5($key),$data,'falbum',$cache_option);
  18.             $this->logger->debug('added to wp-cache: '.$key);
  19.         }
  20.         else{
  21.             wp_cache_set(md5($key),$data,'falbum',$cache_option);
  22.             $this->logger->debug('set wp-cache: '.$key);
  23.         }
  24.     }

I haven't decided how to go about implementing _clear_cached_data() yet as wp_cache_flush doesn't appear to support flushing a specific flag e.g. wp_cache_flush('falbum'). If anyone has any suggestions please comment below.