Anonymous1
Anonymous1

Reputation: 3907

Grails 2.5.0 - Cache java object in memory

In bootstrap, I would like to:

  1. Load a list of objects from the database
  2. Extract information from the list of objects
  3. Manipulate the extracted information, creating a new list from the extracted information
  4. Cache the new list
  5. Access the new list at any point after bootstrap has finished running from a controller or service

Is there a plugin to help me do this?

Upvotes: 0

Views: 143

Answers (2)

injecteer
injecteer

Reputation: 20699

I'm using compile 'com.google.guava:guava:18.0-rc2' see wiki, which provides also auto-eviction.

Upvotes: 0

Igor Artamonov
Igor Artamonov

Reputation: 35961

It doesn't look like caching (i.e. temp value, ephemeral, that could be lost at any moment), it's precalculated value. Don't think cache plugin will help.

Basically you need a place to keep this value. It could be anything actually, a static variable from a basic POJO class for example. But if we're talking about Grails I suggest to make a special Service, that will store value, have a method to get it, and probably a method to make initial calculations. Service is a singleton, shared between different artifacts, it will be easy to extend with new logic, refactor and support this code in future. And easier to understand/remember, in contrast to some magical value coming from a cache.

Like:

class SuperValueService {
    def value

    void refresh() {
        value = ...
    }

}

Init in bootstrap:

class Bootstrap {
    def superValueService

    def init { ->
        superValueService.refresh()
    }
} 

and use from controller:

class MyController {
    def superValueService

    def action() {
        render models: [superValue: superValueService.value]
    }
}

Upvotes: 3

Related Questions