1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef LRU_CACHE_DISK_HANDLER_H
17 #define LRU_CACHE_DISK_HANDLER_H
18 
19 #include <list>
20 #include <string>
21 #include <unordered_map>
22 #include "cJSON.h"
23 #include <mutex>
24 
25 namespace OHOS::NetStack::Http {
26 
27 constexpr const char *LRU_INDEX = "LRUIndex";
28 constexpr const int DECIMAL_BASE = 10;
29 constexpr const int MAX_SIZE = 1024 * 1024;
30 constexpr const size_t INVALID_SIZE = SIZE_MAX;
31 constexpr const int MAX_DISK_CACHE_SIZE = 1024 * 1024 * 10;
32 constexpr const int MIN_DISK_CACHE_SIZE = 1024 * 1024;
33 
34 class LRUCache {
35 public:
36     LRUCache();
37 
38     explicit LRUCache(size_t capacity);
39 
40     std::unordered_map<std::string, std::string> Get(const std::string &key);
41 
42     void Put(const std::string &key, const std::unordered_map<std::string, std::string> &value);
43 
44     void MergeOtherCache(const LRUCache &other);
45 
46     cJSON* WriteCacheToJsonValue();
47 
48     void ReadCacheFromJsonValue(const cJSON* root);
49 
50     void Clear();
51 
52 private:
53     struct Node {
54         std::string key;
55         std::unordered_map<std::string, std::string> value;
56 
57         Node() = delete;
58 
59         Node(std::string key, std::unordered_map<std::string, std::string> value);
60     };
61 
62     void AddNode(const Node &node);
63 
64     void MoveNodeToHead(const std::list<Node>::iterator &it);
65 
66     void EraseTailNode();
67 
68     std::mutex mutex_;
69     std::unordered_map<std::string, std::list<Node>::iterator> cache_;
70     std::list<Node> nodeList_;
71     size_t capacity_;
72     size_t size_;
73 };
74 
75 class DiskHandler final {
76 public:
77     DiskHandler() = delete;
78 
79     explicit DiskHandler(std::string fileName);
80 
81     void Write(const std::string &str);
82 
83     void Delete();
84 
85     [[nodiscard]] std::string Read();
86 
87 private:
88     std::mutex mutex_;
89 
90     std::string fileName_;
91 };
92 
93 class LRUCacheDiskHandler {
94 public:
95     LRUCacheDiskHandler() = delete;
96 
97     LRUCacheDiskHandler(std::string fileName, size_t capacity);
98 
99     void WriteCacheToJsonFile();
100 
101     void ReadCacheFromJsonFile();
102 
103     void Delete();
104 
105     void SetCapacity(size_t capacity);
106 
107     std::unordered_map<std::string, std::string> Get(const std::string &key);
108 
109     void Put(const std::string &key, const std::unordered_map<std::string, std::string> &value);
110 
111 private:
112     LRUCache cache_;
113     DiskHandler diskHandler_;
114     std::atomic<size_t> capacity_;
115 
116     cJSON* ReadJsonValueFromFile();
117 
118     void WriteJsonValueToFile(const cJSON *root);
119 };
120 
121 } // namespace OHOS::NetStack::Http
122 #endif /* LRU_CACHE_DISK_HANDLER_H */
123