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 HIVIEW_BASE_EVENT_STORE_SYS_EVENT_DOC_LRU_CACHE_H 17 #define HIVIEW_BASE_EVENT_STORE_SYS_EVENT_DOC_LRU_CACHE_H 18 19 #include <list> 20 #include <memory> 21 22 #include <string> 23 #include <unordered_map> 24 25 #include "sys_event_doc.h" 26 27 namespace OHOS { 28 namespace HiviewDFX { 29 namespace EventStore { 30 class SysEventDocLruCache { 31 public: 32 typedef std::pair<std::string, std::string> LruCacheKey; 33 typedef std::shared_ptr<SysEventDoc> LruCacheValue; 34 SysEventDocLruCache(size_t capacity)35 explicit SysEventDocLruCache(size_t capacity) : capacity_(capacity) {} ~SysEventDocLruCache()36 ~SysEventDocLruCache() {} 37 38 bool Contain(const LruCacheKey& key) const; 39 LruCacheValue Get(const LruCacheKey& key); 40 bool Add(const LruCacheKey& key, const LruCacheValue& value); 41 bool Remove(const LruCacheKey& key); 42 void Clear(); 43 44 private: 45 typedef std::pair<std::list<LruCacheKey>::iterator, LruCacheValue> LruCacheValuePair; 46 struct LruCacheKeyHashfunc { operatorLruCacheKeyHashfunc47 size_t operator() (const std::pair<std::string, std::string>& keyPair) const 48 { 49 return std::hash<std::string>()(keyPair.first) ^ std::hash<std::string>()(keyPair.second); 50 } 51 }; 52 53 size_t capacity_; 54 std::list<LruCacheKey> lruList_; 55 std::unordered_map<LruCacheKey, LruCacheValuePair, LruCacheKeyHashfunc> lruCache_; 56 }; // SysEventDocLruCache 57 } // EventStore 58 } // HiviewDFX 59 } // OHOS 60 #endif // HIVIEW_BASE_EVENT_STORE_SYS_EVENT_DOC_LRU_CACHE_H 61