Search in sources :

Example 11 with Record

use of io.rx_cache2.internal.Record in project RxCache by VictorAlbertos.

the class TwoLayersCacheTest method When_Save_And_Dynamic_Key_Group_Record_Has_Expired_Only_Get_Null_For_Dynamic_Key.

@Test
public void When_Save_And_Dynamic_Key_Group_Record_Has_Expired_Only_Get_Null_For_Dynamic_Key() {
    twoLayersCacheUT = new io.rx_cache2.internal.cache.TwoLayersCache(evictRecord(memory), retrieveRecord(memory), saveRecord(memory));
    twoLayersCacheUT.save(PROVIDER_KEY, "1", "1", new Mock(MOCK_VALUE), ONE_SECOND_LIFE, true, false);
    twoLayersCacheUT.save(PROVIDER_KEY, "1", "2", new Mock(MOCK_VALUE), DUMMY_LIFE_TIME, true, false);
    twoLayersCacheUT.save(PROVIDER_KEY, "2", "1", new Mock(MOCK_VALUE), DUMMY_LIFE_TIME, true, false);
    twoLayersCacheUT.save(PROVIDER_KEY, "2", "2", new Mock(MOCK_VALUE), DUMMY_LIFE_TIME, true, false);
    waitTime(MORE_THAN_ONE_SECOND_LIFE);
    Record<Mock> record = twoLayersCacheUT.retrieve(PROVIDER_KEY, "1", "1", false, ONE_SECOND_LIFE, false);
    assertThat(record, is(nullValue()));
    record = twoLayersCacheUT.retrieve(PROVIDER_KEY, "1", "1", false, THREE_SECOND_LIFE, false);
    assertThat(record, is(nullValue()));
    record = twoLayersCacheUT.retrieve(PROVIDER_KEY, "1", "2", false, THREE_SECOND_LIFE, false);
    assertNotNull(record);
    record = twoLayersCacheUT.retrieve(PROVIDER_KEY, "2", "1", false, THREE_SECOND_LIFE, false);
    assertNotNull(record);
    record = twoLayersCacheUT.retrieve(PROVIDER_KEY, "2", "2", false, THREE_SECOND_LIFE, false);
    assertNotNull(record);
}
Also used : Mock(io.rx_cache2.internal.Mock) BaseTest(io.rx_cache2.internal.common.BaseTest) Test(org.junit.Test)

Example 12 with Record

use of io.rx_cache2.internal.Record in project RxCache by VictorAlbertos.

the class TwoLayersCacheTest method When_Save_Dynamic_Key_And_Re_Save_Dynamic_Key_Get_Last_Value.

@Test
public void When_Save_Dynamic_Key_And_Re_Save_Dynamic_Key_Get_Last_Value() {
    twoLayersCacheUT = new io.rx_cache2.internal.cache.TwoLayersCache(evictRecord(memory), retrieveRecord(memory), saveRecord(memory));
    twoLayersCacheUT.save(PROVIDER_KEY, "1", "", new Mock(MOCK_VALUE + 1), DUMMY_LIFE_TIME, true, false);
    twoLayersCacheUT.save(PROVIDER_KEY, "1", "", new Mock(MOCK_VALUE + 2), DUMMY_LIFE_TIME, true, false);
    Record<Mock> record = twoLayersCacheUT.retrieve(PROVIDER_KEY, "1", "", false, ONE_SECOND_LIFE, false);
    assertThat(record.getData().getMessage(), is(MOCK_VALUE + 2));
}
Also used : Mock(io.rx_cache2.internal.Mock) BaseTest(io.rx_cache2.internal.common.BaseTest) Test(org.junit.Test)

Example 13 with Record

use of io.rx_cache2.internal.Record in project RxCache by VictorAlbertos.

the class TwoLayersCacheTest method When_Save_And_Provider_Record_Has_Expired_Get_Null.

@Test
public void When_Save_And_Provider_Record_Has_Expired_Get_Null() {
    twoLayersCacheUT = new io.rx_cache2.internal.cache.TwoLayersCache(evictRecord(memory), retrieveRecord(memory), saveRecord(memory));
    twoLayersCacheUT.save(PROVIDER_KEY, "", "", new Mock(MOCK_VALUE), DUMMY_LIFE_TIME, true, false);
    waitTime(MORE_THAN_ONE_SECOND_LIFE);
    Record<Mock> record = twoLayersCacheUT.retrieve(PROVIDER_KEY, "", "", false, ONE_SECOND_LIFE, false);
    assertThat(record, is(nullValue()));
    record = twoLayersCacheUT.retrieve(PROVIDER_KEY, "", "", false, THREE_SECOND_LIFE, false);
    assertThat(record, is(nullValue()));
}
Also used : Mock(io.rx_cache2.internal.Mock) BaseTest(io.rx_cache2.internal.common.BaseTest) Test(org.junit.Test)

Example 14 with Record

use of io.rx_cache2.internal.Record in project RxCache by VictorAlbertos.

the class DeleteRecordMatchingClassNameTest method When_Class_Matches_Delete_Record_1_List.

@Test
public void When_Class_Matches_Delete_Record_1_List() {
    disk.saveRecord(Mock1.KEY, new Record(Arrays.asList(new Mock1()), true, 0l), false, null);
    disk.saveRecord(Mock2.KEY, new Record(Arrays.asList(new Mock2()), true, 0l), false, null);
    assertThat(disk.allKeys().size(), is(2));
    deleteRecordMatchingClassNameUT.with(Arrays.<Class>asList(Mock1.class)).react().test().awaitTerminalEvent();
    assertThat(disk.allKeys().size(), is(1));
}
Also used : Record(io.rx_cache2.internal.Record) BaseTest(io.rx_cache2.internal.common.BaseTest) Test(org.junit.Test)

Example 15 with Record

use of io.rx_cache2.internal.Record in project RxCache by VictorAlbertos.

the class EvictExpirableRecordsPersistence method oEvictingTask.

private Observable<String> oEvictingTask() {
    Observable<String> oEvictingTask = Observable.create(new ObservableOnSubscribe<String>() {

        @Override
        public void subscribe(ObservableEmitter<String> emitter) throws Exception {
            if (!couldBeExpirableRecords) {
                emitter.onNext(Locale.RECORD_CAN_NOT_BE_EVICTED_BECAUSE_NO_ONE_IS_EXPIRABLE);
                emitter.onComplete();
                return;
            }
            int storedMB = persistence.storedMB();
            if (!reachedPercentageMemoryToStart(storedMB)) {
                emitter.onComplete();
                return;
            }
            List<String> allKeys = persistence.allKeys();
            float releasedMBSoFar = 0f;
            for (String key : allKeys) {
                if (reachedPercentageMemoryToStop(storedMB, releasedMBSoFar)) {
                    break;
                }
                Record record = persistence.retrieveRecord(key, isEncrypted, encryptKey);
                if (record == null)
                    continue;
                if (!record.getExpirable())
                    continue;
                persistence.evict(key);
                emitter.onNext(key);
                releasedMBSoFar += record.getSizeOnMb();
            }
            couldBeExpirableRecords = reachedPercentageMemoryToStop(storedMB, releasedMBSoFar);
            emitter.onComplete();
        }
    }).subscribeOn((Schedulers.io())).observeOn(Schedulers.io()).doOnError(new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) throws Exception {
            throwable.printStackTrace();
        }
    });
    return oEvictingTask.share();
}
Also used : ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) Record(io.rx_cache2.internal.Record) ObservableEmitter(io.reactivex.ObservableEmitter)

Aggregations

BaseTest (io.rx_cache2.internal.common.BaseTest)20 Test (org.junit.Test)20 Mock (io.rx_cache2.internal.Mock)17 Record (io.rx_cache2.internal.Record)7 ObservableEmitter (io.reactivex.ObservableEmitter)1 ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)1 Function (io.reactivex.functions.Function)1 Reply (io.rx_cache2.Reply)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DataPoint (org.junit.experimental.theories.DataPoint)1