Basically, I think it would be simpler to let the class deal with the cache expiration. That way, you just have to call "get_data()" outside the class and it would return either the cached version or the new version of the data if the cache expired.

Something along these lines in the class:
In googleWeather class:

  public function get_data(){
    // Get the data from the cache if available and not expired, else build it
    $cache = $this->get_cache();
    return $cache == FALSE ? $this->build_data() : $cache;
  }

  /**
   * Get a cached version of the data.
   * If the version has expired, return FALSE.
   */
  private function get_cache(){
    $cache_id = $this->weather_get_cache_id();

    // Return cache if possible, and if expiration time is still more than current time.
    $cache = cache_get($cache_id, 'cache_block');
    if (!empty($cache) && isset($cache->data) && ($cache->expire > time())) {
      return $cache->data;
    }

    return FALSE;
  }

  /**
   * Store a cached version of the data.
   */
  private function set_cache($data){
    $cache_id = $this->weather_get_cache_id();
    $expired_time = time() + variable_get('google_weather_block_cache', 3600);
    cache_set($cache_id , $data, 'cache_block', $expired_time);
  }

  private function build_data(){
    // the usual stuff to retrieve the data from Google Weather
	...
	
	// Cache the new generated data
    $this->set_cache($data);

    return $data;
 }

  /**
   * Helper function to retrieve cache id.
   */
  private function weather_get_cache_id(){
    $id = isset($this->location->bid) ? "bid_" . $this->location->bid : "uid_" . $this->location->uid;

    return 'google_weather_' . $id;
  }

In google_weather.module:

/**
 * Build the markup for the data retrieved from Google Weather.
 */
function google_weather_show($bid) {
  $location = location_load($bid);
  $weather = new googleWeather($location);
  $content = theme('google_weather_block', $weather->get_data());

  return array('#markup' => $content);
}

Comments

damiankloip’s picture

Mostly, I think this is a good idea. I will implement this into the module. I think I would maybe change a few things but looks pretty good!

damiankloip’s picture

Mostly done, will try to commit later.

hles’s picture

I am just throwing that here because it's related, but I really start wondering about the necessity to use a class. On my installation, I re-coded a few things and I can remove all the attributes from the class except $position and everything is still working.
I don't really see a good reason now to use a custom class. Maybe using an entity instead is a better option as we have a custom table to store the data anyway, and it would be more Drupal7-ish.

damiankloip’s picture

I'm not sure using an entity is necessary as the main reason would be to use fields etc... which we don't want. Plus if we created an entity, it would need to be customised anyway. You could say many times whether a class is needed but it can make some things easier :)

hles’s picture

Alright fair enough, I know that entities don't have to be fieldable, and can have only their own base table, which would do the trick I guess and you would benefit from the (uncomplete) API. But I can't say this is a better way or not in our case as I've never used them that way. Anyway, this is not the issue, I should have not brought that here :)

hles’s picture

I took a look at latest dev and I think there are still a few things to clean up in the class:

- some attributes are either useless or unused like no_of_days, content or data (duplicate). I think we can remove everything except location and language.
- if attributes are removed, constructor needs to be changed
- request_data() don't need parameters because they can be accessed as attributes anyway.
- private method cache_get() 's name may not be appropriate as it already exists as a Drupal function.
- cache_set() affects $this->cache = $cache; but cache is not an attribute.

damiankloip’s picture

Good point on some of that. Not sure I agree with the cache_get/set naming in the class. It makes most sense for them to be called that, and is one of the benefits of oop.

cache_set() affects $this->cache = $cache; but cache is not an attribute.

- Not sure what you mean on the above one?

Otherwise, thanks for the review! :)

damiankloip’s picture

Status: Active » Closed (fixed)

Closing as original issue has been addressed and committed for beta2 release.