Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General: Cache course icons and profile pictures to improve performance #9459

Merged
merged 4 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import jakarta.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.http.CacheControl;
Expand All @@ -32,6 +33,9 @@ public PublicResourcesConfiguration(JHipsterProperties jHipsterProperties) {
this.jHipsterProperties = jHipsterProperties;
}

@Value("${artemis.file-upload-path}")
private String fileUploadPath;

@Override
public void addResourceHandlers(@NotNull ResourceHandlerRegistry registry) {
// Enable static resource serving in general from "/public" from both classpath and hosts filesystem
Expand All @@ -46,6 +50,16 @@ public void addResourceHandlers(@NotNull ResourceHandlerRegistry registry) {

addResourceHandlerForPath(registry, "images", "about").setCacheControl(defaultCacheControl);
addResourceHandlerForPath(registry, "emoji").setCacheControl(defaultCacheControl);

// Add caching for course icons, user profile pictures, and drag and drop quiz pictures
// Add resource handlers for dynamic image paths based on fileUploadPath
// TODO: those paths have to be the same as in FilePathService, ideally we reuse the constants and define them only once
registry.addResourceHandler("/images/course/icons/**").addResourceLocations("file:" + fileUploadPath + "/images/course/icons/").setCacheControl(defaultCacheControl);
krusche marked this conversation as resolved.
Show resolved Hide resolved

registry.addResourceHandler("/images/user/profile-pictures/**").addResourceLocations("file:" + fileUploadPath + "/images/user/profile-pictures/")
.setCacheControl(defaultCacheControl);

registry.addResourceHandler("/images/drag-and-drop/**").addResourceLocations("file:" + fileUploadPath + "/images/drag-and-drop/").setCacheControl(defaultCacheControl);
krusche marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -646,7 +647,8 @@ private ResponseEntity<byte[]> responseEntityForFilePath(Path filePath) {
if (file == null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(file);
return ResponseEntity.ok().cacheControl(CacheControl.maxAge(30, TimeUnit.DAYS)) // Cache for 30 days;
.contentType(getMediaTypeFromFilename(filePath.getFileName().toString())).body(file);
krusche marked this conversation as resolved.
Show resolved Hide resolved
}
catch (IOException e) {
log.error("Failed to return requested file with path {}", filePath, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export class ConversationMessagesComponent implements OnInit, AfterViewInit, OnD
this.createEmptyPost();
}
}

private subscribeToMetis() {
this.metisService.posts.pipe(takeUntil(this.ngUnsubscribe)).subscribe((posts: Post[]) => {
this.setPosts(posts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class MetisConversationService implements OnDestroy {

public setActiveConversation(conversationIdentifier: ConversationDTO | number | undefined) {
this.updateLastReadDateAndNumberOfUnreadMessages();
let cachedConversation = undefined;
let cachedConversation: ConversationDTO | undefined = undefined;
if (conversationIdentifier) {
const parameterJustId = typeof conversationIdentifier === 'number';
cachedConversation = this.conversationsOfUser.find(
Expand Down
Loading