Skip to content

Commit

Permalink
Merge pull request #8 from djdalfaro/lfuBrowserCache
Browse files Browse the repository at this point in the history
Lfu browser cache
  • Loading branch information
NhanTLy authored Apr 9, 2021
2 parents f8b0e6e + ca1a2c0 commit 3c9b780
Show file tree
Hide file tree
Showing 10 changed files with 577 additions and 114 deletions.
7 changes: 4 additions & 3 deletions src/CacheClassBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import normalizeResult from './normalize.js';
import destructureQueries from './destructure.js';

export default class Cache {
constructor(
initialCache = {

constructor(
initialCache = {
ROOT_QUERY: {},
ROOT_MUTATION: {},
}
}
) {
this.storage = initialCache;
this.context = 'client';
Expand Down
140 changes: 51 additions & 89 deletions src/CacheClassServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,68 +17,22 @@ console.log(await redis.ping());

export class Cache {
constructor(
// initialCache = {
// ROOT_QUERY: {},
// ROOT_MUTATION: {},
// }
initialCache = {
ROOT_QUERY: {
'actor(id:1)': 'Actor~1',
movies: ['Movie~1', 'Movie~2', 'Movie~3', 'Movie~4'],
actors: ['Actor~1', 'Actor~2', 'Actor~3', 'Actor~4', 'Actor~6'],
'movies(input:{genre:ACTION})': ['Movie~1', 'Movie~4', 'Movie~5'],
},
ROOT_QUERY: {},
ROOT_MUTATION: {},
'Movie~1': {
id: '1',
title: 'Indiana Jones and the Last Crusade',
actors: ['Actor~1', 'Actor~2', 'Actor~6', 'Actor~7'],
genre: 'ACTION',
releaseYear: 1989,
},
'Movie~2': {
id: '2',
title: 'Empire Strikes Back',
actors: ['Actor~1', 'Actor~3'],
releaseYear: 1980,
},
'Movie~3': {
id: '3',
title: 'Witness',
actors: ['Actor~1', 'Actor~4'],
releaseYear: 1985,
},
'Movie~4': {
id: '4',
title: 'Air Force One',
actors: ['Actor~1', 'Actor~5'],
genre: 'ACTION',
releaseYear: 1997,
},
'Movie~5': 'DELETED',
'Actor~1': { id: '1', firstName: 'Harrison' },
'Actor~2': { id: '2', firstName: 'Sean' },
'Actor~3': { id: '3', firstName: 'Mark' },
'Actor~4': { id: '4', firstName: 'Patti' },
'Actor~5': { id: '5', firstName: 'Gary' },
'Actor~6': 'DELETED',
'Actor~7': { id: '7', firstName: 'Christy' }
}
) {
this.storage = initialCache;
this.context = window.Deno ? 'server' : 'client';
}

insertIntoRedis() {
// console.log('i am called');
for (let key in this.storage) {
redis.set(key, JSON.stringify(this.storage[key]));
}
}

pullOutCache() {

}
pullOutCache() {}

// Main functionality methods
async read(queryStr) {
Expand Down Expand Up @@ -157,54 +111,65 @@ export class Cache {
// go through root queries, remove all instances of bad hashes, add remaining hashes into goodHashes set
rootQueryCleaner(badHashes) {
const goodHashes = new Set();
const rootQuery = this.storage['ROOT_QUERY'];
for (let key in rootQuery) {
if (Array.isArray(rootQuery[key])) {
rootQuery[key] = rootQuery[key].filter(x => !badHashes.has(x));
if (rootQuery[key].length === 0) delete rootQuery[key];
for (let el of rootQuery[key]) goodHashes.add(el);
} else (badHashes.has(rootQuery[key])) ? delete rootQuery[key] : goodHashes.add(rootQuery[key]);
}
return goodHashes;
const rootQuery = this.storage['ROOT_QUERY'];
for (let key in rootQuery) {
if (Array.isArray(rootQuery[key])) {
rootQuery[key] = rootQuery[key].filter((x) => !badHashes.has(x));
if (rootQuery[key].length === 0) delete rootQuery[key];
for (let el of rootQuery[key]) goodHashes.add(el);
} else
badHashes.has(rootQuery[key])
? delete rootQuery[key]
: goodHashes.add(rootQuery[key]);
}
return goodHashes;
}

// Go through the cache, check good hashes for any nested hashes and add them to goodHashes set
getGoodHashes(badHashes, goodHashes) {
for (let key in this.storage) {
if (key === 'ROOT_QUERY' || key === 'ROOT_MUTATION') continue;
for (let i in this.storage[key]) {
if (Array.isArray(this.storage[key][i])) {
for (let el of this.storage[key][i]) {
if (el.includes('~') && !badHashes.has(el)) {
goodHashes.add(el);
}
}
} else if (typeof this.storage[key][i] === 'string') {
if (this.storage[key][i].includes('~') && !badHashes.has(this.storage[key][i])) {
goodHashes.add(this.storage[key][i]);
for (let key in this.storage) {
if (key === 'ROOT_QUERY' || key === 'ROOT_MUTATION') continue;
for (let i in this.storage[key]) {
if (Array.isArray(this.storage[key][i])) {
for (let el of this.storage[key][i]) {
if (el.includes('~') && !badHashes.has(el)) {
goodHashes.add(el);
}
}
} else if (typeof this.storage[key][i] === 'string') {
if (
this.storage[key][i].includes('~') &&
!badHashes.has(this.storage[key][i])
) {
goodHashes.add(this.storage[key][i]);
}
}
}
return goodHashes;
}
return goodHashes;
}

// Remove inaccessible hashes by checking if they are in goodhashes set or not
removeInaccessibleHashes(badHashes, goodHashes) {
for (let key in this.storage) {
if (key === 'ROOT_QUERY' || key === 'ROOT_MUTATION') continue;
if (!goodHashes.has(key)) delete this.storage[key];
for (let i in this.storage[key]) {
if (Array.isArray(this.storage[key][i])) {
this.storage[key][i] = this.storage[key][i].filter(x => !badHashes.has(x));
} else if (typeof this.storage[key][i] === 'string') {
if (this.storage[key][i].includes('~') && badHashes.has(this.storage[key][i])) {
delete this.storage[key][i];
}
for (let key in this.storage) {
if (key === 'ROOT_QUERY' || key === 'ROOT_MUTATION') continue;
if (!goodHashes.has(key)) delete this.storage[key];
for (let i in this.storage[key]) {
if (Array.isArray(this.storage[key][i])) {
this.storage[key][i] = this.storage[key][i].filter(
(x) => !badHashes.has(x)
);
} else if (typeof this.storage[key][i] === 'string') {
if (
this.storage[key][i].includes('~') &&
badHashes.has(this.storage[key][i])
) {
delete this.storage[key][i];
}
}
}
}
}

// cache read/write helper methods
async cacheRead(hash) {
Expand Down Expand Up @@ -288,7 +253,7 @@ export class Cache {
// for each hash from the input query, build the response object
const readVal = await this.cacheRead(hash);
// return undefine if hash has been garbage collected
if (readVal === undefined) return undefined
if (readVal === undefined) return undefined;
if (readVal === 'DELETED') return acc;
const dataObj = {};
for (const field in fields) {
Expand All @@ -311,17 +276,14 @@ export class Cache {
}
}
// acc is an array within a Response object for each hash
try{
const resolvedProm = await Promise.resolve(acc)
try {
const resolvedProm = await Promise.resolve(acc);
resolvedProm.push(dataObj);
return resolvedProm;
} catch (error){
return undefined
} catch (error) {
return undefined;
}

}, []);


}
// Case where allHashesFromQuery has only one hash and is not an array but a single string
const hash = allHashesFromQuery;
Expand Down Expand Up @@ -351,4 +313,4 @@ export class Cache {
return dataObj;
}
}
}
}
Loading

0 comments on commit 3c9b780

Please sign in to comment.