Skip to content

Instantly share code, notes, and snippets.

@archangel-irk
Last active January 20, 2019 13:55

Revisions

  1. archangel-irk revised this gist Jan 20, 2019. 1 changed file with 15 additions and 5 deletions.
    20 changes: 15 additions & 5 deletions localCache.js
    Original file line number Diff line number Diff line change
    @@ -59,7 +59,7 @@ const getBondsData = async ({date, isins}) => {
    let result = [];

    if (data.uncached.length) {
    const result = await http.post({
    result = await http.post({
    url: `/bonds/${date}`,
    body: data.uncached,
    });
    @@ -82,7 +82,17 @@ function assert(condition, message) {
    }
    }

    (function test() {
    async function test(name, func) {
    try {
    await func();
    console.log(name, ': passed')
    } catch (exception) {
    console.log(name, ': failed');
    console.error(exception);
    }
    }

    test('Put data into cache', () => {
    const cache = new BondsCache();
    const date = '20180120';
    const items = [{
    @@ -99,9 +109,9 @@ function assert(condition, message) {
    assert(cache.getItem(id1).isin === items[0].isin, 'First item is not match after `put`');
    assert(cache.getItem(id2).isin === items[1].isin, 'Second item is not match after `put`');
    assert(cache.size() === 2, '`size` is not correct after `put`');
    })();
    });

    (function test() {
    test('Check cached data with initial cache', () => {
    const cache = new BondsCache();
    const date = '20180120';
    const cachedIsin = 'XS0971721963';
    @@ -143,4 +153,4 @@ function assert(condition, message) {

    cache.clear();
    assert(cache.size() === 0, '`size` is not correct after clear');
    })();
    });
  2. archangel-irk revised this gist Jan 20, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion localCache.js
    Original file line number Diff line number Diff line change
    @@ -64,7 +64,7 @@ const getBondsData = async ({date, isins}) => {
    body: data.uncached,
    });

    cache.put(result);
    cache.put(date, result);
    }

    return [].concat(data.cached, result);
  3. archangel-irk revised this gist Jan 20, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion localCache.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // Complexity for getBondsData - 2n
    // Complexity for getBondsData - 3n
    // Memory - ?

    class BondsCache {
  4. archangel-irk created this gist Jan 20, 2019.
    146 changes: 146 additions & 0 deletions localCache.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,146 @@
    // Complexity for getBondsData - 2n
    // Memory - ?

    class BondsCache {
    constructor() {
    this.data = new Map();
    }

    getId(date, isin) {
    return `${date}_${isin}`;
    }

    getItem(id) {
    return this.data.get(id);
    }

    setItem(id, item) {
    this.data.set(id, item);
    }

    clear() {
    this.data.clear();
    }

    size(){
    return this.data.size;
    }

    isCached(id) {
    return this.data.has(id);
    }

    getCachedData(date, isins) {
    const result = {
    cached: [],
    uncached: [],
    };
    isins.forEach((isin) => {
    const id = this.getId(date, isin);
    if (this.isCached(id)) {
    result.cached.push(this.getItem(id));
    } else {
    result.uncached.push(isin);
    }
    });
    return result;
    }

    put(date, items) {
    items.forEach((item) => {
    this.setItem(this.getId(date, item.isin), item);
    });
    }
    }

    const cache = new BondsCache();
    const getBondsData = async ({date, isins}) => {
    const data = cache.getCachedData(date, isins);
    let result = [];

    if (data.uncached.length) {
    const result = await http.post({
    url: `/bonds/${date}`,
    body: data.uncached,
    });

    cache.put(result);
    }

    return [].concat(data.cached, result);
    };

    // getBondsData({
    // date: '20180120',
    // isins: ['XS0971721963', 'RU000A0JU4L3']
    // });


    function assert(condition, message) {
    if (!condition) {
    throw new Error(message || 'Assertion failed');
    }
    }

    (function test() {
    const cache = new BondsCache();
    const date = '20180120';
    const items = [{
    isin: 'XS0971721963',
    data: {}
    }, {
    isin: 'RU000A0JU4L3',
    data: {}
    }];
    cache.put(date, items);

    const id1 = cache.getId(date, items[0].isin);
    const id2 = cache.getId(date, items[1].isin);
    assert(cache.getItem(id1).isin === items[0].isin, 'First item is not match after `put`');
    assert(cache.getItem(id2).isin === items[1].isin, 'Second item is not match after `put`');
    assert(cache.size() === 2, '`size` is not correct after `put`');
    })();

    (function test() {
    const cache = new BondsCache();
    const date = '20180120';
    const cachedIsin = 'XS0971721963';
    const uncachedIsin = 'RU000A0JU4L3';
    const previousResult = [{
    isin: cachedIsin,
    data: {}
    }];
    const request = {
    date,
    isins: [cachedIsin, uncachedIsin]
    };

    // Set initial cache
    const prevId1 = cache.getId(date, cachedIsin);
    cache.put(date, previousResult);
    assert(cache.size() === 1, '`size` is not correct after `put`');
    assert(cache.getItem(prevId1).isin === cachedIsin, 'Cached item is not matched after `put`');

    // Check cached data
    let data = cache.getCachedData(request.date, request.isins);
    assert(data.cached.length === 1, 'cached length is not correct');
    assert(data.uncached.length === 1, 'uncached length is not correct');

    assert(data.cached[0].isin === cachedIsin, 'Cached item is not matched');
    assert(data.uncached[0] === uncachedIsin, 'Uncached item is not matched');

    // Cache api result
    cache.put(date, [{ isin: data.uncached[0], data: {} }]);
    assert(cache.size() === 2, '`size` is not correct after `put` api result');

    // Check cached data
    data = cache.getCachedData(request.date, request.isins);
    assert(data.cached.length === 2, 'cached length is not correct');
    assert(data.uncached.length === 0, 'uncached length is not correct');

    assert(data.cached[0].isin === cachedIsin, 'Cached item is not matched');
    assert(data.cached[1].isin === uncachedIsin, 'Cached item is not matched');

    cache.clear();
    assert(cache.size() === 0, '`size` is not correct after clear');
    })();