Skip to content

Commit

Permalink
Feature #12603: optimizing directory requesting performances by using…
Browse files Browse the repository at this point in the history
… Domain Driver improvements
  • Loading branch information
SilverYoCha committed Sep 29, 2021
1 parent 6af3023 commit fa29e67
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 7 deletions.
Expand Up @@ -534,10 +534,6 @@ public DirectoryItemList getCommonContacts(String userId) {
return lastAllListUsersCalled;
}

private UserFull getUserFul(String userId) {
return getOrganisationController().getUserFull(userId);
}

/**
* @param compoId
* @param txtTitle
Expand Down Expand Up @@ -601,6 +597,11 @@ public DirectoryItemList getConnectedUsers() {
}

public SilverpeasList<UserFragmentVO> getFragments(SilverpeasList<DirectoryItem> items) {
final DirectoryUserFullRequestCache userFullCache = DirectoryUserFullRequestCache.get();
items.stream()
.filter(UserItem.class::isInstance)
.map(UserItem.class::cast)
.forEach(userFullCache::addUserItem);
return items.stream().map(asFragment).filter(Objects::nonNull)
.collect(SilverpeasList.collector(items));
}
Expand Down Expand Up @@ -632,7 +633,7 @@ private UserFragmentVO getUserFragment(UserItem user, SilverpeasTemplate templat
template.setAttribute("invitationReceived", null);
}

UserFull userFull = getUserFul(user.getOriginalId());
UserFull userFull = user.getUserFull();
Map<String, String> extra = new HashMap<>();
if (userFull != null) {
extra = userFull.getAllDefinedValues(getLanguage());
Expand Down
@@ -0,0 +1,88 @@
/*
* Copyright (C) 2000 - 2021 Silverpeas
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* As a special exception to the terms and conditions of version 3.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* Open Source Software ("FLOSS") applications as described in Silverpeas's
* FLOSS exception. You should have received a copy of the text describing
* the FLOSS exception, and it is also available here:
* "https://www.silverpeas.org/legal/floss_exception.html"
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.silverpeas.web.directory.control;

import org.silverpeas.core.admin.user.model.UserFull;
import org.silverpeas.core.cache.service.CacheServiceProvider;
import org.silverpeas.web.directory.model.UserItem;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* This class permits to handles {@link UserFull} caches.
* <p> Callers puts in a first time all the Silverpeas's user ids, and then, on the first
* {@link UserFull} data access, all the data are retrieved in a single repository request. </p>
* @author silveryocha
*/
public class DirectoryUserFullRequestCache {

public static DirectoryUserFullRequestCache get() {
return CacheServiceProvider.getRequestCacheService()
.getCache()
.computeIfAbsent(DirectoryUserFullRequestCache.class.getName(),
DirectoryUserFullRequestCache.class, DirectoryUserFullRequestCache::new);
}

private final Set<String> waitingIds = Collections.synchronizedSet(new HashSet<>());
private final Map<String, UserFull> cache = Collections.synchronizedMap(new HashMap<>());

private DirectoryUserFullRequestCache() {
// hidden constructor
}

/**
* Adds a user handled by the cache.
* @param user a {@link UserItem} instance.
*/
public void addUserItem(final UserItem user) {
final String originalId = user.getOriginalId();
if (!cache.containsKey(originalId)) {
waitingIds.add(originalId);
}
}

/**
* Gets the full user data of the user represented by the given item.
* @param user a user item.
* @return a {@link UserFull} if any, null otherwise.
*/
public UserFull getUserFull(final UserItem user) {
return getUserFull(user.getOriginalId());
}

private UserFull getUserFull(final String userId) {
synchronized (waitingIds) {
if (!waitingIds.isEmpty()) {
UserFull.getByIds(waitingIds).forEach(u -> cache.put(u.getId(), u));
waitingIds.clear();
}
}
return cache.get(userId);
}
}
Expand Up @@ -27,6 +27,7 @@
import org.silverpeas.core.admin.user.model.UserDetail;
import org.silverpeas.core.admin.user.model.UserFull;
import org.silverpeas.core.util.StringUtil;
import org.silverpeas.web.directory.control.DirectoryUserFullRequestCache;

import java.util.Date;
import java.util.Optional;
Expand Down Expand Up @@ -127,11 +128,11 @@ public UserDetail getUserDetail() {

/**
* Gets full data of the user associated to the item.
* @return
* @return a {@link UserFull} instance.
*/
public UserFull getUserFull(){
if (userFull == null) {
userFull = UserFull.getById(getOriginalId());
userFull = DirectoryUserFullRequestCache.get().getUserFull(this);
}
return userFull;
}
Expand Down

0 comments on commit fa29e67

Please sign in to comment.