Search in sources :

Example 16 with Record

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

the class SaveRecord method save.

void save(final String providerKey, final String dynamicKey, final String dynamicKeyGroup, final Object data, final Long lifeTime, final boolean isExpirable, final boolean isEncrypted) {
    String composedKey = composeKey(providerKey, dynamicKey, dynamicKeyGroup);
    Record record = new Record(data, isExpirable, lifeTime);
    memory.put(composedKey, record);
    if (persistence.storedMB() >= maxMgPersistenceCache) {
        System.out.println(Locale.RECORD_CAN_NOT_BE_PERSISTED_BECAUSE_WOULD_EXCEED_THRESHOLD_LIMIT);
    } else {
        persistence.saveRecord(composedKey, record, isEncrypted, encryptKey);
    }
    evictExpirableRecordsPersistence.startTaskIfNeeded(isEncrypted);
}
Also used : Record(io.rx_cache2.internal.Record)

Example 17 with Record

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

the class EvictExpirableRecordsPersistenceTest method populate.

//7 mb
private void populate(boolean expirable) {
    for (int i = 0; i < mocksCount(); i++) {
        List<Mock> mocks = new ArrayList(mocksCount());
        for (int z = 0; z < mocksCount(); z++) {
            Mock mock = new Mock("Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC," + "making it over 2000 years old.Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, " + "making it over 2000 years old. Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, " + "making it over 2000 years old. Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC.");
            mocks.add(mock);
        }
        Record<List<Mock>> record = new Record<>(mocks, expirable, 1l);
        disk.saveRecord(String.valueOf(i), record, false, null);
    }
}
Also used : ArrayList(java.util.ArrayList) Record(io.rx_cache2.internal.Record) ArrayList(java.util.ArrayList) List(java.util.List) DataPoint(org.junit.experimental.theories.DataPoint) Mock(io.rx_cache2.internal.Mock)

Example 18 with Record

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

the class TwoLayersCacheTest method When_Save_And_Evict_Get_Null.

@Test
public void When_Save_And_Evict_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);
    twoLayersCacheUT.evictProviderKey(PROVIDER_KEY);
    Record<Mock> record = twoLayersCacheUT.retrieve(PROVIDER_KEY, "", "", false, ONE_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 19 with Record

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

the class TwoLayersCacheTest method When_Save_And_Not_Evict_Dynamic_Keys_Get_All.

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

Example 20 with Record

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

the class TwoLayersCacheTest method When_Save_Dynamic_Key_And_Evict_One_Dynamic_Key_Get_Others.

@Test
public void When_Save_Dynamic_Key_And_Evict_One_Dynamic_Key_Get_Others() {
    twoLayersCacheUT = new io.rx_cache2.internal.cache.TwoLayersCache(evictRecord(memory), retrieveRecord(memory), saveRecord(memory));
    twoLayersCacheUT.save(PROVIDER_KEY, "filer_1", "", new Mock(MOCK_VALUE), DUMMY_LIFE_TIME, true, false);
    twoLayersCacheUT.save(PROVIDER_KEY, "filer_2", "", new Mock(MOCK_VALUE + 1), DUMMY_LIFE_TIME, true, false);
    twoLayersCacheUT.save(PROVIDER_KEY, "filer_3", "", new Mock(MOCK_VALUE + 2), DUMMY_LIFE_TIME, true, false);
    twoLayersCacheUT.evictDynamicKey(PROVIDER_KEY, "filer_1");
    assertThat(twoLayersCacheUT.retrieve(PROVIDER_KEY, "filer_1", "", false, ONE_SECOND_LIFE, false), is(nullValue()));
    Record<Mock> record1 = twoLayersCacheUT.retrieve(PROVIDER_KEY, "filer_2", "", false, ONE_SECOND_LIFE, false);
    assertThat(record1.getData().getMessage(), is(MOCK_VALUE + 1));
    Record<Mock> record2 = twoLayersCacheUT.retrieve(PROVIDER_KEY, "filer_3", "", false, ONE_SECOND_LIFE, false);
    assertThat(record2.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)

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