{"size": 778, "ext": "js", "max_stars_repo_path": "src/actions/navigator/cache.js", "max_stars_repo_name": "andornaut/amp-media-player", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/actions/navigator/cache.js", "max_issues_repo_name": "andornaut/amp-media-player", "max_issues_repo_issues_event_min_datetime": "2021-10-05T19:51:11.000Z", "max_issues_repo_issues_event_max_datetime": "2021-10-05T19:51:11.000Z", "max_forks_repo_path": "src/actions/navigator/cache.js", "max_forks_repo_name": "andornaut/amp-media-player", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 20.4736842105, "max_line_length": 85, "alphanum_fraction": 0.5642673522}
const queue = [];

const MAX_SIZE = 20;

export const clearCache = () => {
  queue.length = 0;
};

export const getFromCache = (key) => {
  const start = queue.length - 1;
  for (let i = start; i >= 0; i -= 1) {
    const [currentKey, val] = queue[i];
    if (currentKey === key) {
      return val;
    }
  }
  return null;
};

export const removeFromCache = (key) => {
  const start = queue.length - 1;
  for (let i = start; i >= 0; i -= 1) {
    const [currentKey] = queue[i];
    if (currentKey === key) {
      queue.splice(i, 1);
      return;
    }
  }
};

export const setInCache = (key, val) => {
  removeFromCache(key); // May not shrink the backing array if they key is not found.
  if (queue.length >= MAX_SIZE) {
    queue.shift();
  }
  queue.push([key, val]);
};
