Skip to content

Commit f958df5

Browse files
Add SetCacheDir API to customize disk cache directory. (#3225)
1 parent 416e99c commit f958df5

File tree

11 files changed

+87
-3
lines changed

11 files changed

+87
-3
lines changed

android/libpag/src/main/java/org/libpag/PAGDiskCache.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@
3131
* Defines methods to manage the disk cache capabilities.
3232
*/
3333
public class PAGDiskCache {
34+
/**
35+
* Sets the disk cache directory. This should be called before any disk cache operations.
36+
* If the directory does not exist, it will be created automatically.
37+
* Note: Changing the cache directory after cache operations have started may cause
38+
* existing cached files to become inaccessible.
39+
* @param dir The absolute path of the cache directory. Pass null or empty string to use the
40+
* platform default cache directory.
41+
*/
42+
public static native void SetCacheDir(String dir);
43+
3444
/**
3545
* Returns the size limit of the disk cache in bytes. The default value is 1 GB.
3646
*/

include/pag/c/pag_disk_cache.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222

2323
PAG_C_PLUS_PLUS_BEGIN_GUARD
2424

25+
/**
26+
* Sets the disk cache directory. This should be called before any disk cache operations.
27+
* @param dir The absolute path of the cache directory. Pass NULL or empty string to use the
28+
* platform default cache directory.
29+
*/
30+
PAG_EXPORT void pag_disk_cache_set_cache_dir(const char* dir);
31+
2532
PAG_EXPORT size_t pag_disk_cache_get_max_disk_size();
2633

2734
PAG_EXPORT void pag_disk_cache_set_max_disk_size(size_t maxDiskSize);

include/pag/pag.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,16 @@ class PAG_API PAGDecoder {
16781678
*/
16791679
class PAG_API PAGDiskCache {
16801680
public:
1681+
/**
1682+
* Sets the disk cache directory. This should be called before any disk cache operations.
1683+
* If the directory does not exist, it will be created automatically.
1684+
* Note: Changing the cache directory after cache operations have started may cause
1685+
* existing cached files to become inaccessible.
1686+
* @param dir The absolute path of the cache directory. Pass an empty string to use the
1687+
* platform default cache directory.
1688+
*/
1689+
static void SetCacheDir(const std::string& dir);
1690+
16811691
/**
16821692
* Returns the size limit of the disk cache in bytes. The default value is 1 GB.
16831693
*/

src/c/pag_disk_cache.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include "pag/c/pag_disk_cache.h"
2020
#include "pag/pag.h"
2121

22+
void pag_disk_cache_set_cache_dir(const char* dir) {
23+
pag::PAGDiskCache::SetCacheDir(dir ? dir : "");
24+
}
25+
2226
size_t pag_disk_cache_get_max_disk_size() {
2327
return pag::PAGDiskCache::MaxDiskSize();
2428
}

src/platform/android/JPAGDiskCache.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ void JPAGDiskCache::InitJNI(JNIEnv* env) {
2929
LOGE("Could not run PAGDiskCache.InitJNI(), PAGClass is not found!");
3030
return;
3131
}
32-
PAG_GetCacheDir = env->GetStaticMethodID(PAGClass.get(), "GetCacheDir", "()Ljava/lang/String;");
32+
PAG_GetCacheDir =
33+
env->GetStaticMethodID(PAGClass.get(), "GetCacheDir", "()Ljava/lang/String;");
3334
}
3435

3536
std::string JPAGDiskCache::GetCacheDir() {
@@ -48,6 +49,11 @@ std::string JPAGDiskCache::GetCacheDir() {
4849
} // namespace pag
4950

5051
extern "C" {
52+
PAG_API void JNICALL Java_org_libpag_PAGDiskCache_SetCacheDir(JNIEnv* env, jclass, jstring dir) {
53+
auto cacheDir = pag::SafeConvertToStdString(env, dir);
54+
pag::PAGDiskCache::SetCacheDir(cacheDir);
55+
}
56+
5157
PAG_API jlong JNICALL Java_org_libpag_PAGDiskCache_MaxDiskSize(JNIEnv*, jclass) {
5258
return static_cast<jlong>(pag::PAGDiskCache::MaxDiskSize());
5359
}

src/platform/cocoa/PAGDiskCache.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@
2424
*/
2525
PAG_API @interface PAGDiskCache : NSObject
2626

27+
/**
28+
* Sets the disk cache directory. This should be called before any disk cache operations.
29+
* If the directory does not exist, it will be created automatically.
30+
* Note: Changing the cache directory after cache operations have started may cause
31+
* existing cached files to become inaccessible.
32+
* @param dir The absolute path of the cache directory. Pass nil or empty string to use the
33+
* platform default cache directory.
34+
*/
35+
+ (void)SetCacheDir:(NSString*)dir;
36+
2737
/**
2838
* Returns the size limit of the disk cache in bytes. The default value is 1 GB.
2939
*/

src/platform/cocoa/PAGDiskCache.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121

2222
@implementation PAGDiskCache
2323

24+
+ (void)SetCacheDir:(NSString*)dir {
25+
[PAGDiskCacheImpl SetCacheDir:dir];
26+
}
27+
2428
+ (size_t)MaxDiskSize {
2529
return [PAGDiskCacheImpl MaxDiskSize];
2630
}

src/platform/cocoa/private/PAGDiskCacheImpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
@interface PAGDiskCacheImpl : NSObject
2222

23+
+ (void)SetCacheDir:(NSString*)dir;
24+
2325
+ (size_t)MaxDiskSize;
2426

2527
+ (void)SetMaxDiskSize:(size_t)size;

src/platform/cocoa/private/PAGDiskCacheImpl.mm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222

2323
@implementation PAGDiskCacheImpl
2424

25+
+ (void)SetCacheDir:(NSString*)dir {
26+
std::string cacheDir = dir ? [dir UTF8String] : "";
27+
pag::PAGDiskCache::SetCacheDir(cacheDir);
28+
}
29+
2530
+ (size_t)MaxDiskSize {
2631
return pag::PAGDiskCache::MaxDiskSize();
2732
}

src/rendering/caches/DiskCache.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "tgfx/core/Stream.h"
2626

2727
namespace pag {
28+
2829
class FileInfo {
2930
public:
3031
FileInfo(std::string cacheKey, uint32_t fileID, size_t fileSize = 0)
@@ -37,6 +38,10 @@ class FileInfo {
3738
std::list<std::shared_ptr<FileInfo>>::iterator cachedPosition;
3839
};
3940

41+
void PAGDiskCache::SetCacheDir(const std::string& dir) {
42+
DiskCache::GetInstance()->setCacheDir(dir);
43+
}
44+
4045
size_t PAGDiskCache::MaxDiskSize() {
4146
return DiskCache::GetInstance()->getMaxDiskSize();
4247
}
@@ -49,6 +54,24 @@ void PAGDiskCache::RemoveAll() {
4954
DiskCache::GetInstance()->removeAll();
5055
}
5156

57+
std::string DiskCache::getCacheDir() {
58+
if (!customCacheDir.empty()) {
59+
return customCacheDir;
60+
}
61+
return Platform::Current()->getCacheDir();
62+
}
63+
64+
void DiskCache::setCacheDir(const std::string& dir) {
65+
std::lock_guard<std::mutex> autoLock(locker);
66+
customCacheDir = dir;
67+
// Update cache paths
68+
auto cacheDir = dir.empty() ? Platform::Current()->getCacheDir() : dir;
69+
if (!cacheDir.empty()) {
70+
configPath = Directory::JoinPath(cacheDir, "cache.cfg");
71+
cacheFolder = Directory::JoinPath(cacheDir, "files");
72+
}
73+
}
74+
5275
DiskCache* DiskCache::GetInstance() {
5376
static auto& diskCache = *new DiskCache();
5477
return &diskCache;
@@ -69,7 +92,7 @@ bool DiskCache::WriteFile(const std::string& key, std::shared_ptr<tgfx::Data> da
6992
}
7093

7194
DiskCache::DiskCache() {
72-
auto cacheDir = Platform::Current()->getCacheDir();
95+
auto cacheDir = getCacheDir();
7396
if (!cacheDir.empty()) {
7497
configPath = Directory::JoinPath(cacheDir, "cache.cfg");
7598
cacheFolder = Directory::JoinPath(cacheDir, "files");

0 commit comments

Comments
 (0)