To get started with GORM for Redis you need to install the plugin into a Grails application:grails install-plugin redis
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: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/Config.groovy
:grails.redis.host="myserver"
grails.redis.port=6380
grails.redis.password="secret"
grails.redis.pooled=true
grails.redis.resources=15
grails.redis.timeout=5000
If you plan to use Redis as your primary datastore then you need to uninstall the Hibernate plugin:grails uninstall-plugin 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.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:Alternatively you can persist Hibernate entities to Redis using the special redis
scope added to all Hibernate entities:def hibernatePerson = Person.get(1)hibernatePerson.redis.save()def redisPerson = Person.redis.get(1)
This makes it really easy to use Redis as a cache for Hibernate entities and take advantage of some nice Redis features like randomization:def people = Person.list()
people.each { person -> person.redis.save() }def randomRedisPerson = Person.redis.random()