Caching ads from TextLinkAds in rails

In the first part, I explained how you could
Display ads from TextLinkAds in a rails application.
This created the basis for today’s addition: caching.

You know that the ads won’t change that often, and saving your bandwith and CPU cycle (and TextLinkAds too) is not too much to ask.

One way to achieve that is to use the fragment cache provided by Ruby on Rails. It does not however provide expiration dates. To add expiration, you can use 2 related elements, one for storing the expiration, one to store the actual data.

Firt you need to generate the names of the 2 keys:

def fragment_key(name)
  return “TLA/TIME/#{name}”, “TLA/DATA/#{name}”
end

and here, the logic to check whether the cache is present, and has not expired.

def content
  url = “http://www.text-link-ads.com/xml.php?inventory_key=”+@sb_config[‘key’]+”&referer=”+CGI::escape(@request.env[‘REQUEST_URI’])
  agent = “&user_agent=”+CGI::escape(@request.env[‘HTTP_USER_AGENT’])
  url_time, url_data = fragment_key(url)

  #is it time to update the cache?
  time = read_fragment(url_time)
  if (time == nil) || (time.to_time < Time.now)
    @links = request(url+agent) rescue nil
    #if we can get the latest, then update the cache
    if @links != nil
      expire_fragment(url_time)
      expire_fragment(url_data)
      write_fragment(url_time, Time.now+6.hour)
      write_fragment(url_data, @links)
    else
      #otherwise try again in 1 hour
      write_fragment(url_time, Time.now+1.hour)
      @links = read_fragment(url_data)
    end
  else
    #use the cache
    @links = read_fragment(url_data)
  end
end

In case the request to TextLinkAds fails (@links is nil), for whatever reason, you keep the cache for one extra hour till you make an other attempt.

And to make the code complete, here are the needed files to include and other needed helper functions:

require 'net/http'
require 'cgi'

def request(url)
  XmlSimple.xml_in(http_get(url))
end

# Does an HTTP GET on a given URL and returns the response body
def http_get(url)
  Net::HTTP.get_response(URI.parse(url)).body.to_s
end

So this takes care of caching the calls to TextLinkAds. You may not be done, though. Depending on the app you use, and its caching strategy, you may have another problem to solve. Your app may be caching your pages and actions, which may cause your links from TextLinkAds never to be updated. One solution is to use an approach similar to the one used by typo as described by Scott Laird and use caches_action_with_params from *scottstuff*.

This can wrap any method in your controller, and provide you the ability to control the longevity of your application’s cache simply by setting the lifetime value in the response:

response.lifetime = 6.hour

So for an application like typo, the code would be:

def content
  response.lifetime = 6.hour
  url = "http://www.text-link-ads.com/xml.php?inventory_key="+@sb_config['key']+"&referer="+CGI::escape(@request.env['REQUEST_URI'])
  agent = "&user_agent="+CGI::escape(@request.env['HTTP_USER_AGENT'])
  url_time, url_data = fragment_key(url)

  #is it time to update the cache?
  time = read_fragment(url_time)
  if (time == nil) || (time.to_time < Time.now)
    @links = request(url+agent) rescue nil
    #if we can get the latest, then update the cache
    if @links != nil
      expire_fragment(url_time)
      expire_fragment(url_data)
      write_fragment(url_time, Time.now+6.hour)
      write_fragment(url_data, @links)
    else
      #otherwise try again in 1 hour
      write_fragment(url_time, Time.now+1.hour)
      @links = read_fragment(url_data)
    end
  else
    #use the cache
    @links = read_fragment(url_data)
  end
end

As time permits, I’ll be posting my sidebar plugin for typo to provide a nice UI to set your TextLinkAds account and referral id.

Text Link Ads

Tags: ,

3 Responses to “Caching ads from TextLinkAds in rails”

  1. Frederic de Villamil May 8, 2006 at 11:13 pm #

    nice post, I was just looking for a way to add text-links-ads on my typo blog.
    Do you still plan to make a typo plugin?

  2. Pascal May 8, 2006 at 11:13 pm #

    yes, definitely. In fact, I have had the plugin running for some time here and it is just a matter of packaging it with a readme. Now that I’m back home, I should be able to get to it. How does the this weekend sound?

  3. Pascal May 8, 2006 at 11:13 pm #

    Frederic, I released the plugin and you can download it from http://blog.nanorails.com/articles/2006/06/10/textlinkads-typo-sidebar-plugin

1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95|96|97|98|99|100|101|102|103|104|105|106|107|108|109|110|111|112|113|114|115|116|117|118|119|120|121|122|123|124|125|126|127|128|129|130|131|132|133|134|135|136|137|138|139|140|141|142|143|144|145|146|147|148|149|150|151|152|153|154|155|156|157|158|159|160|161|162|163|164|165|166|167|168|169|170|171|172|173|174|175|176|177|178|179|180|181|182|183|184|185|186|187|188|189|190|191|192|193|194|195|196|197|198|199|200|201|202|203|204|205|206|207|208| buy cheap tenormin buy karela online purchase online without prescription nolvadex proscar uk where to buy no prescription needed secure online purchase risperdal no prescription cheapest accutane pills no prescription antibiotics online purchase indocin generic tablets buy herbal somaeurax and cost buy without a prescription nitroglycerinAccutane Online Doxycycline online Buy Cheap Lexapro Online No Prescription Prednisone Online payday loans online no checking account