mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-19 01:22: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)
|
while (1)
|
||||||
{
|
{
|
||||||
std::this_thread::sleep_for(std::chrono::minutes(saving_freq));
|
std::this_thread::sleep_for(std::chrono::minutes(saving_freq));
|
||||||
|
if (bank.GetChangeState())
|
||||||
|
{
|
||||||
bank.Save();
|
bank.Save();
|
||||||
|
}
|
||||||
std::cout << "Saving " << std::time(0) << '\n';
|
std::cout << "Saving " << std::time(0) << '\n';
|
||||||
}
|
}
|
||||||
}).detach();
|
}).detach();
|
||||||
|
|
|
||||||
24
src/bank.cpp
24
src/bank.cpp
|
|
@ -1,18 +1,13 @@
|
||||||
#include "bank.h"
|
#include "bank.h"
|
||||||
|
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
void Bank::ChangesMade() noexcept
|
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); }
|
||||||
return change_flag.store(1, std::memory_order_release);
|
bool Bank::GetChangeState() noexcept { return change_flag.load(std::memory_order_acquire); }
|
||||||
}
|
#else
|
||||||
void Bank::ChangesSaved() noexcept
|
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); }
|
||||||
return change_flag.store(1, std::memory_order_release);
|
bool Bank::GetChangeState() noexcept { return 1; }
|
||||||
}
|
|
||||||
bool Bank::GetChangeState() noexcept
|
|
||||||
{
|
|
||||||
return change_flag.load(std::memory_order_acquire);
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int_fast8_t Bank::AddUser(const std::string &name, const std::string &init_pass) noexcept
|
int_fast8_t Bank::AddUser(const std::string &name, const std::string &init_pass) noexcept
|
||||||
|
|
@ -26,14 +21,12 @@ int_fast8_t Bank::AddUser(const std::string &name, const std::string &init_pass)
|
||||||
return ErrorResponse::InvalidRequest;
|
return ErrorResponse::InvalidRequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
|
||||||
std::shared_lock<std::shared_mutex> lock{size_l};
|
std::shared_lock<std::shared_mutex> lock{size_l};
|
||||||
return (users.try_emplace_l(
|
return (users.try_emplace_l(
|
||||||
name, [](User &) {}, init_pass))
|
name, [](User &) {}, init_pass))
|
||||||
? true
|
? true
|
||||||
: ErrorResponse::UserAlreadyExists;
|
: ErrorResponse::UserAlreadyExists;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
int_fast8_t Bank::AdminAddUser(const std::string &attempt, std::string &&name, uint32_t init_bal, std::string &&init_pass) noexcept
|
int_fast8_t Bank::AdminAddUser(const std::string &attempt, std::string &&name, uint32_t init_bal, std::string &&init_pass) noexcept
|
||||||
{
|
{
|
||||||
if (name.size() > max_name_size)
|
if (name.size() > max_name_size)
|
||||||
|
|
@ -253,6 +246,8 @@ Json::Value Bank::GetLogs(const std::string &name, const std::string &attempt) n
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bank::Save()
|
void Bank::Save()
|
||||||
|
{
|
||||||
|
if (GetChangeState())
|
||||||
{
|
{
|
||||||
Json::Value temp;
|
Json::Value temp;
|
||||||
|
|
||||||
|
|
@ -280,6 +275,7 @@ void Bank::Save()
|
||||||
user_save.close();
|
user_save.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//NOT THREAD SAFE, BY NO MEANS SHOULD THIS BE CALLED WHILE RECEIEVING REQUESTS
|
//NOT THREAD SAFE, BY NO MEANS SHOULD THIS BE CALLED WHILE RECEIEVING REQUESTS
|
||||||
void Bank::Load()
|
void Bank::Load()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue