export default class CacheManager {

    constructor() {

        this.memory = new Map();

    }

    get(key) {

        if (this.memory.has(key)) {
            return this.memory.get(key);
        }

        const session = sessionStorage.getItem(key);

        if (session) {

            const parsed = JSON.parse(session);

            this.memory.set(key, parsed);

            return parsed;

        }

        return null;

    }

    set(key, value) {

        this.memory.set(key, value);

        sessionStorage.setItem(key, JSON.stringify(value));

    }

    clear(key) {

        this.memory.delete(key);

        sessionStorage.removeItem(key);

    }

    clearAll() {

        this.memory.clear();

        sessionStorage.clear();

    }

}
