• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 DYNAMIC_CACHE_H
17 #define DYNAMIC_CACHE_H
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 
23 #include "datetime_ex.h"
24 #include "iremote_object.h"
25 #include "parameter.h"
26 #include "refbase.h"
27 #include "sam_log.h"
28 #include "sysparam_errno.h"
29 
30 namespace OHOS {
31 using namespace std;
32 template <class Query, class Result>
33 class DynamicCache {
34 public:
QueryResult(Query query,int32_t code)35     Result QueryResult(Query query, int32_t code)
36     {
37         int32_t waterLineLength = 128;
38         char waterLine[128] = {0};
39         string defaultValue = "default";
40         GetParameter(key_.c_str(), defaultValue.c_str(), waterLine, waterLineLength);
41         {
42             std::lock_guard<std::mutex> autoLock(queryCacheLock_);
43             if (localPara_.count(key_) != 0 && cacheMap_.count(query) != 0 &&
44                 defaultValue != string(waterLine) && string(waterLine) == localPara_[key_]) {
45                 HILOGD("DynamicCache QueryResult Return Cache");
46                 return cacheMap_[query];
47             }
48         }
49         HILOGD("DynamicCache QueryResult Recompute");
50         Result res = Recompute(query, code);
51         if (res == nullptr) {
52             return nullptr;
53         }
54         {
55             std::lock_guard<std::mutex> autoLock(queryCacheLock_);
56             localPara_[key_] = waterLine;
57             cacheMap_[query] = res;
58         }
59         return res;
60     }
61 
ClearCache()62     void ClearCache()
63     {
64         std::lock_guard<std::mutex> autoLock(queryCacheLock_);
65         cacheMap_.clear();
66     }
67 
InvalidateCache()68     bool InvalidateCache()
69     {
70         HILOGD("DynamicCache InvalidateCache Begin");
71         string tickCount = to_string(GetTickCount());
72         int32_t ret = SetParameter(key_.c_str(), tickCount.c_str());
73         if (ret != EC_SUCCESS) {
74             HILOGE("DynamicCache InvalidateCache SetParameter error:%{public}d!", ret);
75             return false;
76         }
77         HILOGD("DynamicCache InvalidateCache End");
78         return true;
79     }
80 
SetKey(const string & key)81     bool SetKey(const string& key)
82     {
83         int32_t maxLength = 256;
84         if (key.size() == 0 || (int32_t)key.size() > maxLength) {
85             HILOGE("DynamicCache SetKey size error:%{public}zu!", key.size());
86             return false;
87         }
88         key_ = key;
89         return true;
90     }
91 
Recompute(Query query,int32_t code)92     virtual Result Recompute(Query query, int32_t code)
93     {
94         return cacheMap_[query];
95     }
96 private:
97     map<string, string> localPara_;
98     string key_;
99     map<Query, Result> cacheMap_;
100     std::mutex queryCacheLock_;
101 };
102 }
103 #endif /* DYNAMIC_CACHE_H */