-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.cpp
More file actions
42 lines (37 loc) · 847 Bytes
/
service.cpp
File metadata and controls
42 lines (37 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "service.h"
#include <iostream>
void Service::lockMtx()
{
mtx.try_lock();
}
void Service::unlockMtx()
{
mtx.unlock();
}
void Service::SET(const std::string &clientName, const std::string &key, const std::string &value)
{
lockMtx();
cache.upsert(key, value);
unlockMtx();
};
std::string Service::GET(const std::string &clientName, const std::string &key)
{
return cache.find(key);
}
bool Service::INCDEC(const std::string &clientName, const std::string &key, const bool isInc)
{
lockMtx();
int intValue;
try
{
intValue = std::stoi(cache.find(key));
}
catch (std::exception e)
{
unlockMtx();
return false;
}
cache.upsert(key, std::to_string(intValue + (isInc ? 1 : -1)));
unlockMtx();
return true;
};