/**
  * Saves a short URL into the short URL cache
  *
  * @param ShortUrl $short_url
  */
 public function Save($short_url)
 {
     $url_table = $this->GetSettings()->GetTable('ShortUrl');
     $a_format_urls = $short_url->GetFormat()->GetDestinationUrls();
     foreach ($a_format_urls as $s_short_url_pattern => $s_destination_url) {
         # Need the above two parameters as a minimum for a short URL to work
         if ($short_url->GetShortUrl() and $s_destination_url) {
             $short_url_instance = str_replace('{0}', $short_url->GetShortUrl(), $s_short_url_pattern);
             $destination_url_instance = ltrim($short_url->ApplyParameters($s_destination_url), '/');
             # Now break up the destination URL so we can get all the URL parameters separated
             $url_bits = explode('?', $destination_url_instance, 2);
             $query_bits = count($url_bits) > 1 ? explode('&', $url_bits[1]) : array();
             $parameters = array();
             $values = array();
             foreach ($query_bits as $name_value_pair) {
                 $pair = explode('=', $name_value_pair, 2);
                 $parameters[] = $pair[0];
                 $values[] = $pair[1];
             }
             # If this is a changed, rather than new, short URL, remove the old one from the database cache.
             # But not if we're running RegenerateCache, because that's just deleted everything in the table
             # so it'll be quicker if we don't run this query a few thousand times.
             if (!$this->regenerating) {
                 $sql = 'DELETE FROM ' . $url_table . ' WHERE script = ' . Sql::ProtectString($this->GetDataConnection(), $url_bits[0], false) . ' AND param_names = ' . Sql::ProtectString($this->GetDataConnection(), join('|', $parameters)) . ' AND param_values = ' . Sql::ProtectString($this->GetDataConnection(), join('|', $values));
                 $this->GetDataConnection()->query($sql);
             }
             # Now save the new one
             $sql = "INSERT INTO {$url_table} SET short_url = " . Sql::ProtectString($this->GetDataConnection(), $short_url_instance, false) . ", " . 'short_url_base = ' . Sql::ProtectString($this->GetDataConnection(), $short_url->GetShortUrl(), false) . ', ' . 'script = ' . Sql::ProtectString($this->GetDataConnection(), $url_bits[0], false) . ", " . 'param_names = ' . Sql::ProtectString($this->GetDataConnection(), join('|', $parameters)) . ', ' . 'param_values = ' . Sql::ProtectString($this->GetDataConnection(), join('|', $values));
             $this->GetDataConnection()->query($sql);
         }
     }
 }