Skip to content

Commit

Permalink
Nouveau système de cache (les pages chargent plus rapidement)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperAtraction committed Aug 19, 2023
1 parent 60da1a0 commit 1ca5a64
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 25 deletions.
Binary file modified Olop
Binary file not shown.
2 changes: 1 addition & 1 deletion Olop.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 11.0.1, 2023-08-16T11:26:12. -->
<!-- Written by QtCreator 11.0.1, 2023-08-19T19:53:16. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down
1 change: 1 addition & 0 deletions assets.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
<file>assets/Store.html</file>
<file>assets/settings.html</file>
<file>assets/Olop.js</file>
<file>assets/pages.txt</file>
</qresource>
</RCC>
109 changes: 86 additions & 23 deletions assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -429,24 +429,78 @@
var themeMode = 1;
var FirstTATheme = 21;
var secondTATheme = 8;
var pageCache = {};
var useCache = 1;

function loadPage(page) {
var loader = $("#loader");
var content = $("#content");
loader.show();
content.hide();

content.load(page + ".html", function(response, status, xhr) {
loader.hide();
if (status === "error") {
var errorMessage = "Erreur: " + xhr.status + " " + xhr.statusText;
showNotification(-1, "Erreur "+xhr.status, errorMessage);
} else {
content.show();
var loader = $("#loader");
var content = $("#content");

loader.show();
content.hide();

if (useCache === 1 && pageCache[page]) {
content.html(pageCache[page]);
loader.hide();
content.show();
} else {
content.load(page + ".html", function(response, status, xhr) {
loader.hide();
if (status === "error") {
var errorMessage = "Erreur: " + xhr.status + " " + xhr.statusText;
showNotification(-1, "Erreur "+xhr.status, errorMessage);
} else {
if (useCache === 1) {
// Si useCache est défini à 1, stockez le contenu chargé dans le cache
pageCache[page] = response;
}
});
}
content.show();
}
});
}
}

function preloadPage(page) {
if (useCache === 1) {
$.get(page + ".html", function(response) {
pageCache[page] = response;
});
}
}

function clearPageCache() {
pageCache = {};
}

function preloadAllPages() {
return new Promise((resolve) => {
$.get("pages.txt", function(data) {
// Divisez les données en lignes
var pages = data.split("\n");

var promises = []; // Utilisé pour stocker toutes les promesses de préchargement

// Préchargez chaque page
for (var i = 0; i < pages.length; i++) {
// Enlevez les espaces blancs inutiles
var page = pages[i].trim();

if (page) { // Ignorez les lignes vides
promises.push($.get(page + ".html", function(response) {
if (useCache === 1) {
pageCache[page] = response;
}
}));
}
}

// Lorsque toutes les pages sont préchargées, masquez le loader et résolvez la promesse
$.when(...promises).then(() => {
resolve();
});
});
});
}
// Afficher ou masquer le menu
function toggleMenu() {
var sidebar = document.getElementById("sidebar");
Expand Down Expand Up @@ -1094,15 +1148,24 @@

setTimeout(() => {
updateTheme();
setInterval(updateTheme, 60000); // Vérifie le thème chaque minute
loadPage("home");
updateTime();
update();
toggleMenu();

// Masquer le loader
var loader = document.getElementById('loader');
loader.style.display = 'none';
setInterval(updateTheme, 60000); // Vérifie le thème chaque minute

var loader = document.getElementById('loader');
loader.style.display = 'block'; // Affichez le loader avant de précharger les pages

preloadAllPages().then(() => {
// Lorsque toutes les pages sont préchargées, continuez avec le reste de la logique

// Charger la page 'home'
loadPage("home");

updateTime();
update();
toggleMenu();

// Masquer le loader
loader.style.display = 'none';
});

}, 10);

Expand Down
2 changes: 2 additions & 0 deletions assets/pages.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
home
Library
1 change: 0 additions & 1 deletion mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,3 @@ void MainWindow::on_actionQuitter_triggered()
{
exit(0);
}

0 comments on commit 1ca5a64

Please sign in to comment.