Search code examples
springservletsgrailsgrails-2.0grails-controller

How to count unique requests in Grails?


Given a controller action test() as defined below. I can access the request and get the IP as well as the user agent. I want to count the number of requests that access test(). This is easy, I would just increase a counter on every request.

My question is how can I count the unique requests? I.e., I want to count a request coming from the same computer only once. Is there another way than storing all the ip addresses and user agents to see if they already occurred?

def test() {
  println "ip: "+request.getRemoteAddr()
  println "user agent: "+request.getHeader("User-Agent")

}

Solution

  • You can create a new domain class as:

    class RequestCount {
       String ip
       String userAgent
    }
    

    Then save a new object on each request. Count the number of unique entries and your are done.