Skip to content

Instantly share code, notes, and snippets.

@iocopocomaioco
Created October 3, 2012 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iocopocomaioco/3828584 to your computer and use it in GitHub Desktop.
Save iocopocomaioco/3828584 to your computer and use it in GitHub Desktop.
Using Redis with - or without - hybernate
>> Using Redis Standalone > must uninstall the Hybernate plugin
grails uninstall-plugin hibernate
With this done all domain classes in grails-app/domain will be persisted via Redis and not Hibernate.
>> 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"
>> Combining Redis and Hibernate
persist Hibernate entities to Redis using special redis scope
def hibernatePerson = Person.get(1)
hibernatePerson.redis.save()
def redisPerson = Person.redis.get(1)
use Redis as a cache for Hibernate entities and take advantage of some nice Redis features:
def people = Person.list()
people.each { person -> person.redis.save() }
def randomRedisPerson = Person.redis.random()
Object Mapping
Here is an example of a domain class that can be persisted to Redis:
class Person {
String firstName
String lastName
static constraints = {
firstName blank:false
lastName blank:false
}
static mapping = {
lastName index:true
}
}
key difference with GORM for Redis is you must specify the properties you want to index
Person.findByLastName("Simpson")
unlike SQL where every single property can be queried with Redis you must specify which properties can be queried up front.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment