Author: bleny Date: 2014-09-05 12:19:33 +0200 (Fri, 05 Sep 2014) New Revision: 2236 Url: http://forge.codelutin.com/projects/wao/repository/revisions/2236 Log: Introduction d'un cache optimiste sur les contacts et les navires (refs #5288, refs #5392) Added: branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/BoatsFilterValuesCacheLoader.java branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsFilterValuesCacheLoader.java Modified: branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/BoatsFilterValuesCacheKey.java branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/BoatsService.java branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsFilterValuesCacheKey.java branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsService.java branches/wao-4.0.x/wao-web/src/main/java/fr/ifremer/wao/web/DefaultWaoApplicationContext.java Modified: branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/BoatsFilterValuesCacheKey.java =================================================================== --- branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/BoatsFilterValuesCacheKey.java 2014-09-04 08:05:26 UTC (rev 2235) +++ branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/BoatsFilterValuesCacheKey.java 2014-09-05 10:19:33 UTC (rev 2236) @@ -63,4 +63,19 @@ return Objects.hashCode(locale, optionalCompanyId, obsProgram, boatsFilter); } + public Locale getLocale() { + return locale; + } + + public ObsProgram getObsProgram() { + return obsProgram; + } + + public Optional<String> getOptionalCompanyId() { + return optionalCompanyId; + } + + public BoatsFilter getBoatsFilter() { + return boatsFilter; + } } Added: branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/BoatsFilterValuesCacheLoader.java =================================================================== --- branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/BoatsFilterValuesCacheLoader.java (rev 0) +++ branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/BoatsFilterValuesCacheLoader.java 2014-09-05 10:19:33 UTC (rev 2236) @@ -0,0 +1,61 @@ +package fr.ifremer.wao.services.service; + +import com.google.common.cache.CacheLoader; +import fr.ifremer.wao.WaoTopiaPersistenceContext; +import fr.ifremer.wao.services.WaoApplicationContext; +import fr.ifremer.wao.services.WaoServiceContext; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.util.Locale; + +public class BoatsFilterValuesCacheLoader extends CacheLoader<BoatsFilterValuesCacheKey, BoatsFilterValues> { + + private static final Log log = LogFactory.getLog(BoatsFilterValuesCacheLoader.class); + + protected WaoApplicationContext waoApplicationContext; + + public BoatsFilterValuesCacheLoader(WaoApplicationContext waoApplicationContext) { + this.waoApplicationContext = waoApplicationContext; + } + + @Override + public BoatsFilterValues load(BoatsFilterValuesCacheKey key) { + + if (log.isTraceEnabled()) { + log.trace("start refreshing boatFilterValuesCache for key " + key); + } + + Locale locale = key.getLocale(); + + WaoTopiaPersistenceContext persistenceContext = null; + + try { + + persistenceContext = waoApplicationContext.newPersistenceContext(); + + WaoServiceContext waoServiceContext = waoApplicationContext.newServiceContext(persistenceContext, locale); + + BoatsService service = waoServiceContext.newService(BoatsService.class); + + BoatsFilterValues boatsFilterValues = service.getBoatsFilterValues(key); + + if (log.isTraceEnabled()) { + log.trace("will return from refreshing boatFilterValuesCache"); + } + + return boatsFilterValues; + + } catch (Exception e) { + if (persistenceContext != null) { + persistenceContext.rollback(); + } + throw e; + } finally { + if (persistenceContext != null) { + persistenceContext.close(); + } + } + + } +} Modified: branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/BoatsService.java =================================================================== --- branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/BoatsService.java 2014-09-04 08:05:26 UTC (rev 2235) +++ branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/BoatsService.java 2014-09-05 10:19:33 UTC (rev 2236) @@ -90,21 +90,37 @@ if (boatsFilterValues == null) { - BoatTopiaDao dao = getBoatDao(); + if (log.isTraceEnabled()) { + log.trace("cache fail, will compute filter values for key " + boatsFilterValuesCacheKey); + } - List<Boat> boats = dao.findAll(filter); + boatsFilterValues = getBoatsFilterValues(boatsFilterValuesCacheKey); - boatsFilterValues = new BoatsFilterValues(serviceContext.getLocale(), authenticatedWaoUser.getObsProgram(), optionalCompanyId); + cache.put(boatsFilterValuesCacheKey, boatsFilterValues); - for (Boat boat : boats) { - boatsFilterValues.addBoat(boat); - } + } - cache.put(boatsFilterValuesCacheKey, boatsFilterValues); + return boatsFilterValues; + } + public BoatsFilterValues getBoatsFilterValues(BoatsFilterValuesCacheKey boatsFilterValuesCacheKey) { + + BoatTopiaDao dao = getBoatDao(); + + List<Boat> boats = dao.findAll(boatsFilterValuesCacheKey.getBoatsFilter()); + + BoatsFilterValues boatsFilterValues = + new BoatsFilterValues( + boatsFilterValuesCacheKey.getLocale(), + boatsFilterValuesCacheKey.getObsProgram(), + boatsFilterValuesCacheKey.getOptionalCompanyId()); + + for (Boat boat : boats) { + boatsFilterValues.addBoat(boat); } return boatsFilterValues; + } public BoatsList getBoatsList(AuthenticatedWaoUser authenticatedWaoUser, Modified: branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsFilterValuesCacheKey.java =================================================================== --- branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsFilterValuesCacheKey.java 2014-09-04 08:05:26 UTC (rev 2235) +++ branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsFilterValuesCacheKey.java 2014-09-05 10:19:33 UTC (rev 2236) @@ -63,4 +63,19 @@ return Objects.hashCode(locale, optionalCompanyId, obsProgram, contactsFilter); } + public Locale getLocale() { + return locale; + } + + public ObsProgram getObsProgram() { + return obsProgram; + } + + public Optional<String> getOptionalCompanyId() { + return optionalCompanyId; + } + + public ContactsFilter getContactsFilter() { + return contactsFilter; + } } Added: branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsFilterValuesCacheLoader.java =================================================================== --- branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsFilterValuesCacheLoader.java (rev 0) +++ branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsFilterValuesCacheLoader.java 2014-09-05 10:19:33 UTC (rev 2236) @@ -0,0 +1,61 @@ +package fr.ifremer.wao.services.service; + +import com.google.common.cache.CacheLoader; +import fr.ifremer.wao.WaoTopiaPersistenceContext; +import fr.ifremer.wao.services.WaoApplicationContext; +import fr.ifremer.wao.services.WaoServiceContext; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.util.Locale; + +public class ContactsFilterValuesCacheLoader extends CacheLoader<ContactsFilterValuesCacheKey, ContactsFilterValues> { + + private static final Log log = LogFactory.getLog(BoatsFilterValuesCacheLoader.class); + + protected WaoApplicationContext waoApplicationContext; + + public ContactsFilterValuesCacheLoader(WaoApplicationContext waoApplicationContext) { + this.waoApplicationContext = waoApplicationContext; + } + + @Override + public ContactsFilterValues load(ContactsFilterValuesCacheKey key) { + + if (log.isTraceEnabled()) { + log.trace("start refreshing boatFilterValuesCache for key " + key); + } + + Locale locale = key.getLocale(); + + WaoTopiaPersistenceContext persistenceContext = null; + + try { + + persistenceContext = waoApplicationContext.newPersistenceContext(); + + WaoServiceContext waoServiceContext = waoApplicationContext.newServiceContext(persistenceContext, locale); + + ContactsService service = waoServiceContext.newService(ContactsService.class); + + ContactsFilterValues boatsFilterValues = service.getContactsFilterValues(key); + + if (log.isTraceEnabled()) { + log.trace("will return from refreshing boatFilterValuesCache"); + } + + return boatsFilterValues; + + } catch (Exception e) { + if (persistenceContext != null) { + persistenceContext.rollback(); + } + throw e; + } finally { + if (persistenceContext != null) { + persistenceContext.close(); + } + } + + } +} Modified: branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsService.java =================================================================== --- branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsService.java 2014-09-04 08:05:26 UTC (rev 2235) +++ branches/wao-4.0.x/wao-services/src/main/java/fr/ifremer/wao/services/service/ContactsService.java 2014-09-05 10:19:33 UTC (rev 2236) @@ -153,19 +153,31 @@ ContactsFilterValues contactsFilterValues = cache.getIfPresent(cacheKey); if (contactsFilterValues == null) { + contactsFilterValues = getContactsFilterValues(cacheKey); + cache.put(cacheKey, contactsFilterValues); + } - ContactTopiaDao dao = getContactDao(); - List<Contact> contacts = dao.findAllForFilterValues(filter); + return contactsFilterValues; + } - contactsFilterValues = new ContactsFilterValues(locale, obsProgram, optionalCompanyId); - for (Contact contact : contacts) { - contactsFilterValues.addContact(contact); - } + public ContactsFilterValues getContactsFilterValues(ContactsFilterValuesCacheKey cacheKey) { - cache.put(cacheKey, contactsFilterValues); + ContactTopiaDao dao = getContactDao(); + + List<Contact> contacts = dao.findAllForFilterValues(cacheKey.getContactsFilter()); + + ContactsFilterValues contactsFilterValues = + new ContactsFilterValues( + cacheKey.getLocale(), + cacheKey.getObsProgram(), + cacheKey.getOptionalCompanyId()); + + for (Contact contact : contacts) { + contactsFilterValues.addContact(contact); } return contactsFilterValues; + } public ObsMerContactsList getContactsList(AuthenticatedWaoUser authenticatedWaoUser, Modified: branches/wao-4.0.x/wao-web/src/main/java/fr/ifremer/wao/web/DefaultWaoApplicationContext.java =================================================================== --- branches/wao-4.0.x/wao-web/src/main/java/fr/ifremer/wao/web/DefaultWaoApplicationContext.java 2014-09-04 08:05:26 UTC (rev 2235) +++ branches/wao-4.0.x/wao-web/src/main/java/fr/ifremer/wao/web/DefaultWaoApplicationContext.java 2014-09-05 10:19:33 UTC (rev 2236) @@ -34,8 +34,10 @@ import fr.ifremer.wao.services.WaoWebApplicationContext; import fr.ifremer.wao.services.service.BoatsFilterValues; import fr.ifremer.wao.services.service.BoatsFilterValuesCacheKey; +import fr.ifremer.wao.services.service.BoatsFilterValuesCacheLoader; import fr.ifremer.wao.services.service.ContactsFilterValues; import fr.ifremer.wao.services.service.ContactsFilterValuesCacheKey; +import fr.ifremer.wao.services.service.ContactsFilterValuesCacheLoader; import fr.ifremer.wao.services.service.InitWaoService; import fr.ifremer.wao.services.service.SamplingPlan; import fr.ifremer.wao.services.service.SamplingPlanCacheKey; @@ -130,7 +132,11 @@ @Override public Cache<BoatsFilterValuesCacheKey, BoatsFilterValues> getBoatsFilterValuesCache() { if (boatsFilterValuesCache == null) { - boatsFilterValuesCache = newCacheBuilder(100).expireAfterWrite(18, TimeUnit.HOURS).build(); + BoatsFilterValuesCacheLoader loader = new BoatsFilterValuesCacheLoader(this); + boatsFilterValuesCache = newCacheBuilder(100) + . expireAfterAccess(20, TimeUnit.DAYS) + . refreshAfterWrite(20, TimeUnit.MINUTES) + . build(loader); } return boatsFilterValuesCache; } @@ -146,7 +152,11 @@ @Override public Cache<ContactsFilterValuesCacheKey, ContactsFilterValues> getContactsFilterValuesCache() { if (contactsFilterValuesCache == null) { - contactsFilterValuesCache = newCacheBuilder(100).expireAfterWrite(18, TimeUnit.HOURS).build(); + ContactsFilterValuesCacheLoader loader = new ContactsFilterValuesCacheLoader(this); + contactsFilterValuesCache = newCacheBuilder(100) + . expireAfterAccess(20, TimeUnit.DAYS) + . refreshAfterWrite(20, TimeUnit.MINUTES) + . build(loader); } return contactsFilterValuesCache; } @@ -178,6 +188,8 @@ } + logCacheStats(); + return persistenceContext; } @@ -195,12 +207,7 @@ @Override public void close() { - if (log.isInfoEnabled()) { - log.info("samplingPlansCache stats: " + getSamplingPlansCache().stats().toString()); - log.info("boatsFilterValuesCache stats: " + getBoatsFilterValuesCache().stats().toString()); - log.info("contactsFilterValuesCache stats: " + getContactsFilterValuesCache().stats().toString()); - log.info("synthesesCache stats: " + getSynthesesCache().stats().toString()); - } + logCacheStats(); if (topiaApplicationContext != null) { @@ -210,6 +217,17 @@ } + protected void logCacheStats() { + + if (log.isInfoEnabled()) { + log.info("samplingPlansCache stats: " + getSamplingPlansCache().stats().toString()); + log.info("boatsFilterValuesCache stats: " + getBoatsFilterValuesCache().stats().toString()); + log.info("contactsFilterValuesCache stats: " + getContactsFilterValuesCache().stats().toString()); + log.info("synthesesCache stats: " + getSynthesesCache().stats().toString()); + } + + } + public void init() { if (getApplicationConfig().isLogConfigurationProvided()) {