public class

OfflineResourceCache

extends Object
java.lang.Object
   ↳ com.salesforce.android.knowledge.core.offline.OfflineResourceCache

Class Overview

This class can be used to cache online resources for later offline viewing.

You can retrieve an instance of this class from getCache(). Resources will be written into the cache automatically if a non-disabled OfflineResourceConfig object is passed to offlineResourceConfig(OfflineResourceConfig) at configuration time. It is not usually necessary to call putResource(String, Source) manually, but you may wish to use getResource(String) to retrieved a cached resource when rendering article content offline.

The cache is backed by a disk-based LRU cache which has a maximum size as specified in maxSize(int). Older article resources are evicted to make room as necessary.

Summary

Nested Classes
class OfflineResourceCache.Builder Builder class for OfflineResourceCache
Constants
int CACHE_KEY_HASH_SEED
String CACHE_SUB_DIRECTORY
Fields
protected static final ServiceLogger log
Protected Constructors
OfflineResourceCache(OfflineResourceCache.Builder builder)
Public Methods
Async<Void> clear()
Empty the cache.
static void clearCache(Context context, AuthenticatedUser authenticatedUser)
Clear the cache for the specified user.
boolean delete()
Delete the cache from disk by removing all associated files.
static void deleteCache(Context context, AuthenticatedUser authenticatedUser)
Delete the cache for the specified user from disk.
static void deleteCache(Context context)
Delete offline resource cache for all users.
InputStream getResource(String url)
Fetches a cached resource from the filesystem, if present.
boolean putResource(String url, Source source)
Puts the supplied resource into the cache.
long size()
Return the current approximate size of the cache on disk.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

protected static final int CACHE_KEY_HASH_SEED

Constant Value: 129492964 (0x07b7e7e4)

protected static final String CACHE_SUB_DIRECTORY

Constant Value: "salesforce_kb"

Fields

protected static final ServiceLogger log

Protected Constructors

protected OfflineResourceCache (OfflineResourceCache.Builder builder)

Public Methods

public Async<Void> clear ()

Empty the cache. This deletes any resources stored on disk.

Returns
  • A completable async that will be called when the cache has been cleared.

public static void clearCache (Context context, AuthenticatedUser authenticatedUser)

Clear the cache for the specified user. Clearing is performed on a background thread.

Parameters
context A Context, required for accessing the cache directory
authenticatedUser The the user whose cache will be cleared

public boolean delete ()

Delete the cache from disk by removing all associated files.

Returns
  • True if successful

public static void deleteCache (Context context, AuthenticatedUser authenticatedUser)

Delete the cache for the specified user from disk.

Parameters
context A Context, required for accessing the cache directory
authenticatedUser The the user whose cache will be deleted

public static void deleteCache (Context context)

Delete offline resource cache for all users.

Parameters
context A Context, required for accessing the cache directory

public InputStream getResource (String url)

Fetches a cached resource from the filesystem, if present. Note that this method is synchronous and should not be accessed from the main thread.

Parameters
url The online URL of the resource.
Returns
  • An InputStream containing the resource bytes if the resource has been previously cached, null otherwise.

public boolean putResource (String url, Source source)

Puts the supplied resource into the cache. Note that this is synchronous and should not be accessed from the main thread.

Parameters
url The url from which the resource was downloaded.
source The source bytes for the resource.
Returns
  • True if the resource was saved in the cache, false if it could not be saved.

public long size ()

Return the current approximate size of the cache on disk.

Returns
  • The approximate size of the cache, in bytes.