Ant's
Ant's

Reputation: 13811

How Do I Make A Cache In Grails?

Here it is what I'm trying or doing, I'm just displaying say around 100 tasks instance in a page. The displayed tasks are the links (click-able), clicking on it will take to that particular tasks instance show page. From that show page, the user can go back to the page he/she was before, i.e to the page of displaying 100 tasks.

Now this time again i'm fetching those 100 tasks instance from DB, I need a way to "cache" them, so that there is no need to fetch again. Is there a way to do so?

Edit:

def user = User.get(springSecurityService.principal.id)
         sleep(1000)
          params.max = Math.min(params.max ? params.int('max') : 10, 100)
         def tasks = Tasks.findAllByIsReadAndUser(false,user,[cache:true],params)
         def tasksCount = Tasks.createCriteria().list(max: params.max as Integer, offset: params.offset as Integer) {
             and {
                 eq('user',user)
                 eq('isRead',false)
             }
         }
         if(request.xhr) {
            render(view:'scroll', model:[userTasks:tasks,tasksCount: tasksCount.getTotalCount()])
         }
         else
         {
            [userTasks: tasks, tasksCount: tasksCount.getTotalCount()]
         }
    }

Notably I have this code, which works if the user hits scroll bar with bottom of his page and fetches more data :

 (function() {
                      jQuery(function() {
                        return $(window).scroll(function() {
                          var url;
                          url = $('.pagination .nextLink').attr('href');
                          if (url && $(window).scrollTop() > $(document).height() - $(window).height() - 50) {
                            $('.pagination').show();
                            $('.pagination').text('Fetching more data please wait...');
                            return $.get(url, function(data) {
                              $('#scrolling').append(data);
                              return $('.pagination').hide();
                            });
                          }
                        });
                      });
                    }).call(this);

Now the problem is that, even though adding cache:true in my controller code and cache is been taken place, due to this jquery code I'm again fetching the data!

How do I stop this?

Thanks in advance.

Upvotes: 0

Views: 287

Answers (2)

Ken Liu
Ken Liu

Reputation: 22914

The cache:true parameter caches the domain objects in Hibernate's L2 cache. This is a cache between the application and the database. In other words, if you fetch the same objects from Grails again it will get the objects from memory (the L2 cache) and not query the database again.

This will not cache requests between the browser and the application. Fortunately browsers already have a cache for this purpose.

Upvotes: 0

amra
amra

Reputation: 16935

Grails supports hibernate second-level cache. You can enable it in domain object, e.g.:

class Book {
    …
    static mapping = {
        cache true
    }
}

For details take a look into Database Mapping - cache or 5.5.2.2 Caching Strategy

Upvotes: 3

Related Questions