mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-18 09:02:14 +11:00
✨ implementing change state saving
This commit is contained in:
parent
57a0ab5d54
commit
7aadd63cd5
2 changed files with 40 additions and 41 deletions
3
main.cpp
3
main.cpp
|
|
@ -62,7 +62,10 @@ int main(int argc, char **argv)
|
|||
while (1)
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::minutes(saving_freq));
|
||||
if (bank.GetChangeState())
|
||||
{
|
||||
bank.Save();
|
||||
}
|
||||
std::cout << "Saving " << std::time(0) << '\n';
|
||||
}
|
||||
}).detach();
|
||||
|
|
|
|||
24
src/bank.cpp
24
src/bank.cpp
|
|
@ -1,18 +1,13 @@
|
|||
#include "bank.h"
|
||||
|
||||
#if CONSERVATIVE_DISK_SAVE
|
||||
void Bank::ChangesMade() noexcept
|
||||
{
|
||||
return change_flag.store(1, std::memory_order_release);
|
||||
}
|
||||
void Bank::ChangesSaved() noexcept
|
||||
{
|
||||
return change_flag.store(1, std::memory_order_release);
|
||||
}
|
||||
bool Bank::GetChangeState() noexcept
|
||||
{
|
||||
return change_flag.load(std::memory_order_acquire);
|
||||
}
|
||||
void Bank::ChangesMade() noexcept { return change_flag.store(1, std::memory_order_release); }
|
||||
void Bank::ChangesSaved() noexcept { return change_flag.store(1, std::memory_order_release); }
|
||||
bool Bank::GetChangeState() noexcept { return change_flag.load(std::memory_order_acquire); }
|
||||
#else
|
||||
void Bank::ChangesMade() noexcept { return change_flag.store(1, std::memory_order_release); }
|
||||
void Bank::ChangesSaved() noexcept { return change_flag.store(1, std::memory_order_release); }
|
||||
bool Bank::GetChangeState() noexcept { return 1; }
|
||||
#endif
|
||||
|
||||
int_fast8_t Bank::AddUser(const std::string &name, const std::string &init_pass) noexcept
|
||||
|
|
@ -26,13 +21,11 @@ int_fast8_t Bank::AddUser(const std::string &name, const std::string &init_pass)
|
|||
return ErrorResponse::InvalidRequest;
|
||||
}
|
||||
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> lock{size_l};
|
||||
return (users.try_emplace_l(
|
||||
name, [](User &) {}, init_pass))
|
||||
? true
|
||||
: ErrorResponse::UserAlreadyExists;
|
||||
}
|
||||
}
|
||||
int_fast8_t Bank::AdminAddUser(const std::string &attempt, std::string &&name, uint32_t init_bal, std::string &&init_pass) noexcept
|
||||
{
|
||||
|
|
@ -254,6 +247,8 @@ Json::Value Bank::GetLogs(const std::string &name, const std::string &attempt) n
|
|||
|
||||
void Bank::Save()
|
||||
{
|
||||
if (GetChangeState())
|
||||
{
|
||||
Json::Value temp;
|
||||
|
||||
//loading info into json temp
|
||||
|
|
@ -279,6 +274,7 @@ void Bank::Save()
|
|||
writer->write(temp, &user_save);
|
||||
user_save.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//NOT THREAD SAFE, BY NO MEANS SHOULD THIS BE CALLED WHILE RECEIEVING REQUESTS
|
||||
|
|
|
|||
Loading…
Reference in a new issue