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 #ifndef COMMUNICATIONNETSTACK_LRU_CACHE_DISK_HANDLER_H 17 #define COMMUNICATIONNETSTACK_LRU_CACHE_DISK_HANDLER_H 18 19 #include <atomic> 20 21 #include "disk_handler.h" 22 #include "lru_cache.h" 23 24 static constexpr const int MAX_DISK_CACHE_SIZE = 1024 * 1024 * 10; 25 static constexpr const int MIN_DISK_CACHE_SIZE = 1024 * 1024; 26 27 namespace OHOS::NetStack { 28 class LRUCacheDiskHandler { 29 public: 30 LRUCacheDiskHandler() = delete; 31 32 LRUCacheDiskHandler(std::string fileName, size_t capacity); 33 34 void WriteCacheToJsonFile(); 35 36 void ReadCacheFromJsonFile(); 37 38 void Delete(); 39 40 void SetCapacity(size_t capacity); 41 42 std::unordered_map<std::string, std::string> Get(const std::string &key); 43 44 void Put(const std::string &key, const std::unordered_map<std::string, std::string> &value); 45 46 private: 47 LRUCache cache_; 48 DiskHandler diskHandler_; 49 std::atomic<size_t> capacity_; 50 51 Json::Value ReadJsonValueFromFile(); 52 53 void WriteJsonValueToFile(const Json::Value &root); 54 }; 55 } // namespace OHOS::NetStack 56 #endif /* COMMUNICATIONNETSTACK_LRU_CACHE_DISK_HANDLER_H */ 57