(Quick Reference)

2 Getting Started - Reference Documentation

Authors: Graeme Rocher

Version: 5.0.8.RELEASE

2 Getting Started

To get started with GORM for Redis you need to add the Redis GORM plugin as a dependency in build.gradle:

compile "org.grails.plugins:redis-gorm:VERSION"

With that done you need to set up a running Redis server. GORM for Redis requires Redis 2.0.0 or above which you can download at on the Redis download page. Once downloaded extract the archive and run the following commands from the redis-2.0.0 directory:

make
./redis-server

With the above commands executed in a terminal window you should see output like the following appear:

[66243] 03 Sep 17:35:12 * Server started, Redis version 2.0.0
[66243] 03 Sep 17:35:12 * DB loaded from disk: 0 seconds
[66243] 03 Sep 17:35:12 * The server is now ready to accept connections on port 6379

As you can see the server is running on port 6379, but don't worry the Redis plugin for Grails will automatically configure itself to look for Redis on that port by default.

If you want to configure how Grails connects to redis then you can do so using the following settings in grails-app/conf/application.yml:

grails:
    redis-gorm:
        host: "myserver"
        port: 6380
        password: "secret"
        pooled: true
        resources: 15
        timeout: 5000

2.1 Using Redis Standalone

If you plan to use Redis as your primary datastore then you need to remove the Hibernate plugin definition from build.gradle.

compile "org.grails.plugins:hibernate"

With this done all domain classes in grails-app/domain will be persisted via Redis and not Hibernate. You can create a domain class by running the regular create-domain-class command:

grails create-domain-class Person

The Person domain class will automatically be a persistent entity that can be stored in Redis.

2.2 Combining Redis and Hibernate

If you have both the Hibernate and Redis plugins installed then by default all classes in the grails-app/domain directory will be persisted by Hibernate and not Redis. If you want to persist a particular domain class with Redis then you must use the mapWith property in the domain class:

static mapWith = "redis"