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 #ifndef API_CACHE_MANAGER_H
16 #define API_CACHE_MANAGER_H
17 
18 #include <string>
19 #include <vector>
20 #include "expire_lru_cache.h"
21 #include "message_parcel.h"
22 
23 namespace OHOS {
24 class ApiCacheManager {
25 public:
26     static ApiCacheManager& GetInstance();
27 
28     void AddCacheApi(const std::u16string& descriptor, uint32_t apiCode, int64_t expireTimeSec);
29 
30     void DelCacheApi(const std::u16string& descriptor, uint32_t apiCode);
31 
32     void ClearCache();
33 
34     void ClearCache(const std::u16string& descriptor);
35 
36     void ClearCache(const std::u16string& descriptor, int32_t apiCode);
37 
38     bool PreSendRequest(const std::u16string& descriptor, uint32_t apiCode, const MessageParcel &data,
39         MessageParcel& reply);
40     bool PostSendRequest(const std::u16string& descriptor, uint32_t apiCode, const MessageParcel &data,
41         MessageParcel& reply);
42 private:
43     ApiCacheManager() = default;
~ApiCacheManager()44     ~ApiCacheManager()
45     {
46         for (auto i:caches_) {
47             delete i.second;
48         }
49     };
50     ApiCacheManager(const ApiCacheManager&) = delete;
51     ApiCacheManager& operator= (const ApiCacheManager&) = delete;
52     ApiCacheManager(ApiCacheManager&&) = delete;
53     ApiCacheManager& operator= (ApiCacheManager&&) = delete;
54 
55     std::mutex cachesMutex_;
56     std::map<std::pair<std::u16string, uint32_t>, ExpireLruCache<std::vector<uint8_t>, std::vector<uint8_t>>*> caches_;
57 };
58 }
59 
60 #endif