Skip to content

Commit

Permalink
#16333 when the secondary cache is repopulate, the previous elements …
Browse files Browse the repository at this point in the history
…on the primary cache were orphan, now the elements on the current secondary cache are removed before override the secondary cache
  • Loading branch information
jdotcms committed Apr 16, 2019
1 parent aad9c66 commit 5833de7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -837,11 +837,10 @@ public void sameURLSystemHost()
* Testing how the cache is working with multiple vanities using on different Sites
*/
@Test
public void test_same_vanity_on_system_and_default_host()
public void test_same_vanity_on_system()
throws DotDataException, DotSecurityException {

Contentlet systemVanityURL = null;
Contentlet defaultHostVanityURL = null;

try {

Expand Down Expand Up @@ -914,7 +913,7 @@ public void test_same_vanity_on_system_and_default_host()
Assert.assertNotEquals(VanityUrlAPI.CACHE_404_VANITY_URL,
vanityURLCached.getVanityUrlId());

///////
/////// UNPUBLISH

filtersUtil.unpublishVanityURL(systemVanityURL);

Expand All @@ -928,33 +927,36 @@ public void test_same_vanity_on_system_and_default_host()

Assert.assertNull(vanityURLCached);


vanityURLCached = vanityUrlAPI
.getLiveCachedVanityUrl(requestedURL, APILocator.systemHost(), defaultLanguageId, user);
Assert.assertNotNull(vanityURLCached);
Assert.assertEquals(VanityUrlAPI.CACHE_404_VANITY_URL,
vanityURLCached.getVanityUrlId());

vanityURLCached = vanityUrlCache.get(
new CacheVanityKey(
systemVanityURL.getStringProperty(VanityUrlContentType.SITE_FIELD_VAR),
systemVanityURL.getLanguageId(),
requestedURL
));

Assert.assertNull(vanityURLCached);

/////////

vanityURLCached = vanityUrlAPI
.getLiveCachedVanityUrl(requestedURL, defaultHost, defaultLanguageId, user);
Assert.assertNotNull(vanityURLCached);
Assert.assertEquals(VanityUrlAPI.CACHE_404_VANITY_URL,
vanityURLCached.getVanityUrlId());

vanityURLCached = vanityUrlAPI
.getLiveCachedVanityUrl(requestedURL, APILocator.systemHost(), defaultLanguageId, user);
Assert.assertNotNull(vanityURLCached);
Assert.assertEquals(VanityUrlAPI.CACHE_404_VANITY_URL,
vanityURLCached.getVanityUrlId());

} finally {
contentletAPI.archive(systemVanityURL, user, false);
//contentletAPI.archive(defaultHostVanityURL, user, false);

contentletAPI.delete(systemVanityURL, user, false);
//contentletAPI.delete(defaultHostVanityURL, user, false);
}
}


/**
* Testing how the cache is working with multiple vanities using on different Sites
*/
Expand Down Expand Up @@ -1066,7 +1068,7 @@ public void differentSitesTest()
vanityURLCached.getVanityUrlId());

//------------------------------------
//Unpublish/Pubish the VanityURL that lives in the SYSTEM_HOST
//Unpublish/Pubish the VanityURL that lives in the SYSTEM_HOST todo: something between here and the previous remove the secondary cache
//------------------------------------
filtersUtil.unpublishVanityURL(systemHostVanityURL);

Expand All @@ -1079,6 +1081,15 @@ public void differentSitesTest()
));
Assert.assertNull(vanityURLCached);

vanityURLCached = vanityUrlCache.get(
new CacheVanityKey(
systemHostVanityURL.getStringProperty(VanityUrlContentType.SITE_FIELD_VAR),
systemHostVanityURL.getLanguageId(),
requestedURL
));
Assert.assertNull(vanityURLCached);


//Request a vanity with a URL with a match
vanityURLCached = vanityUrlAPI
.getLiveCachedVanityUrl(requestedURL, defaultHost, defaultLanguageId, user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6636,7 +6636,7 @@ public void run() {
localSystemEventsAPI
.notify(new CommitListenerEvent(contentlet));
}
}, 1001);
});
}

HibernateUtil.addCommitListener(()-> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import com.dotcms.contenttype.model.type.VanityUrlContentType;
import com.dotcms.repackage.com.google.common.annotations.VisibleForTesting;
import com.dotcms.services.VanityUrlServices;
import com.dotcms.vanityurl.model.CacheVanityKey;
import com.dotcms.vanityurl.model.CachedVanityUrl;
import com.dotcms.vanityurl.model.DefaultVanityUrl;
import com.dotcms.vanityurl.model.VanityUrl;
import com.dotmarketing.beans.Host;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.CacheLocator;
import com.dotmarketing.business.DotStateException;
import com.dotmarketing.business.UserAPI;
import com.dotmarketing.common.db.DotConnect;
Expand All @@ -30,12 +32,7 @@
import org.apache.commons.collections.keyvalue.MultiKey;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.regex.Matcher;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -124,8 +121,8 @@ private void initializeActiveVanityURLsCacheBySiteAndLanguage(final String siteI
.sorted(Comparator.comparing(VanityUrl::getOrder))
.collect(toImmutableList());
// Add them to caches
vanityUrls.forEach(this::addToSingleVanityURLCache);
this.addSecondaryVanityURLCacheCollection (vanityUrls);
vanityUrls.forEach(this::addToSingleVanityURLCache);
// If a site was sent we need to make sure it was initialized in the cache
if (null != siteId && null != languageId) {
this.checkSiteLanguageVanities
Expand Down Expand Up @@ -530,10 +527,31 @@ private void addSecondaryVanityURLCacheCollection(final List<VanityUrl> vanityUr
vanityPerSiteLanguageMap.get(key).add(new CachedVanityUrl(vanityUrl));
}

// when the site + lang list is gonna be override, the main cache items should be clean too
vanityPerSiteLanguageMap.keySet().forEach(this::cleanCurrentVanityUrlsPerSite);

vanityPerSiteLanguageMap.forEach( (k, vanityUrlBuilder) ->
this.vanityUrlServices.setCachedVanityUrlList (k.hostId(), k.languageId(), vanityUrlBuilder.build()));
} // addSecondaryVanityURLCacheCollection.

private void cleanCurrentVanityUrlsPerSite(final SiteLanguageKey siteLanguageKey) {

final List<CachedVanityUrl> cachedVanityUrls = this.vanityUrlServices.getCachedVanityUrlList
(siteLanguageKey.hostId(), siteLanguageKey.languageId());

if (null != cachedVanityUrls) {

for (final CachedVanityUrl cachedVanityUrl : cachedVanityUrls) {

CacheLocator.getVanityURLCache().remove(new CacheVanityKey(
cachedVanityUrl.getSiteId(),
cachedVanityUrl.getLanguageId(),
cachedVanityUrl.getUrl()
).toString());
}
}
}

@CloseDBIfOpened
@Override
public void validateVanityUrl(final Contentlet contentlet) {
Expand Down

0 comments on commit 5833de7

Please sign in to comment.