Created
October 3, 2012 17:52
-
-
Save iocopocomaioco/3828584 to your computer and use it in GitHub Desktop.
Using Redis with - or without - hybernate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> 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