-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.cpp
52 lines (42 loc) · 1.25 KB
/
redis.cpp
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
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <unordered_map>
#include <string>
class Redis {
public:
std::unordered_map<std::string, std::string> store;
std::string set(const std::string& key, const std::string& value) {
return store[key] = value;
}
void get(const std::string& key) {
auto it = store.find(key);
if (it != store.end()) {
std::cout << it->second << std::endl;
} else {
std::cout << "Key '" << key << "' not found." << std::endl;
}
}
int delete_item(const std::string& key) {
auto it = store.find(key);
if (it != store.end()) {
store.erase(it);
return 1; // Indicate success
} else {
return 0; // Indicate failure (key not found)
}
}
};
int main() {
Redis r;
r.set("key", "value");
std::cout << "After setting key 'key' to 'value':" << std::endl;
r.get("key");
int delete_result = r.delete_item("key");
if (delete_result == 1) {
std::cout << "Key 'key' deleted successfully." << std::endl;
} else {
std::cout << "Key 'key' not found. Nothing deleted." << std::endl;
}
std::cout << "After deleting key 'key':" << std::endl;
r.get("key");
return 0;
}