Search in sources :

Example 11 with Record

use of com.apollographql.apollo.cache.normalized.Record in project apollo-android by apollographql.

the class LruNormalizedCacheTest method testDualCacheMultipleRecord.

@Test
public void testDualCacheMultipleRecord() {
    LruNormalizedCacheFactory secondaryCacheFactory = new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION);
    NormalizedCache primaryCache = new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION).chain(secondaryCacheFactory).createChain(basicFieldAdapter);
    Record.Builder recordBuilder = Record.builder("root1");
    recordBuilder.addField("bar", "bar");
    final Record record1 = recordBuilder.build();
    recordBuilder = Record.builder("root2");
    recordBuilder.addField("bar", "bar");
    final Record record2 = recordBuilder.build();
    recordBuilder = Record.builder("root3");
    recordBuilder.addField("bar", "bar");
    final Record record3 = recordBuilder.build();
    Collection<Record> records = Arrays.asList(record1, record2, record3);
    Collection<String> keys = Arrays.asList(record1.key(), record2.key(), record3.key());
    primaryCache.merge(records, CacheHeaders.NONE);
    assertThat(primaryCache.loadRecords(keys, CacheHeaders.NONE).size()).isEqualTo(3);
    // verify write through behavior
    assertThat(primaryCache.loadRecords(keys, CacheHeaders.NONE).size()).isEqualTo(3);
    assertThat(primaryCache.nextCache().get().loadRecords(keys, CacheHeaders.NONE).size()).isEqualTo(3);
}
Also used : NormalizedCache(com.apollographql.apollo.cache.normalized.NormalizedCache) Record(com.apollographql.apollo.cache.normalized.Record) Test(org.junit.Test)

Example 12 with Record

use of com.apollographql.apollo.cache.normalized.Record in project apollo-android by apollographql.

the class LruNormalizedCacheTest method testHeader_noCache.

@Test
public void testHeader_noCache() {
    LruNormalizedCache lruCache = new LruNormalizedCacheFactory(EvictionPolicy.builder().maxSizeBytes(10 * 1024).build()).create(basicFieldAdapter);
    Record testRecord = createTestRecord("1");
    lruCache.merge(testRecord, CacheHeaders.builder().addHeader(ApolloCacheHeaders.DO_NOT_STORE, "true").build());
    final Record record = lruCache.loadRecord("key1", CacheHeaders.NONE);
    assertThat(record).isNull();
    Record testRecord1 = createTestRecord("1");
    Record testRecord2 = createTestRecord("2");
    Collection<Record> testRecordSet = new HashSet<>();
    testRecordSet.add(testRecord1);
    testRecordSet.add(testRecord2);
    lruCache.merge(testRecordSet, CacheHeaders.builder().addHeader(ApolloCacheHeaders.DO_NOT_STORE, "true").build());
    final Record record1 = lruCache.loadRecord("key1", CacheHeaders.NONE);
    final Record record2 = lruCache.loadRecord("key2", CacheHeaders.NONE);
    assertThat(record1).isNull();
    assertThat(record2).isNull();
}
Also used : Record(com.apollographql.apollo.cache.normalized.Record) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with Record

use of com.apollographql.apollo.cache.normalized.Record in project apollo-android by apollographql.

the class LruNormalizedCacheTest method testClearPrimaryCache.

@Test
public void testClearPrimaryCache() {
    LruNormalizedCacheFactory secondaryCacheFactory = new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION);
    LruNormalizedCache primaryCache = (LruNormalizedCache) new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION).chain(secondaryCacheFactory).createChain(basicFieldAdapter);
    Record record = Record.builder("key").build();
    primaryCache.merge(record, CacheHeaders.NONE);
    primaryCache.clearCurrentCache();
    assertThat(primaryCache.nextCache().get().loadRecord("key", CacheHeaders.NONE)).isNotNull();
    assertThat(primaryCache.nextCache().get().loadRecord("key", CacheHeaders.NONE)).isNotNull();
}
Also used : Record(com.apollographql.apollo.cache.normalized.Record) Test(org.junit.Test)

Example 14 with Record

use of com.apollographql.apollo.cache.normalized.Record in project apollo-android by apollographql.

the class LruNormalizedCacheTest method testLoad_recordNotPresent.

@Test
public void testLoad_recordNotPresent() {
    LruNormalizedCache lruCache = new LruNormalizedCacheFactory(EvictionPolicy.builder().maxSizeBytes(10 * 1024).build()).create(basicFieldAdapter);
    final Record record = lruCache.loadRecord("key1", CacheHeaders.NONE);
    assertThat(record).isNull();
}
Also used : Record(com.apollographql.apollo.cache.normalized.Record) Test(org.junit.Test)

Example 15 with Record

use of com.apollographql.apollo.cache.normalized.Record in project apollo-android by apollographql.

the class LruNormalizedCacheTest method testSaveAndLoad_multipleRecord_readMultiple.

@Test
public void testSaveAndLoad_multipleRecord_readMultiple() {
    LruNormalizedCache lruCache = new LruNormalizedCacheFactory(EvictionPolicy.builder().maxSizeBytes(10 * 1024).build()).create(basicFieldAdapter);
    Record testRecord1 = createTestRecord("1");
    Record testRecord2 = createTestRecord("2");
    Record testRecord3 = createTestRecord("3");
    List<Record> inputRecords = Arrays.asList(testRecord1, testRecord2, testRecord3);
    lruCache.merge(inputRecords, CacheHeaders.NONE);
    final Collection<Record> readRecords = lruCache.loadRecords(Arrays.asList("key1", "key2", "key3"), CacheHeaders.NONE);
    // noinspection ResultOfMethodCallIgnored
    assertThat(readRecords).containsExactlyElementsIn(inputRecords);
}
Also used : Record(com.apollographql.apollo.cache.normalized.Record) Test(org.junit.Test)

Aggregations

Record (com.apollographql.apollo.cache.normalized.Record)42 Test (org.junit.Test)35 CacheReference (com.apollographql.apollo.cache.normalized.CacheReference)15 NormalizedCache (com.apollographql.apollo.cache.normalized.NormalizedCache)7 Nonnull (javax.annotation.Nonnull)5 CacheKey (com.apollographql.apollo.cache.normalized.CacheKey)4 CacheHeaders (com.apollographql.apollo.cache.CacheHeaders)3 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)3 HeroAndFriendsNamesQuery (com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesQuery)3 HeroTypeDependentAliasedFieldQuery (com.apollographql.apollo.integration.normalizer.HeroTypeDependentAliasedFieldQuery)3 NonNull (android.support.annotation.NonNull)2 ApolloCacheHeaders (com.apollographql.apollo.cache.ApolloCacheHeaders)2 NormalizedCacheFactory (com.apollographql.apollo.cache.normalized.NormalizedCacheFactory)2 RecordFieldJsonAdapter (com.apollographql.apollo.cache.normalized.RecordFieldJsonAdapter)2 ApolloException (com.apollographql.apollo.exception.ApolloException)2 HeroParentTypeDependentFieldQuery (com.apollographql.apollo.integration.normalizer.HeroParentTypeDependentFieldQuery)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Set (java.util.Set)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2