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);
}
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));
}
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()));
}
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));
}
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();
}
Aggregations