Ad Caching
GFP SDK supports disk caching for ads starting from version 8.12.0.
This significantly reduces ad loading time,
especially improving user experience when applied to splash ads that run in cold start environments.
The disk caching targets include not only simple ad responses but also resources such as images and videos used in ads to minimize loading delays.
- In internal testing, the average reduction in ad loading time is approximately 71.4%.
- As of version
8.12.0, the disk caching feature can only be applied to S2S Native ads.
Implementation
Disk caching can be applied in two main ways as shown below, and you can choose one method to apply.
- Adding options to GfpAdLoader
- Applying custom implementation through GfpAdCacheManager
For most cases including splash ads, method 1 is sufficient and can be applied more simply than method 2.
1. Adding Options to GfpAdLoader
Add the withDiskCache(true) option when building the GfpAdLoader object used for ad loading.
- Kotlin
- Java
val adLoader = GfpAdLoader.Builder(context, adParam)
// . . .
.withDiskCache(true)
.build()
adLoader.loadAd()
GfpAdLoader adLoader = new GfpAdLoader.Builder(context, adParam)
// . . .
.withDiskCache(true)
.build();
adLoader.loadAd();
Detailed Behavior
- When
loadAd()is executed, it loads the disk-cached ad and simultaneously automatically requests and disk-caches the next ad for the same ad unit ID - If there is no disk-cached ad, it loads the ad in the same way as a normal GfpAdLoader (same as automatically disk-caching the next ad)
- The process of automatically loading the next ad is performed on a Worker Thread
On the first attempt after applying this option, there is no cached ad, so a normal ad request and load is performed. At this time, the next ad is automatically cached, so you can load the disk-cached ad from the second attempt.
2. Using GfpAdCacheManager
You can use GfpAdCacheManager to manually cache or load ads and receive callbacks for them.
GfpAdCacheManager.prefetch
Ad disk caching function, used when you need to additionally cache multiple ad responses besides the automatically cached next ad.
| Parameter | Description |
|---|---|
| context | context |
| adUnitId | Ad unit ID to cache |
| type | Ad type to cache |
| callback | Disk caching callback triggered on success and failure |
- Kotlin
- Java
// Ad disk caching
GfpAdCacheManager.prefetch(context, "AOS_NATIVE_EXAMPLE", GfpAdCacheType.Native, object : GfpAdCacheManager.PrefetchCallback {
override fun onSuccess() {
// Ad caching success
}
override fun onFailure(error: GfpAdCacheError) {
// Ad caching failure
}
)
GfpAdCacheManager.prefetch(context, "AOS_NATIVE_EXAMPLE", GfpAdCacheType.Native, new GfpAdCacheManager.PrefetchCallback() {
@Override
void onSuccess() {
// Ad caching success
}
@Overrideq
void onFailure(GfpAdCacheError error) {
// Ad caching failure
}
)
This function includes File I/O operations internally, so it is recommended to execute it on a Worker Thread.
GfpAdCacheManager.load
Disk-cached ad loading function that allows automatic caching of the next ad simultaneously with loading through parameters.
| Parameter | Description |
|---|---|
| context | context |
| adUnitId | Ad unit ID to load |
| type | Ad type to load |
| callback | Load callback for cached ads that triggers the ad response object (AdCallResponse) on success |
| autoPrefetchNext | Whether to automatically cache 1 new ad for the ad unit ID |
| prefetchCallback | Callback for new ad caching when autoPrefetchNext = true |
- Kotlin
- Java
// Load disk-cached ad
GfpAdCacheManager.load(context, "AOS_NATIVE_EXAMPLE", GfpAdCacheType.Native, object : GfpAdCacheManager.LoadCallback {
override fun onSuccess(adCallResponse: AdCallResponse) {
// Cached ad load success
// Can be loaded directly through adLoader
adLoader.loadAd(adCallResponse)
}
override fun onFailure(error: GfpAdCacheError) {
// Cached ad load failure
// Attempt to load using the normal method through adLoader
adLoader.loadAd()
}
)
// Load disk-cached ad
GfpAdCacheManager.load(context, "AOS_NATIVE_EXAMPLE", GfpAdCacheType.Native, new GfpAdCacheManager.LoadCallback() {
@Override
void onSuccess(AdCallResponse adCallResponse) {
// Cached ad load success
// Can be loaded directly through adLoader
adLoader.loadAd(adCallResponse);
}
@Override
void onFailure(GfpAdCacheError error) {
// Cached ad load failure
// Attempt to load using the normal method through adLoader
adLoader.loadAd();
}
)
The ad caching task performed through autoPrefetchNext is executed on a Worker Thread. Therefore, prefetchCallback is also triggered on the Worker Thread, so UI operations in that callback function may not work properly
GfpAdCacheManager.clear
Delete ad response cache files corresponding to a specific ad unit ID
| Parameter | Description |
|---|---|
| context | context |
| adUnitId | Ad unit ID to delete |
- Kotlin
- Java
GfpAdCacheManager.clear(context, "AOS_NATIVE_EXAMPLE")
GfpAdCacheManager.clear(context, "AOS_NATIVE_EXAMPLE");
GfpAdCacheManager.clearAll
Delete all ad response cache files
| Parameter | Description |
|---|---|
| context | context |
- Kotlin
- Java
GfpAdCacheManager.clearAll(context)
GfpAdCacheManager.clearAll(context);
GfpAdCacheError
Caching-related error class
| Type | Description |
|---|---|
| NotFound | No cached ad |
| Invalid | Ad that cannot be cached (not an S2S ad or no-fill ad response occurred) |
| FileIO | File I/O related error occurred during disk operation |
| LimitExceeded | Maximum number of ads that can be cached per adUnitId exceeded (5) |
FAQ
- Where are cache files stored?
- Cached ad responses and resources are stored in the cache directory (cacheDir).
- What happens when loading if there are multiple cached ads?
- When loading cached ads, ads that were cached longer ago are used first.
- What are the criteria for cacheable ads?
- Caching is only possible for ad unit IDs that only have S2S ads integrated, and empty (no-fill) ad responses are not cached.
- Is there a limit to the number of ads that can be cached?
- Up to 5 ads can be cached on the device per ad unit ID, and caching attempts exceeding this limit will fail.
- How long are cached files retained?
- Loaded ads are immediately deleted, and ad response and resource cache files older than 7 days are not used and are deleted.
- What happens when the service app version changes?
- When the service app version changes, all files cached in the previous version are not used and are deleted.