• 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 FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_FILE_CACHE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_FILE_CACHE_H
18 #include <list>
19 #include <mutex>
20 #include <shared_mutex>
21 #include <unordered_map>
22 #include <utility>
23 #include <vector>
24 
25 #include "base/utils/singleton.h"
26 #include "core/components_ng/image_provider/image_data.h"
27 
28 namespace OHOS::Ace {
29 const std::string ASTC_SUFFIX = ".astc";
30 const std::string SLASH = "/";
31 const std::string BACKSLASH = "\\";
32 struct FileInfo {
FileInfoFileInfo33     FileInfo(std::string name, size_t size, time_t time, size_t cnt)
34         : fileName(std::move(name)), fileSize(size), accessTime(time), accessCount(cnt) {}
35 
36     // file information will be sort by access time.
37     bool operator<(const FileInfo& otherFile) const
38     {
39         return accessTime > otherFile.accessTime;
40     }
41     std::string fileName;
42     size_t fileSize;
43     time_t accessTime;
44     size_t accessCount;
45 };
46 
47 class ImageFileCache : public Singleton<ImageFileCache> {
48     DECLARE_SINGLETON(ImageFileCache);
49     ACE_DISALLOW_MOVE(ImageFileCache);
50 public:
51     void SetImageCacheFilePath(const std::string& cacheFilePath);
52     std::string GetImageCacheFilePath();
53     std::string GetImageCacheFilePath(const std::string& url);
54     std::string GetImageCacheKey(const std::string& fileName);
55 
56     void SetCacheFileLimit(size_t cacheFileLimit);
57     void SetClearCacheFileRatio(float clearRatio);
58 
59     std::string GetCacheFilePath(const std::string& url);
60 
61     RefPtr<NG::ImageData> GetDataFromCacheFile(const std::string& url, const std::string& suffix);
62 
63     void SetCacheFileInfo();
64     void WriteCacheFile(
65         const std::string& url, const void* data, size_t size, const std::string& suffix = std::string());
66     void ClearCacheFile(const std::vector<std::string>& removeFiles);
67     std::string ConstructCacheFilePath(const std::string& fileName);
68     void DumpCacheInfo();
69 private:
70     void SaveCacheInner(const std::string& cacheKey, const std::string& suffix, size_t cacheSize,
71         std::vector<std::string>& removeVector);
72     std::string GetCacheFilePathInner(const std::string& url, const std::string& suffix);
73 
74     std::shared_mutex cacheFilePathMutex_;
75     std::string cacheFilePath_;
76 
77     std::atomic<size_t> fileLimit_ = 100 * 1024 * 1024; // the capacity is 100MB
78 
79     std::atomic<float> clearCacheFileRatio_ = 0.5f; // default clear ratio is 0.5
80 
81     std::mutex cacheFileSizeMutex_;
82     int64_t cacheFileSize_ = 0;
83 
84     std::mutex cacheFileInfoMutex_;
85     // lru
86     std::list<FileInfo> cacheFileInfo_;
87     std::unordered_map<std::string, std::list<FileInfo>::iterator> fileNameToFileInfoPos_;
88     const size_t convertAstcThreshold_ = 5;
89 
90     bool hasSetCacheFileInfo_ = false;
91 };
92 } // namespace OHOS::Ace
93 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_FILE_CACHE_H