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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_LRU_COUNT_LIMIT_LRU_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_LRU_COUNT_LIMIT_LRU_H
18 #include <atomic>
19 #include <list>
20 #include <string>
21 #include <unordered_map>
22 #include <utility>
23 
24 #include "base/memory/ace_type.h"
25 
26 namespace OHOS::Ace {
27 template<typename T>
28 struct CacheNode {
CacheNodeCacheNode29     CacheNode(std::string key, const T& obj) : cacheKey(std::move(key)), cacheObj(obj) {}
30     std::string cacheKey;
31     T cacheObj;
32 };
33 
34 class CountLimitLRU : public AceType {
35     DECLARE_ACE_TYPE(CountLimitLRU, AceType);
36 
37 public:
38     template<typename T>
39     static void CacheWithCountLimitLRU(const std::string& key, const T& cacheObj, std::list<CacheNode<T>>& cacheList,
40         std::unordered_map<std::string, typename std::list<CacheNode<T>>::iterator>& cache,
41         const std::atomic<size_t>& capacity);
42 
43     template<typename T>
44     static T GetCacheObjWithCountLimitLRU(const std::string& key, std::list<CacheNode<T>>& cacheList,
45         std::unordered_map<std::string, typename std::list<CacheNode<T>>::iterator>& cache);
46 
47     template<typename T>
48     static void RemoveCacheObjFromCountLimitLRU(const std::string& key, std::list<CacheNode<T>>& cacheList,
49         std::unordered_map<std::string, typename std::list<CacheNode<T>>::iterator>& cache);
50 };
51 } // namespace OHOS::Ace
52 
53 #include "count_limit_lru.inl"
54 #endif
55