generate_key($api_call); $cache_hits = $this->read(['key' => $key]); if(count($cache_hits) === 0) { $attributes = []; $attributes['key'] = $key; $attributes['expires_at'] = date('Y-m-d H:i:s', time() + $duration); $attributes['response_data'] = $response_data; return $this->create($attributes); } else { $cache_hit = $cache_hits[0]; $attributes = []; $attributes['expires_at'] = date('Y-m-d H:i:s', time() + $duration); $attributes['response_data'] = $response_data; $attributes['api_cache_id'] = $cache_hit['api_cache_id']; return $this->update($attributes); } } /** * Clear the cache for a specific API call. * * @param $api_call The API call to clear the cache for. * * @return mixed The updated cache row or null if it wasn't cached. */ public function clear_cache($api_call) { $key = $this->generate_key($api_call); $cache_hits = $this->read(['key' => $key]); if(count($cache_hits) > 0) { $cache_hit = $cache_hits[0]; $attributes = []; $attributes['expires_at'] = date('Y-m-d H:i:s', strtotime('1970-01-01 00:00:01')); $attributes['api_cache_id'] = $cache_hit['api_cache_id']; return $this->update($attributes); } return null; } /** * Retrieve a cache entry with a matching key that is not expired. * * @param $api_call The API call to retrieve. * * @return mixed The api_cache row if found, else null. */ public function retrieve($api_call) { $cache_hits = $this->read([ 'key' => $this->generate_key($api_call) ]); foreach($cache_hits as $cache_hit) { if(time() < strtotime($cache_hit['expires_at'])) { return $cache_hit; } } return null; } /** * Generate a cache key. * * @param $api_call The API call to generate the key for. * * @return string The cache key. */ private function generate_key($api_call) { return sha1( 'resource=' . $api_call->get_resource() . 'method=' . $api_call->get_method() . 'arguments=' . ( $api_call->get_arguments() !== null ? json_encode($api_call->get_arguments()) : '' ) . 'user_id=' . ( $this->session->get_user_id() !== null ? $this->session->get_user_id() : '' ) ); } }