diff --git a/grails-app/assets/javascripts/profileEditor/controllers/AdminController.js b/grails-app/assets/javascripts/profileEditor/controllers/AdminController.js index 9239bcd0..23760c92 100644 --- a/grails-app/assets/javascripts/profileEditor/controllers/AdminController.js +++ b/grails-app/assets/javascripts/profileEditor/controllers/AdminController.js @@ -248,7 +248,7 @@ profileEditor.controller('ALAAdminController', function ($http, util, messageSer messageService.success(response.data.resp); } }, function() { - messageService.alert(response.data.error); + messageService.alert("Failed to clear cache the job"); }); } }); diff --git a/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy b/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy index 49c63f9b..884070a8 100644 --- a/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy +++ b/grails-app/controllers/au/org/ala/profile/hub/AdminController.groovy @@ -3,6 +3,8 @@ package au.org.ala.profile.hub import au.org.ala.profile.security.Role import au.org.ala.profile.security.Secured import au.org.ala.ws.service.WebService +import net.sf.ehcache.Cache +import net.sf.ehcache.CacheManager import org.apache.http.HttpStatus import org.apache.http.entity.ContentType @@ -14,7 +16,11 @@ class AdminController extends BaseController { WebService webService ProfileService profileService GrailsCacheManager grailsCacheManager - + static long ONE_DAY_SECONDS = 86400 + static String USER_DETAILS_CACHE = "userDetailsCache" + static String USER_DETAILS_BY_ID_CACHE = "userDetailsByIdCache" + static String USER_LIST_CACHE = "userListCache" + static String VOCAB_LIST_CACHE = "vocabListCache" @Secured(role = Role.ROLE_ADMIN, opusSpecific = false) def index() { render view: "admin.gsp" @@ -122,22 +128,51 @@ class AdminController extends BaseController { @Secured(role = Role.ROLE_ADMIN, opusSpecific = false) def cacheManagement() { - def cacheNames = grailsCacheManager.getCacheNames() + def cacheNames + if (grailsCacheManager == null) { + cacheNames = getCacheManager().getCacheNames() + } else { + cacheNames = grailsCacheManager.getCacheNames() + } success cacheNames } + private static CacheManager getCacheManager() { + CacheManager manager = CacheManager.create() + Cache userDetailCache = new Cache(USER_DETAILS_CACHE, 2000, true, false, ONE_DAY_SECONDS, 2) + if (!manager.ehcaches.get(USER_DETAILS_CACHE)) { + manager.addCache(userDetailCache) + } + Cache userDetailsByIdCache = new Cache(USER_DETAILS_BY_ID_CACHE, 2000, true, false, ONE_DAY_SECONDS, 2) + if (!manager.getEhcache(USER_DETAILS_BY_ID_CACHE)) { + manager.addCache(userDetailsByIdCache) + } + Cache userListCache = new Cache(USER_LIST_CACHE, 2000, true, false, ONE_DAY_SECONDS, 2) + if (!manager.getEhcache(USER_LIST_CACHE)) { + manager.addCache(userListCache) + } + Cache vocabListCache = new Cache(VOCAB_LIST_CACHE, 2000, true, false, ONE_DAY_SECONDS, 2) + if (!manager.getEhcache(VOCAB_LIST_CACHE)) { + manager.addCache(vocabListCache) + } + return manager + } + @Secured(role = Role.ROLE_ADMIN, opusSpecific = false) def clearCache() { Map result = [:] if (params.id) { - grailsCacheManager.getCache(params.id).clear() + if (grailsCacheManager == null) { + getCacheManager().getCache(params.id).remove(params.id) + } else { + grailsCacheManager.getCache(params.id).clear() + } result.resp = "Successfully cleared cache - " + params.id result.statusCode = HttpStatus.SC_OK success result } else { - result.error = "Failed to clear cache the job" - result.statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR - sendError result + var message = "Failed to clear cache the job" + sendError(HttpStatus.SC_BAD_REQUEST, message ?:""); } } } diff --git a/src/test/groovy/au/org/ala/profile/hub/AdminControllerSpec.groovy b/src/test/groovy/au/org/ala/profile/hub/AdminControllerSpec.groovy index c23f9592..7c034429 100644 --- a/src/test/groovy/au/org/ala/profile/hub/AdminControllerSpec.groovy +++ b/src/test/groovy/au/org/ala/profile/hub/AdminControllerSpec.groovy @@ -6,15 +6,15 @@ import spock.lang.Specification class AdminControllerSpec extends Specification implements ControllerUnitTest { - AdminController mockAdminController + AdminController controller def setup() { - mockAdminController = Mock(AdminController) + controller = new AdminController() } def "cache management should display to the admin controller when admin mode"() { when: - mockAdminController.cacheManagement() + controller.cacheManagement() then: assert response.status == HttpStatus.SC_OK @@ -23,9 +23,18 @@ class AdminControllerSpec extends Specification implements ControllerUnitTest