Project

General

Profile

« Previous | Next » 

Revision 48102

simplier cache management

View differences:

modules/dnet-modular-repositories-ui/trunk/src/main/java/eu/dnetlib/functionality/modular/ui/repositories/RepoInternalController.java
9 9
import javax.servlet.http.HttpServletRequest;
10 10
import javax.servlet.http.HttpServletResponse;
11 11

  
12
import com.google.common.cache.Cache;
12 13
import org.apache.commons.io.IOUtils;
13 14
import org.apache.commons.lang.StringUtils;
14 15
import org.apache.commons.lang.exception.ExceptionUtils;
15 16
import org.apache.commons.logging.Log;
16 17
import org.apache.commons.logging.LogFactory;
18
import org.springframework.cache.annotation.CacheEvict;
19
import org.springframework.cache.annotation.Cacheable;
17 20
import org.springframework.http.HttpStatus;
18 21
import org.springframework.stereotype.Controller;
19 22
import org.springframework.ui.ModelMap;
......
47 50
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
48 51
import eu.dnetlib.msro.workflows.sarasvati.loader.WorkflowExecutor;
49 52
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
50
import net.sf.ehcache.Cache;
51
import net.sf.ehcache.Element;
52 53

  
53 54
@Controller
54 55
public class RepoInternalController {
......
65 66
	@Resource
66 67
	private RepoUIUtils repoUIUtils;
67 68

  
68
	@Resource(name = "repoUIJsonCache")
69
	private Cache repoUIJsonCache;
70

  
71 69
	private static final Log log = LogFactory.getLog(RepoInternalController.class);
72 70

  
73 71
	@RequestMapping(value = "/ui/browseRepoField.do")
......
75 73
		return serviceLocator.getService(DatasourceManagerService.class).browseField(field);
76 74
	}
77 75

  
78
	@SuppressWarnings("unchecked")
76
	@Cacheable(cacheNames = "repoUIJsonCache", key = "#param, #value", condition = "#refresh == false")
79 77
	@RequestMapping(value = "/ui/listApis.do")
80 78
	public @ResponseBody List<SearchInterfacesEntry> listApis(
81 79
			@RequestParam(value = "param", required = true) final String param,
82 80
			@RequestParam(value = "value", required = true) final String value,
83 81
			@RequestParam(value = "refresh", required = false) final String refresh) throws Exception {
84 82

  
85
		final String cacheKey = "list@@@" + param + "@@@" + value;
86

  
87
		final Element elem = repoUIJsonCache.get(cacheKey);
88

  
89
		if ((elem != null) && (refresh == null)) {
90
			return (List<SearchInterfacesEntry>) elem.getObjectValue();
91
		} else {
92
			log.info("Refreshing " + cacheKey + " cache...");
93
			final List<SearchInterfacesEntry> list = serviceLocator.getService(DatasourceManagerService.class).searchInterface(param, value);
94
			repoUIJsonCache.put(new Element(cacheKey, list));
95
			return list;
96
		}
83
		return serviceLocator.getService(DatasourceManagerService.class).searchInterface(param, value);
97 84
	}
98 85

  
99 86
	@RequestMapping(value = "/ui/listRepositories.map")
......
106 93
		return serviceLocator.getService(DatasourceManagerService.class).simpleListDatasourcesByType(type);
107 94
	}
108 95

  
96
	@CacheEvict("repoUIJsonCache")
109 97
	@RequestMapping(value = "/ui/validateRepo.do")
110 98
	public @ResponseBody String listRepositories(@RequestParam(value = "id", required = true) final String id,
111 99
			@RequestParam(value = "b", required = true) final boolean b) throws Exception {
......
117 105
		final String newId = b ? serviceLocator.getService(ISRegistryService.class).validateProfile(id)
118 106
				: serviceLocator.getService(ISRegistryService.class).invalidateProfile(id);
119 107

  
120
		repoUIJsonCache.removeAll();
121

  
122 108
		return newId;
123 109
	}
124 110

  
......
191 177
		return serviceLocator.getService(DatasourceManagerService.class).deleteInterface(repoId, ifaceId);
192 178
	}
193 179

  
180
	@CacheEvict("repoUIJsonCache")
194 181
	@RequestMapping("/ui/repoApiCompliance.update")
195 182
	public @ResponseBody boolean updateRepoApiCompliance(@RequestParam(value = "id", required = true) final String repoId,
196 183
			@RequestParam(value = "iface", required = true) final String ifaceId,
......
200 187

  
201 188
		serviceLocator.getService(DatasourceManagerService.class).overrideCompliance(repoId, ifaceId, compliance);
202 189

  
203
		repoUIJsonCache.removeAll();
204

  
205 190
		return true;
206 191
	}
207 192

  
193
	@CacheEvict("repoUIJsonCache")
208 194
	@RequestMapping("/ui/repoApiCompliance.reset")
209 195
	public @ResponseBody boolean resetRepoApiCompliance(@RequestParam(value = "id", required = true) final String repoId,
210 196
			@RequestParam(value = "iface", required = true) final String ifaceId) throws Exception {
......
213 199

  
214 200
		serviceLocator.getService(DatasourceManagerService.class).overrideCompliance(repoId, ifaceId, null);
215 201

  
216
		repoUIJsonCache.removeAll();
217

  
218 202
		return true;
219 203
	}
220 204

  
modules/dnet-modular-repositories-ui/trunk/src/main/resources/eu/dnetlib/functionality/modular/ui/repositories/applicationContext-modular-ui-repositories.xml
6 6

  
7 7
	<bean name="repoUIUtils" class="eu.dnetlib.functionality.modular.ui.repositories.RepoUIUtils" />
8 8
	
9
	<bean id="repoUICacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" 
10
		p:cacheManagerName="repoUICacheManager"/>
11
	
12
	<bean id="repoUIJsonCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean"
13
		p:eternal="false" p:timeToLive="0" p:timeToIdle="36000"
14
		p:maxElementsInMemory="100" p:maxElementsOnDisk="2000"
15
		p:cacheManager-ref="repoUICacheManager" p:cacheName="repoUIJsonCache" />
16

  
17 9
</beans>

Also available in: Unified diff