Skip to content

Commit

Permalink
chore: add cache to contentletVersionInfo ref: #26931
Browse files Browse the repository at this point in the history
  • Loading branch information
erickgonzalez committed Dec 11, 2023
1 parent c9d14b4 commit d76f534
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 54 deletions.
Expand Up @@ -5,6 +5,8 @@
import com.dotmarketing.beans.VersionInfo;
import com.dotmarketing.portlets.contentlet.model.ContentletVersionInfo;

import java.util.List;

public abstract class IdentifierCache implements Cachable {

abstract protected void addIdentifierToCache(Identifier id);
Expand Down Expand Up @@ -70,4 +72,9 @@ public String get404Group() {
return "Identifier404Cache";
}

public abstract void putContentVersionInfos(String identifier,
List<ContentletVersionInfo> cvis);

public abstract List<ContentletVersionInfo> getContentVersionInfos(String identifier);

}
Expand Up @@ -11,6 +11,7 @@
import com.dotmarketing.beans.Host;
import com.dotmarketing.beans.Identifier;
import com.dotmarketing.beans.VersionInfo;
import com.dotmarketing.db.DbConnectionFactory;
import com.dotmarketing.portlets.contentlet.model.ContentletVersionInfo;
import com.dotmarketing.util.InodeUtils;
import com.dotmarketing.util.Logger;
Expand Down Expand Up @@ -159,15 +160,16 @@ protected void removeFromCacheByIdentifier(final Identifier id) {
if(id==null) {
return;
}
if(InodeUtils.isSet(id.getId())) {

if(UtilMethods.isSet(id::getId)) {
final String key = getPrimaryGroup() + id.getId();
cache.remove(key, getPrimaryGroup());
cache.remove(key, get404Group());
cache.remove(allInfosKey(id.getId()), getVersionInfoGroup());
}

final String uri = id.getURI();
if(UtilMethods.isSet(id.getHostId()) && UtilMethods.isSet(uri)) {
if(UtilMethods.isSet(id::getHostId) && UtilMethods.isSet(uri)) {
final String key = getPrimaryGroup() + id.getHostId() + "-" + uri;
cache.remove(key, getPrimaryGroup());
cache.remove(key, get404Group());
Expand Down Expand Up @@ -226,6 +228,7 @@ public void removeFromCacheByVersionable(Versionable versionable) {

public void removeFromCacheByInode(String inode) {
cache.remove(getVersionGroup() + inode, getVersionGroup());
cache.remove(allInfosKey(inode), getVersionInfoGroup());
}


Expand Down Expand Up @@ -295,16 +298,42 @@ public VersionInfo getVersionInfo(String identifier) {
public void removeContentletVersionInfoToCache(String identifier, long lang) {
String key = getKey(identifier, lang);
cache.remove(getVersionInfoGroup()+key, getVersionInfoGroup());
cache.remove( allInfosKey(identifier) ,getVersionInfoGroup());
}

@Override
public void removeContentletVersionInfoToCache(String identifier, long lang, final String variantId) {
String key = getKey(identifier, lang, variantId);
cache.remove(getVersionInfoGroup() + key , getVersionInfoGroup());
cache.remove( allInfosKey(identifier) ,getVersionInfoGroup());
}

@Override
protected void removeVersionInfoFromCache(String identifier) {
cache.remove(getVersionInfoGroup()+identifier, getVersionInfoGroup());
cache.remove( allInfosKey(identifier) ,getVersionInfoGroup());
}

private String allInfosKey(String... vals){
return "all-"+ String.join("-", vals);
}


@Override
public List<ContentletVersionInfo> getContentVersionInfos(final String identifier){
// we don't read from cache in transactions
if(DbConnectionFactory.inTransaction()){
return null;
}
return (List<ContentletVersionInfo>) cache.getNoThrow(allInfosKey(identifier) , getVersionInfoGroup());
}

@Override
public void putContentVersionInfos(final String identifier, final List<ContentletVersionInfo> versionInfos){
// we don't write cache in transactions
if(DbConnectionFactory.inTransaction()){
return;
}
cache.put(allInfosKey(identifier) ,versionInfos, getVersionInfoGroup());
}
}
Expand Up @@ -13,6 +13,7 @@
import com.dotmarketing.db.DbConnectionFactory;
import com.dotmarketing.db.HibernateUtil;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotRuntimeException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.containers.business.ContainerAPI;
import com.dotmarketing.portlets.containers.model.Container;
Expand All @@ -31,6 +32,8 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.beanutils.BeanUtils;

/**
Expand Down Expand Up @@ -389,56 +392,20 @@ public Optional<ContentletVersionInfo> findAnyContentletVersionInfo(final String
@Override
public Optional<ContentletVersionInfo> findAnyContentletVersionInfo(final String identifier, final boolean deleted)
throws DotDataException {
final DotConnect dotConnect = new DotConnect()
.setSQL("SELECT * FROM contentlet_version_info WHERE identifier= ? AND deleted = ? "
+ "AND variant_id = '"+DEFAULT_VARIANT.name()+"'")
.addParam(identifier)
.addParam(deleted)
.setMaxRows(1);

final List<ContentletVersionInfo> versionInfos = TransformerLocator
.createContentletVersionInfoTransformer(dotConnect.loadObjectResults()).asList();

return versionInfos == null || versionInfos.isEmpty()
? Optional.empty()
: Optional.of(versionInfos.get(0));
return findContentletVersionInfos(identifier, DEFAULT_VARIANT.name(), 0).stream().filter(cvi->!cvi.isDeleted() || deleted ).findAny();
}

@Override
public Optional<ContentletVersionInfo> findAnyContentletVersionInfoAnyVariant(final String identifier, final boolean deleted)
throws DotDataException {
final DotConnect dotConnect = new DotConnect()
.setSQL("SELECT * FROM contentlet_version_info WHERE identifier= ? AND deleted = ?")
.addParam(identifier)
.addParam(deleted)
.setMaxRows(1);

final List<ContentletVersionInfo> versionInfos = TransformerLocator
.createContentletVersionInfoTransformer(dotConnect.loadObjectResults()).asList();

return versionInfos == null || versionInfos.isEmpty()
? Optional.empty()
: Optional.of(versionInfos.get(0));
return findContentletVersionInfos(identifier, null, 0).stream().filter(cvi->!cvi.isDeleted() || deleted ).findAny();
}

@Override
public Optional<ContentletVersionInfo> findAnyContentletVersionInfo(final String identifier,
final String variant, final boolean deleted)
throws DotDataException {
final DotConnect dotConnect = new DotConnect()
.setSQL("SELECT * FROM contentlet_version_info WHERE identifier= ? AND deleted = ? "
+ "AND variant_id = ?")
.addParam(identifier)
.addParam(deleted)
.addParam(variant)
.setMaxRows(1);

final List<ContentletVersionInfo> versionInfos = TransformerLocator
.createContentletVersionInfoTransformer(dotConnect.loadObjectResults()).asList();

return versionInfos == null || versionInfos.isEmpty()
? Optional.empty()
: Optional.of(versionInfos.get(0));
return findContentletVersionInfos(identifier, variant, 0).stream().filter(cvi->!cvi.isDeleted() || deleted ).findAny();
}

private List<ContentletVersionInfo> findContentletVersionInfos(final String identifier,
Expand All @@ -448,22 +415,31 @@ private List<ContentletVersionInfo> findContentletVersionInfos(final String iden

private List<ContentletVersionInfo> findContentletVersionInfos(final String identifier, final String variantName,
final int maxResults) throws DotDataException, DotStateException {
final DotConnect dotConnect = new DotConnect();
List<ContentletVersionInfo> infos = findAllContentletVersionInfos(identifier);

if (UtilMethods.isSet(variantName)) {
dotConnect.setSQL(
"SELECT * FROM contentlet_version_info WHERE identifier=? AND variant_id = ?")
.addParam(identifier)
.addParam(variantName);
} else {
dotConnect.setSQL(
"SELECT * FROM contentlet_version_info WHERE identifier=?")
.addParam(identifier);
infos = infos.stream().filter(i-> variantName.equals(i.getVariant())).collect(Collectors.toList());
}

if (maxResults > 0) {
dotConnect.setMaxRows(maxResults);
if (maxResults > 0 && infos.size() > maxResults) {
infos = infos.subList(0, maxResults);
}
return infos;

}

/**
* this will return ALL CVIs from the database
* @param identifier
* @return
* @throws DotDataException
* @throws DotStateException
*/
private List<ContentletVersionInfo> findContentletVersionInfosInDB(final String identifier) throws DotDataException, DotStateException {

DotConnect dotConnect = new DotConnect().setSQL(
"SELECT * FROM contentlet_version_info WHERE identifier=?")
.addParam(identifier);

final List<ContentletVersionInfo> versionInfos = TransformerLocator
.createContentletVersionInfoTransformer(dotConnect.loadObjectResults()).asList();
Expand All @@ -476,7 +452,23 @@ private List<ContentletVersionInfo> findContentletVersionInfos(final String iden
@Override
protected List<ContentletVersionInfo> findAllContentletVersionInfos(final String identifier)
throws DotDataException, DotStateException {
return findContentletVersionInfos(identifier, -1);
if(identifier==null){
throw new DotRuntimeException("identifier cannot be null");
}

List<ContentletVersionInfo> infos = icache.getContentVersionInfos(identifier);
if (infos != null) {
return infos;
}
synchronized (identifier.intern()) {
infos = icache.getContentVersionInfos(identifier);
if (infos != null) {
return infos;
}
infos = findContentletVersionInfosInDB(identifier);
icache.putContentVersionInfos(identifier, infos);
return infos;
}
}

@Override
Expand Down

0 comments on commit d76f534

Please sign in to comment.