Skip to content

Commit

Permalink
Move caching functions into namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
teusbenschop committed May 31, 2024
1 parent 64e2121 commit 0b5220c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
35 changes: 22 additions & 13 deletions database/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,16 +327,13 @@ void remove (std::string schema)
}


} // namespace.


// Deletes expired cached items.
void database_cache_trim (bool clear)
void trim (bool clear)
{
if (clear) Database_Logs::log ("Clearing cache");

std::string output, error;

// The directory that contains the file-based cache files.
std::string path = database::cache::file::full_path ("");

Expand Down Expand Up @@ -370,7 +367,7 @@ void database_cache_trim (bool clear)
if (percentage_disk_in_use < 70) minutes = "+1440";
// One week.
if (percentage_disk_in_use < 50) minutes = "+10080";

// Handle clearing the cache immediately.
if (clear) minutes = "+0";

Expand Down Expand Up @@ -398,7 +395,7 @@ void database_cache_trim (bool clear)

// The directory that contains the database-based cache files.
path = filter_url_create_root_path ({database_logic_databases ()});

// The number of days to keep cached data depends on the percentage of the disk in use.
// There have been instances that the cache takes up 4, 5, or 6 Gbytes in the Cloud.
// This can be even more.
Expand All @@ -412,10 +409,10 @@ void database_cache_trim (bool clear)
if (percentage_disk_in_use > 80) days = "+14";
if (percentage_disk_in_use > 85) days = "+7";
if (percentage_disk_in_use > 90) days = "+1";

// Handle clearing the cache immediately.
if (clear) days = "0";

Database_Logs::log ("Will remove resource caches not accessed for " + days + " days");

// Remove database-based cached files that have not been modified for x days.
Expand All @@ -429,18 +426,30 @@ void database_cache_trim (bool clear)
}


} // namespace.


namespace database::cache {


// This returns true if the $html can be cached.
bool database_cache_can_cache (const std::string& error, const std::string& html)
bool can_cache (const std::string& error, const std::string& html)
{
// Normally if everything is fine, then caching is possible.
bool cache = true;

// Do not cache the data in an error situation.
if (!error.empty()) cache = false;

// Do not cache the data if Cloudflare does DDoS protection.
// https://github.com/bibledit/cloud/issues/693.
if (html.find ("Cloudflare") != std::string::npos) cache = false;

return cache;
}


} // namespace.



8 changes: 4 additions & 4 deletions database/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ namespace database::cache::file {
bool exists (std::string schema);
void put (std::string schema, const std::string& contents);
std::string get (std::string schema);
void trim (bool clear);

}

namespace database::cache {

bool can_cache (const std::string & error, const std::string & html);

void database_cache_trim (bool clear);


bool database_cache_can_cache (const std::string & error, const std::string & html);
}
4 changes: 2 additions & 2 deletions resource/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ std::string resource_logic_client_fetch_cache_from_cloud (std::string resource,

// Cache the content under circumstances.
if (cache) {
if (database_cache_can_cache (error, content)) {
if (database::cache::can_cache (error, content)) {
database::cache::sql::cache (resource, book, chapter, verse, content);
}
}
Expand Down Expand Up @@ -757,7 +757,7 @@ std::string resource_logic_web_or_cache_get (std::string url, std::string& error

#ifdef HAVE_CLOUD
// In the Cloud, cache the response based on certain criteria.
bool cache = database_cache_can_cache (error, html);
const bool cache = database::cache::can_cache (error, html);
if (cache) {
database::cache::file::put (url, html);
}
Expand Down
4 changes: 2 additions & 2 deletions tasks/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,10 @@ void tasks_run_one (const std::string& filename)
changes_clear_notifications_user (parameter1, parameter2);
}
else if (command == CLEARCACHES) {
database_cache_trim (true);
database::cache::file::trim (true);
}
else if (command == TRIMCACHES) {
database_cache_trim (false);
database::cache::file::trim (false);
}
else if (command == EXPORT2NMT) {
nmt_logic_export (parameter1, parameter2);
Expand Down
2 changes: 1 addition & 1 deletion unittests/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ TEST (database, cache)
database::cache::file::put (url, contents);
EXPECT_EQ (true, database::cache::file::exists (url));
EXPECT_EQ (contents, database::cache::file::get (url));
database_cache_trim (false);
database::cache::file::trim (false);
}

// Excercise the ready-flag.
Expand Down

0 comments on commit 0b5220c

Please sign in to comment.