Skip to content

Commit

Permalink
Fixing tests and bugs related to consecutiveErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewshell committed Apr 3, 2023
1 parent 191998e commit ff54184
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 860 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# cacheism
Simple caching library

[![Node.js CI](https://github.com/andrewshell/cacheism/actions/workflows/node.js.yml/badge.svg)](https://github.com/andrewshell/cacheism/actions/workflows/node.js.yml)

## Overview

The goal of cacheism is to wrap an async function with caching logic where we
Expand Down Expand Up @@ -72,13 +74,13 @@ Hit {
version: 3,
cacheName: '-internal/hoopla',
cached: true,
created: 2022-04-22T21:05:14.094Z,
created: 2023-04-02T22:00:49.320Z,
data: { message: 'Hoopla!' },
error: Error: Death
at /Users/andrewshell/code/personal/test-cacheism/index.js:8:15
at Cacheism.go (/Users/andrewshell/code/personal/cacheism/lib/cacheism.js:29:30)
at async run (/Users/andrewshell/code/personal/test-cacheism/index.js:7:13),
errorTime: 2022-04-22T21:30:56.275Z,
at /Users/andrewshell/code/test-cacheism/index.js:9:19
at Cacheism.go (/Users/andrewshell/code/cacheism/lib/cacheism.js:30:30)
at async run (/Users/andrewshell/code/test-cacheism/index.js:7:18),
errorTime: 2023-04-02T22:00:49.928Z,
consecutiveErrors: 1,
etag: '"15-QcHvuZdyxCmLJ4zoYIPsP6pkNoM"',
isHit: true,
Expand All @@ -100,12 +102,12 @@ Miss {
version: 3,
cacheName: '-internal/hoopla',
cached: false,
created: 2022-04-22T21:30:56.275Z,
created: 2023-04-02T22:02:30.294Z,
data: null,
error: Error: Missing cache
at Cacheism.go (/Users/andrewshell/code/personal/cacheism/lib/cacheism.js:27:19)
at async run (/Users/andrewshell/code/personal/test-cacheism/index.js:7:18),
errorTime: 2022-04-22T21:30:56.275Z,
at Cacheism.go (/Users/andrewshell/code/cacheism/lib/cacheism.js:28:19)
at async run (/Users/andrewshell/code/test-cacheism/index.js:7:18),
errorTime: 2023-04-02T22:02:30.294Z,
consecutiveErrors: 1,
etag: null,
isHit: false,
Expand Down
10 changes: 7 additions & 3 deletions lib/cacheism.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Cacheism.prototype.go = async function (cacheDomain, cachePath, status, callback

try {

if (status >= this.status.preferCache && hasCache) {
if (status >= this.status.preferCache && hasCache && existing.isHit) {
response = existing;
} else if (status === this.status.onlyCache) {
throw new Error('Missing cache');
Expand All @@ -37,11 +37,15 @@ Cacheism.prototype.go = async function (cacheDomain, cachePath, status, callback

if (status >= this.status.cacheOnFail && hasCache) {
response = existing;
response.error = err;
response.error = err.toString();
response.errorTime = new Date();
response.consecutiveErrors++;
} else {
response = new common.Miss(name, err, existing.consecutiveErrors + 1);
response = new common.Miss(
name,
err.toString(),
existing.consecutiveErrors + 1
);
}

}
Expand Down
5 changes: 3 additions & 2 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ class Data {
response = new Hit(this.cacheName, this.data, this.etag);
response.cached = true;
response.created = this.created;
response.consecutiveErrors = this.consecutiveErrors;
} else {
response = new Miss(this.cacheName, this.error, this.consecutiveErrors);
response.cached = true;
response.cached = false;
response.created = this.created;
response.errorTime = this.errorTime;
}
Expand All @@ -87,7 +88,7 @@ class Data {
cacheName: this.cacheName,
created: this.created,
data: this.data,
error: null == this.error ? null : this.error.toString(),
error: this.error,
errorTime: this.errorTime,
consecutiveErrors: this.consecutiveErrors,
etag: this.etag,
Expand Down

0 comments on commit ff54184

Please sign in to comment.