InterceptorResponse resolveFromCache(InterceptorRequest request) throws ApolloException { ResponseNormalizer<Record> responseNormalizer = apolloStore.cacheResponseNormalizer(); //noinspection unchecked ApolloStoreOperation<Response> apolloStoreOperation = apolloStore.read(request.operation, responseFieldMapper, responseNormalizer, request.cacheHeaders); Response cachedResponse = apolloStoreOperation.execute(); if (cachedResponse.data() != null) { logger.d("Cache HIT for operation %s", request.operation); return new InterceptorResponse(null, cachedResponse, responseNormalizer.records()); } logger.d("Cache MISS for operation %s", request.operation); throw new ApolloException(String.format("Cache miss for operation %s", request.operation)); }
/** * Clear all entries from the normalized cache. This is synchronous operation and will be executed int the current * thread * * @return {@code true} if operation succeed, {@code false} otherwise */ public boolean clearNormalizedCache() { return apolloStore.clearAll().execute(); }
private ApolloInterceptorChain prepareInterceptorChain(Operation operation) { List<ApolloInterceptor> interceptors = new ArrayList<>(); HttpCachePolicy.Policy httpCachePolicy = operation instanceof Query ? this.httpCachePolicy : null; ResponseFieldMapper responseFieldMapper = responseFieldMapperFactory.create(operation); interceptors.addAll(applicationInterceptors); interceptors.add(responseFetcher.provideInterceptor(logger)); interceptors.add(new ApolloCacheInterceptor(apolloStore, responseFieldMapper, dispatcher, logger)); if (operation instanceof Query && enableAutoPersistedQueries) { interceptors.add(new ApolloAutoPersistedQueryInterceptor(logger)); } interceptors.add(new ApolloParseInterceptor(httpCache, apolloStore.networkResponseNormalizer(), responseFieldMapper, scalarTypeAdapters, logger)); interceptors.add(new ApolloServerInterceptor(serverUrl, httpCallFactory, httpCachePolicy, false, scalarTypeAdapters, logger)); return new RealApolloInterceptorChain(interceptors); }
@Override public synchronized void cancel() { switch (state.get()) { case ACTIVE: try { activeCall.cancel(); apolloStore.unsubscribe(recordChangeSubscriber); } finally { tracker.unregisterQueryWatcher(this); originalCallback.set(null); state.set(CANCELED); } break; case IDLE: state.set(CANCELED); break; case CANCELED: case TERMINATED: // These are not illegal states, but cancelling does nothing break; default: throw new IllegalStateException("Unknown state"); } }
@Override public void run() { try { apolloStore.publish(cacheKeys); } catch (Exception e) { logger.e(e, "Failed to publish cache changes"); } } });
Set<String> cacheResponse(final InterceptorResponse networkResponse, final InterceptorRequest request) { final Optional<List<Record>> records = networkResponse.cacheRecords.map( new Function<Collection<Record>, List<Record>>() { @NotNull @Override public List<Record> apply(@NotNull Collection<Record> records) { final List<Record> result = new ArrayList<>(records.size()); for (Record record : records) { result.add(record.toBuilder().mutationId(request.uniqueId).build()); } return result; } } ); if (!records.isPresent()) { return Collections.emptySet(); } try { return apolloStore.writeTransaction(new Transaction<WriteableStore, Set<String>>() { @Nullable @Override public Set<String> execute(WriteableStore cache) { return cache.merge(records.get(), request.cacheHeaders); } }); } catch (Exception e) { logger.e("Failed to cache operation response", e); return Collections.emptySet(); } }
public <D extends Operation.Data, T, V extends Operation.Variables> GraphQLStoreOperation<T> read( @Nonnull final Operation<D, T, V> operation) { return mStore.read(operation); }
public ResponseNormalizer<Record> cacheResponseNormalizer() { return mStore.cacheResponseNormalizer(); }
@Override public void run() { try { apolloStore.rollbackOptimisticUpdatesAndPublish(request.uniqueId).execute(); } catch (Exception e) { logger.e(e, "failed to rollback operation optimistic updates, for: %s", request.operation); } } });
@Override public void run() { try { if (request.optimisticUpdates.isPresent()) { Operation.Data optimisticUpdates = request.optimisticUpdates.get(); apolloStore.writeOptimisticUpdatesAndPublish(request.operation, optimisticUpdates, request.uniqueId) .execute(); } } catch (Exception e) { logger.e(e, "failed to write operation optimistic updates, for: %s", request.operation); } } });
Set<String> rollbackOptimisticUpdates(final InterceptorRequest request) { try { return apolloStore.rollbackOptimisticUpdates(request.uniqueId).execute(); } catch (Exception e) { logger.e(e, "failed to rollback operation optimistic updates, for: %s", request.operation); return Collections.emptySet(); } }
@Override public synchronized void refetch() { switch (state.get()) { case ACTIVE: apolloStore.unsubscribe(recordChangeSubscriber); activeCall.cancel(); activeCall = activeCall.clone().responseFetcher(refetchResponseFetcher); activeCall.enqueue(callbackProxy()); break; case IDLE: throw new IllegalStateException("Cannot refetch a watcher which has not first called enqueueAndWatch."); case CANCELED: throw new IllegalStateException("Cannot refetch a canceled watcher,"); case TERMINATED: throw new IllegalStateException("Cannot refetch a watcher which has experienced an error."); default: throw new IllegalStateException("Unknown state"); } }
public void publish(@Nonnull final Set<String> changedKeys) { mStore.publish(changedKeys); }
public <R> R writeTransaction(Transaction<WriteableStore, R> transaction) { return mStore.writeTransaction(transaction); }
public <F extends GraphqlFragment> GraphQLStoreOperation<F> read( @Nonnull final ResponseFieldMapper<F> responseFieldMapper, @Nonnull final CacheKey cacheKey, @Nonnull final Operation.Variables variables) { return mStore.read(responseFieldMapper, cacheKey, variables); }
@Override public void run() { try { mApolloStore.rollbackOptimisticUpdatesAndPublish(request.uniqueId).execute(); } catch (Exception e) { logger.e(e, "failed to rollback operation optimistic updates, for: %s", request.operation); } } });
@Override public void run() { try { if (request.optimisticUpdates.isPresent()) { Operation.Data optimisticUpdates = request.optimisticUpdates.get(); mApolloStore.writeOptimisticUpdatesAndPublish(request.operation, optimisticUpdates, request.uniqueId) .execute(); } } catch (Exception e) { logger.e(e, "failed to write operation optimistic updates, for: %s", request.operation); } } });
private Set<String> rollbackOptimisticUpdates(final InterceptorRequest request) { try { return mApolloStore.rollbackOptimisticUpdates(request.uniqueId).execute(); } catch (Exception e) { logger.e(e, "failed to rollback operation optimistic updates, for: %s", request.operation); return Collections.emptySet(); } }
private InterceptorResponse resolveFromCache(InterceptorRequest request) throws ApolloException { ResponseNormalizer<Record> responseNormalizer = mApolloStore.cacheResponseNormalizer(); //noinspection unchecked GraphQLStoreOperation<Response> graphQLStoreOperation = mApolloStore.read(request.operation, responseFieldMapper, responseNormalizer, request.cacheHeaders); Response cachedResponse = graphQLStoreOperation.execute(); if (cachedResponse.data() != null) { logger.d("Cache HIT for operation %s", request.operation); return new InterceptorResponse(null, cachedResponse, responseNormalizer.records()); } logger.d("Cache MISS for operation %s", request.operation); throw new ApolloException(String.format("Cache miss for operation %s", request.operation)); }
public synchronized void unsubscribe(ApolloStore.RecordChangeSubscriber subscriber) { mStore.unsubscribe(subscriber); }