logo

Grails with Redis: Painless NoSQL

logo

In the leadup to JavaOne, I whipped up a little competition app in Grails using the new Redis support. Graeme has written up a great GORM for Redis intro post that gives you all the info you need to get underway, so I’ll just add my experience to those interested. Here’s my little JavaOne competition app in action:

Redis Giveaway App

If you haven’t played with Redis before, think of it as a supercharged memcached. Except, of course, It’s persistent, and the values can be sets and lists.

First up, you’ll need a redis binary to run on your system. I’m developing on Windows these days, and you can get a Windows Redis binary. It’s just a few files that you can unzip where you please. Fire up redis-server.exe and you’re off and running

As for the Grails side, you’ll want to “grails uninstall-plugin hibernate”, since I’m using just redis without any other SQL stores. Check out Graeme’s post to learn how you can mix and match hibernate and Redis in the same app.

So what other changes did I need to make to store my data in Redis? None! Graeme’s done all the hard work of implement all the basic GORM stuff (including dynamic finders) right into the Redis plugin! Rock on!

One neat feature I did take advantage off is Redis unique support for random() selection of data elements. For example, I selected my competition winner by implementing the following code:

def winner = {

    	log.debug "Generating random winner"
    	Submission winningSub = Submission.random()
    	render "${winningSub.contestant.name} - ${winningSub.dTerm.name} ${winningSub.sTerm.name} ${winningSub.lTerm.name}"

    }

So the takeaway from all this is… if you’ve been keen to explore some NoSQL options, there’s never been a better time to have a look at Grails Redis support and dip your toes in the NoSQL waters. Having GORM support makes it insanely easy to experiment with your NoSQL store.

If you want to see how Grails is interacting with Redis, fire up redis-client, then issue the “monitor” command. You’ll see how GORM is doing its reads and writes against your Redis store. Interesting sets flying around the place!

Looking for docs? There’s a fantastic manual available, which even covers some of the new testing stuff (which lets you test against a ConcurrentHashMap rather than having to spark up a Redis instance!).

If you’re keen to have a look at my little demo, feel free to clone the BitBucket repo and have fun! I’m thinking we’re going to have to include NoSQL section in the next Grails in Action

2 Responses to “Grails with Redis: Painless NoSQL”

  1. uday says:

    Hi sir,

    i am new to the Grails and Redis .

    i have to build a project by using above technologies.

    i feel comfort with Grails but i coming to Redis i know basic command now to run it thats it i dont know more than that.

    can u help me out .

    give me the basic stuff which need to practice and i am using STS tool.give me the sample project which build on both Grails and Redis .

    and pls explain me how to configure Redis in config.groovy.

    i am waiting for ur help and suggestions .

    if possible please mail me to ” sivakotiuday@gmail.com” .

    thank you.

    Your Uday.

  2. Alex says:

    It’s not the first time that I am googling grails and reached you blog. Great articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

logo
logo
Powered by WordPress | Designed by Elegant Themes