Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3339424
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:29:26+00:00 2026-05-18T00:29:26+00:00

I have a site where users will first enter their search criteria. The search

  • 0

I have a site where users will first enter their search criteria. The search combines data from 2 sources, one of which is not a database. The search results are generally large enough to be paged.

Since the initial query is expensive, I would like to cache the search results if the search criteria doesn’t change for subsequent page views.

My current method looks like:

def search(criteriaMap, offset, pagesize)

What is the best way of caching the search results in groovy/grails? How would I expire the search results?

Note, I see there is a springcache plugin but I am not sure if this will work for paged results. I also know that grails is packaged with ehcache but I haven’t seen an API I directly have access to, I have only seen it used for 2nd level caching with GORM.

Update: I took part of @mfloryan’s solution to solve this.

I created two methods in the searchService:

@Cacheable("searchCache")
def searchResults(uid, trimmedParams)
def trimParams(params)

(One is cached and the other is not.)

My code in the searchController:

def trimmedParams = searchService.trimParams(params)
def results = searchService.searchResults(trimmedParams)

//Page results
results = HelperService.pageResults(results, params.offset, params.max)

[results: results, searchParams : trimmedParams]

As a results, I invoked the search service with a trimmed list of parameters that do not include paging params. The cached search results can be returned.

The searchController takes care of the paging.

note: I didn’t cache the hibernate results because I would end up with double the data in the cache. I only cached the combined results from searchService.searchResults (which hits a REST API and my local database)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:29:27+00:00Added an answer on May 18, 2026 at 12:29 am

    Firstly, if you want to cache the query, then perhaps using the Hibernate query cache is a good start (Set cache:true in the mappings and pass cache: true to the executeQuery)

    Using the SpringCache plug-in will allow you to cache the entire response from the controller. All you would need to do is decorate the controller like that: @Cacheable("searchMethodCache") or only decorate individual methods that you want results of cached. The cache will save response by query string so it should work well with paging.
    The @CacheFlush annotation allows to set when the cache should be cleared.

    The EH Cache itself is configured in grails-app/conf/spring/resources.groovy

    — UPDATE —

    Following you comment I begin to suspect your might be thinking of a slightly different solution. So there is a query that returns a list of results and that list is then paged and displayed. With standard Grails approach Hibernate will generate one query for each page (returning a subset from the full list thus running the potentially expensive query once for each page). Presumably you want the query to be run once – to return the full result set that gets cached and then serve individual pages from the it.
    If that’s the case, far as I know, you will have to page the results manually either from the Hibernate cache or caching the result set somewhere else (Session context?)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a site where some users will be registered by our staff, and
I have a site that will require a login by the users. The client
i have a doubt in sessionid. when user visit the site one sessionid will
I have made this batch script which will allow the user to enter a
I am developing a site for our company that will enable users from other
I have two sites. The first site requires users to log in. An authentication
I constantly have to update main images on my site, the user will go
I have a site where users can log in and check on outstanding support
Let's say I have a site where Users can add Entries through admin panel.
I have a members site where users are given up to 7 web page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.