1 /*
2 * Copyright (c) 2022 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 #include "lru_cache_disk_handler.h"
17
18 #include <thread>
19
20 #include "netstack_log.h"
21
22 namespace OHOS::NetStack {
LRUCacheDiskHandler(std::string fileName,size_t capacity)23 LRUCacheDiskHandler::LRUCacheDiskHandler(std::string fileName, size_t capacity)
24 : diskHandler_(std::move(fileName)),
25 capacity_(std::max<size_t>(std::min<size_t>(MAX_DISK_CACHE_SIZE, capacity), MIN_DISK_CACHE_SIZE))
26 {
27 }
28
SetCapacity(size_t capacity)29 void LRUCacheDiskHandler::SetCapacity(size_t capacity)
30 {
31 capacity_ = std::max<size_t>(std::min<size_t>(MAX_DISK_CACHE_SIZE, capacity), MIN_DISK_CACHE_SIZE);
32 WriteCacheToJsonFile();
33 }
34
Delete()35 void LRUCacheDiskHandler::Delete()
36 {
37 cache_.Clear();
38 diskHandler_.Delete();
39 }
40
ReadJsonValueFromFile()41 Json::Value LRUCacheDiskHandler::ReadJsonValueFromFile()
42 {
43 std::string jsonStr = diskHandler_.Read();
44 Json::Value root;
45 JSONCPP_STRING err;
46 Json::CharReaderBuilder builder;
47 std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
48 if (!reader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.size(), &root, &err)) {
49 NETSTACK_LOGE("parse json not success, maybe file is broken: %{public}s", err.c_str());
50 return {};
51 }
52 return root;
53 }
54
WriteJsonValueToFile(const Json::Value & root)55 void LRUCacheDiskHandler::WriteJsonValueToFile(const Json::Value &root)
56 {
57 Json::StreamWriterBuilder builder;
58 std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
59 std::stringstream s;
60 int res = writer->write(root, &s);
61 if (res != 0) {
62 NETSTACK_LOGE("write json failed %{public}d", res);
63 return;
64 }
65 diskHandler_.Write(s.str());
66 }
67
WriteCacheToJsonFile()68 void LRUCacheDiskHandler::WriteCacheToJsonFile()
69 {
70 LRUCache oldCache(capacity_);
71 oldCache.ReadCacheFromJsonValue(ReadJsonValueFromFile());
72 oldCache.MergeOtherCache(cache_);
73 Json::Value root = oldCache.WriteCacheToJsonValue();
74 WriteJsonValueToFile(root);
75 cache_.Clear();
76 }
77
ReadCacheFromJsonFile()78 void LRUCacheDiskHandler::ReadCacheFromJsonFile()
79 {
80 cache_.ReadCacheFromJsonValue(ReadJsonValueFromFile());
81 }
82
Get(const std::string & key)83 std::unordered_map<std::string, std::string> LRUCacheDiskHandler::Get(const std::string &key)
84 {
85 auto valueFromMemory = cache_.Get(key);
86 if (!valueFromMemory.empty()) {
87 return valueFromMemory;
88 }
89
90 LRUCache diskCache(capacity_);
91 diskCache.ReadCacheFromJsonValue(ReadJsonValueFromFile());
92 auto valueFromDisk = diskCache.Get(key);
93 cache_.Put(key, valueFromDisk);
94 return valueFromDisk;
95 }
96
Put(const std::string & key,const std::unordered_map<std::string,std::string> & value)97 void LRUCacheDiskHandler::Put(const std::string &key, const std::unordered_map<std::string, std::string> &value)
98 {
99 cache_.Put(key, value);
100 }
101 } // namespace OHOS::NetStack
102