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 #ifndef OHOS_ROSEN_LRUCACHE_H 17 #define OHOS_ROSEN_LRUCACHE_H 18 19 #include <list> 20 #include <mutex> 21 #include <unordered_map> 22 23 namespace OHOS::Rosen { 24 namespace { 25 constexpr int32_t UNDEFINED_REMOVED_KEY = -1; 26 } // namespace 27 class LruCache { 28 public: LruCache(std::size_t capacity)29 LruCache(std::size_t capacity) : capacity_(capacity) {} 30 31 bool Visit(int32_t key); 32 int32_t Put(int32_t key); 33 void Remove(int32_t key); 34 35 private: 36 bool LocalVisit(int32_t key); 37 const std::size_t capacity_; 38 std::list<int32_t> cacheList_; 39 std::unordered_map<int32_t, std::list<int32_t>::iterator> cacheMap_; 40 mutable std::mutex lruCacheMutex_; 41 }; 42 } // namespace OHOS::Rosen 43 #endif // OHOS_ROSEN_LRUCACHE_H