Scala example for accessing Ehcache Server

A few months ago I was chatting to Brad Clow about the new Ehcache Server. I asked him for an example with Scala. Anyway he just got back to me with what is the smallest program yet for accessing RESTful Ehcache Server.
In a file named ExampleScalaGet.scala:

import java.net.URL
import scala.io.Source.fromInputStream
object ExampleScalaGet extends Application {
val url = new URL("http://localhost:8080/ehcache/rest/sampleCache2/2")
fromInputStream(url.openStream).getLines.foreach(print)
}

run it with:
scala -e ExampleScalaGet
The program outputs:
Say goodnight,
Gracie.

Goodnight,
Gracie.


If you are on a Mac the easiest way to get scale installed is “sudo port install scala”.
The Scala program ways in at 6 lines. Now, let’s compare it’s tersity with the other languages, all doing a GET and printing the result (See http://ehcache.sourceforge.net/documentation/cache_server.html for the examples). Whitespace lines are not included.
Scala 6 lines, using java.net.URL
PHP 6 lines, using curl lib
Python 3 lines, using urllib2
Ruby 6 lines, using open-uri
Java 19 lines, using java.net.HttpUrlConnection, which does a few things before it gives the InputStream as used in the Scala example.
Thanks to Brad for rounding out the examples.
Scala looks very interesting.
Comparing it with the others:
Brad has also blogged about his Scala example here: http://bradclow.blogspot.com/2009/01/scala-example-for-accessing-ehcache.html

By Greg Luck

As Terracotta’s CTO, Greg (@gregrluck) is entrusted with understanding market and technology forces and the business drivers that impact Terracotta’s product innovation and customer success. He helps shape company and technology strategy and designs many of the features in Terracotta’s products. Greg came to Terracotta on the acquisition of the popular caching project Ehcache which he founded in 2003. Prior to joining Terracotta, Greg served as Chief Architect at Australian online travel giant Wotif.com. He also served as a lead consultant for ThoughtWorks on accounts in the United States and Australia, was CIO at Virgin Blue, Tempo Services, Stamford Hotels and Resorts and Australian Resorts and spent seven years as a Chartered Accountant in KPMG’s small business and insolvency divisions. He is a regular speaker at conferences and contributor of articles to the technical press.

6 comments

  1. Probably should do try/finally/close on the stream in Scala, too. Or am I missing something?

  2. If we’re counting lines then I get to play golf 🙂
    //in a file named ExampleScalaGet2.scala
    import java.net.URL
    import scala.io.Source.fromInputStream
    fromInputStream(new URL(“http://localhost:8080/ehcache/rest/sampleCache2/2”).openStream).getLines.foreach(print)
    //run with “scala ExampleScalaGet2”

  3. Since we’re golfing, the Ruby example listed has a bunch of requires that aren’t used (rubygems, rexml), plus some useless variable assignment. Two lines are all that you need:

    require "open-uri"
    puts open("http://localhost:8080/ehcache/rest/sampleCache2/2").read
    

    Granted, it’s not statically typed, and it uses Ruby’s much-maligned Net::HTTP, and it makes open work on both files and URIs… but hey.
    Been waiting for a real-life need to play around with Ehcache — might have to jump the gun a bit.

  4. In Clojure:
    If you just want lines:
    (clojure.contrib.duck-streams/read-lines (java.net.URL. “http://your.address/here”))
    or if you want a tree:
    (clojure.xml/parse “http://your.address/here”)

Comments are closed.