Skip to content

Commit ff54184

Browse files
committed
Fixing tests and bugs related to consecutiveErrors
1 parent 191998e commit ff54184

File tree

7 files changed

+163
-860
lines changed

7 files changed

+163
-860
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# cacheism
22
Simple caching library
33

4+
[![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)
5+
46
## Overview
57

68
The goal of cacheism is to wrap an async function with caching logic where we
@@ -72,13 +74,13 @@ Hit {
7274
version: 3,
7375
cacheName: '-internal/hoopla',
7476
cached: true,
75-
created: 2022-04-22T21:05:14.094Z,
77+
created: 2023-04-02T22:00:49.320Z,
7678
data: { message: 'Hoopla!' },
7779
error: Error: Death
78-
at /Users/andrewshell/code/personal/test-cacheism/index.js:8:15
79-
at Cacheism.go (/Users/andrewshell/code/personal/cacheism/lib/cacheism.js:29:30)
80-
at async run (/Users/andrewshell/code/personal/test-cacheism/index.js:7:13),
81-
errorTime: 2022-04-22T21:30:56.275Z,
80+
at /Users/andrewshell/code/test-cacheism/index.js:9:19
81+
at Cacheism.go (/Users/andrewshell/code/cacheism/lib/cacheism.js:30:30)
82+
at async run (/Users/andrewshell/code/test-cacheism/index.js:7:18),
83+
errorTime: 2023-04-02T22:00:49.928Z,
8284
consecutiveErrors: 1,
8385
etag: '"15-QcHvuZdyxCmLJ4zoYIPsP6pkNoM"',
8486
isHit: true,
@@ -100,12 +102,12 @@ Miss {
100102
version: 3,
101103
cacheName: '-internal/hoopla',
102104
cached: false,
103-
created: 2022-04-22T21:30:56.275Z,
105+
created: 2023-04-02T22:02:30.294Z,
104106
data: null,
105107
error: Error: Missing cache
106-
at Cacheism.go (/Users/andrewshell/code/personal/cacheism/lib/cacheism.js:27:19)
107-
at async run (/Users/andrewshell/code/personal/test-cacheism/index.js:7:18),
108-
errorTime: 2022-04-22T21:30:56.275Z,
108+
at Cacheism.go (/Users/andrewshell/code/cacheism/lib/cacheism.js:28:19)
109+
at async run (/Users/andrewshell/code/test-cacheism/index.js:7:18),
110+
errorTime: 2023-04-02T22:02:30.294Z,
109111
consecutiveErrors: 1,
110112
etag: null,
111113
isHit: false,

lib/cacheism.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Cacheism.prototype.go = async function (cacheDomain, cachePath, status, callback
2222

2323
try {
2424

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

3838
if (status >= this.status.cacheOnFail && hasCache) {
3939
response = existing;
40-
response.error = err;
40+
response.error = err.toString();
4141
response.errorTime = new Date();
4242
response.consecutiveErrors++;
4343
} else {
44-
response = new common.Miss(name, err, existing.consecutiveErrors + 1);
44+
response = new common.Miss(
45+
name,
46+
err.toString(),
47+
existing.consecutiveErrors + 1
48+
);
4549
}
4650

4751
}

lib/common.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ class Data {
7070
response = new Hit(this.cacheName, this.data, this.etag);
7171
response.cached = true;
7272
response.created = this.created;
73+
response.consecutiveErrors = this.consecutiveErrors;
7374
} else {
7475
response = new Miss(this.cacheName, this.error, this.consecutiveErrors);
75-
response.cached = true;
76+
response.cached = false;
7677
response.created = this.created;
7778
response.errorTime = this.errorTime;
7879
}
@@ -87,7 +88,7 @@ class Data {
8788
cacheName: this.cacheName,
8889
created: this.created,
8990
data: this.data,
90-
error: null == this.error ? null : this.error.toString(),
91+
error: this.error,
9192
errorTime: this.errorTime,
9293
consecutiveErrors: this.consecutiveErrors,
9394
etag: this.etag,

0 commit comments

Comments
 (0)