Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Add ApolloClient.Builder(ApolloHttpCache) (#5638) #5640

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libraries/apollo-http-cache/api/apollo-http-cache.api
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public final class com/apollographql/apollo3/cache/http/DiskLruHttpCache$Compani
}

public final class com/apollographql/apollo3/cache/http/HttpCache {
public static final fun configureApolloClientBuilder (Lcom/apollographql/apollo3/ApolloClient$Builder;Lcom/apollographql/apollo3/cache/http/ApolloHttpCache;)Lcom/apollographql/apollo3/ApolloClient$Builder;
public static final fun configureApolloClientBuilder (Lcom/apollographql/apollo3/ApolloClient$Builder;Ljava/io/File;J)Lcom/apollographql/apollo3/ApolloClient$Builder;
public static final fun getHttpCache (Lcom/apollographql/apollo3/ApolloClient;)Lcom/apollographql/apollo3/cache/http/ApolloHttpCache;
public static final fun httpDoNotStore (Lcom/apollographql/apollo3/api/MutableExecutionOptions;Z)Ljava/lang/Object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ import java.io.File
import java.time.Instant
import java.time.format.DateTimeParseException

class CachingHttpInterceptor(
directory: File,
maxSize: Long,
fileSystem: FileSystem = FileSystem.SYSTEM,
class CachingHttpInterceptor internal constructor(
private val lruHttpCache: ApolloHttpCache
) : HttpInterceptor {
private val lruHttpCache = DiskLruHttpCache(fileSystem, directory, maxSize)

constructor(
directory: File,
maxSize: Long,
fileSystem: FileSystem = FileSystem.SYSTEM,
): this(DiskLruHttpCache(fileSystem, directory, maxSize))

val cache: ApolloHttpCache = lruHttpCache

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import okio.FileSystem
import java.io.File

enum class HttpFetchPolicy {
Expand Down Expand Up @@ -72,10 +73,15 @@ fun ApolloClient.Builder.httpCache(
directory: File,
maxSize: Long,
): ApolloClient.Builder {
val cachingHttpInterceptor = CachingHttpInterceptor(
directory = directory,
maxSize = maxSize,
)
return httpCache(DiskLruHttpCache(FileSystem.SYSTEM, directory, maxSize))
}

@JvmName("configureApolloClientBuilder")
fun ApolloClient.Builder.httpCache(
apolloHttpCache: ApolloHttpCache,
): ApolloClient.Builder {
val cachingHttpInterceptor = CachingHttpInterceptor(apolloHttpCache)

val apolloRequestToCacheKey = mutableMapOf<String, String>()
return addHttpInterceptor(object : HttpInterceptor {
override suspend fun intercept(request: HttpRequest, chain: HttpInterceptorChain): HttpResponse {
Expand Down
Loading