1 /*
2 * Copyright (c) 2025 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 "dynamic_cache.h"
17
18 #include "datetime_ex.h"
19 #include "parameter.h"
20 #include "refbase.h"
21 #include "sam_log.h"
22 #include "sysparam_errno.h"
23
24 using namespace std;
25 namespace OHOS {
26
QueryResult(int32_t querySaId,int32_t code)27 sptr<IRemoteObject> DynamicCache::QueryResult(int32_t querySaId, int32_t code)
28 {
29 int32_t waterLineLength = 128;
30 char waterLine[128] = {0};
31 string defaultValue = "default";
32 GetParameter(key_.c_str(), defaultValue.c_str(), waterLine, waterLineLength);
33 {
34 std::lock_guard<std::mutex> autoLock(queryCacheLock_);
35 if (CanUseCache(querySaId, waterLine, defaultValue)) {
36 HILOGD("DynamicCache QueryResult Return Cache %{public}d", querySaId);
37 return lastQuerySaProxy_;
38 }
39 }
40 HILOGD("DynamicCache QueryResult Recompute");
41 sptr<IRemoteObject> res = Recompute(querySaId, code);
42 if (res == nullptr) {
43 return nullptr;
44 }
45 {
46 std::lock_guard<std::mutex> autoLock(queryCacheLock_);
47 localPara_[key_] = waterLine;
48 if (lastQuerySaProxy_ != nullptr) {
49 HILOGD("DynamicCache RemoveDeathRecipient");
50 lastQuerySaProxy_->RemoveDeathRecipient(this);
51 }
52 lastQuerySaId_ = querySaId;
53 lastQuerySaProxy_ = res;
54 res->AddDeathRecipient(this);
55 }
56 return res;
57 }
58
CanUseCache(int32_t querySaId,char * waterLine,string defaultValue)59 bool DynamicCache::CanUseCache(int32_t querySaId, char* waterLine, string defaultValue)
60 {
61 return localPara_.count(key_) != 0 && lastQuerySaId_ == querySaId &&
62 defaultValue != string(waterLine) && string(waterLine) == localPara_[key_] &&
63 lastQuerySaProxy_ != nullptr && !lastQuerySaProxy_->IsObjectDead();
64 }
65
InvalidateCache()66 bool DynamicCache::InvalidateCache()
67 {
68 HILOGD("DynamicCache InvalidateCache Begin");
69 string tickCount = to_string(GetTickCount());
70 int32_t ret = SetParameter(key_.c_str(), tickCount.c_str());
71 if (ret != EC_SUCCESS) {
72 HILOGE("DynamicCache InvalidateCache SetParameter error:%{public}d!", ret);
73 return false;
74 }
75 HILOGD("DynamicCache InvalidateCache End");
76 return true;
77 }
78
SetKey(const string & key)79 bool DynamicCache::SetKey(const string& key)
80 {
81 int32_t maxLength = 256;
82 if (key.size() == 0 || (int32_t)key.size() > maxLength) {
83 HILOGE("DynamicCache SetKey size error:%{public}zu!", key.size());
84 return false;
85 }
86 key_ = key;
87 return true;
88 }
89 } // namespace OHOS
90