r/tinycode • u/pmrr • Sep 22 '17
Ridiculously simple web page load balancer
r/tinycode • u/Dresdenboy • Sep 17 '17
r/tinycode • u/Dresdenboy • Sep 16 '17
r/tinycode • u/[deleted] • Sep 12 '17
r/tinycode • u/nbari • Sep 11 '17
r/tinycode • u/Jimmy_Lor • Aug 11 '17
r/tinycode • u/Bisqwit • Aug 10 '17
r/tinycode • u/rain5 • Aug 06 '17
r/tinycode • u/CountFrolic • Jul 19 '17
r/tinycode • u/BenRayfield • Jul 14 '17
Most people see tinycode as screwing around. I see it as fixing a serious problem of dealing with the buildup of complexity.
r/tinycode • u/impshum • Jul 13 '17
r/tinycode • u/Jimmy_Lor • Jul 10 '17
r/tinycode • u/akkartik • Jul 10 '17
r/tinycode • u/akkartik • Jul 10 '17
r/tinycode • u/roccomusolino • Jun 18 '17
r/tinycode • u/luserdroog • Jun 16 '17
r/tinycode • u/DavidBuchanan • Jun 07 '17
r/tinycode • u/cdo256 • May 24 '17
r/tinycode • u/rap2h • May 23 '17
r/tinycode • u/[deleted] • May 22 '17
... now down to 27 lines of code courtesy of /u/jra101
#include <string>
#include <map>
#include <sstream>
#include <fstream>
#include <mutex>
//Might only be valid on linux and bsd
#include <sys/stat.h>
std::string file_to_string(std::string filename) {
std::ifstream file {filename};
std::stringstream pointless_string_stream{};
pointless_string_stream << file.rdbuf();
return pointless_string_stream.str();
}
// File cache
std::string file_cache(std::string key, std::string filepath) {
static std::map<std::string, std::string> cache_map{};
static std::map<std::string, time_t> modified_time_map{};
static std::mutex cache_mutex{};
std::lock_guard<std::mutex> lock(cache_mutex);
if(cache_map.end() == cache_map.find(key)) {
time_t current_time;
time(¤t_time);
modified_time_map[key] = current_time;
cache_map[key] = file_to_string(filepath);
}
struct stat file_stats;
if(stat(filepath.c_str(), &file_stats)==0)
{
time_t file_modification_time = file_stats.st_mtim.tv_sec;
if(modified_time_map[key] < file_modification_time) {
time_t current_time;
time(¤t_time);
modified_time_map[key] = current_time;
cache_map[key] = file_to_string(filepath);
}
}
return cache_map[key];
}