Skip to content

Commit

Permalink
Merge branch 'release/1.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
drwatsno committed Jul 8, 2019
2 parents 5126d86 + e7a4e34 commit 987c732
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 221 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

#### [1.1.1]
- run executor after storage throws an error
- change default operation timeout to 150

#### [1.1.0]
- support for dynamic tags in Manager's default storage via "getTags" option.
- updated jest
Expand Down
392 changes: 183 additions & 209 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cachalot",
"version": "1.1.0",
"version": "1.1.1",
"description": "Cache manager for nodejs with support different cache strategies",
"keywords": [
"cache",
Expand Down Expand Up @@ -38,17 +38,17 @@
"lodash": "^4.17.11"
},
"devDependencies": {
"@types/ioredis": "^4.0.10",
"@types/jest": "^24.0.13",
"@types/lodash": "^4.14.132",
"@types/ioredis": "^4.0.12",
"@types/jest": "^24.0.15",
"@types/lodash": "^4.14.135",
"@types/node": "^8",
"coveralls": "^3.0.3",
"ioredis": "^4.9.5",
"coveralls": "^3.0.4",
"ioredis": "^4.11.1",
"jest": "^24.8.0",
"ts-jest": "^24.0.2",
"tslint": "^5.16.0",
"tslint": "^5.18.0",
"tslint-config-unional": "^0.10.0",
"typedoc": "^0.14.2",
"typescript": "^3.4.5"
"typescript": "^3.5.2"
}
}
2 changes: 1 addition & 1 deletion src/adapters/redis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { withTimeout } from '../../with-timeout';
* Hash prefix, used in set, get commands
*/
export const CACHE_PREFIX = 'cache';
export const DEFAULT_OPERATION_TIMEOUT = 200;
export const DEFAULT_OPERATION_TIMEOUT = 150;
export const DEFAULT_LOCK_EXPIRES = 20000;

export type CommandArgument = string | number;
Expand Down
13 changes: 13 additions & 0 deletions src/managers/read-through.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,17 @@ describe('ReadThroughManager', () => {

expect(result).toEqual({ test: 123 });
});

it('get returns result from executor if storage methods throws errors', async () => {
const testStorage = new TestStorage(internalStorage);

testStorage.get.mockImplementation(async () => { throw new Error('Operation timeout after 200'); });

const testManager: any = new ReadThroughManager({
storage: testStorage,
logger
});

await expect(testManager.get('test', async () => ({ test: 123 }))).resolves.toEqual({ test: 123 });
});
});
10 changes: 9 additions & 1 deletion src/managers/read-through.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ class ReadThroughManager extends BaseManager {
public async get<E extends Executor>(key: string, executor: E, options: ReadWriteOptions = {}):
Promise<ValueOfExecutor<E>> {
const executorContext = { key, executor, options };
const record = await this.storage.get(key);
let record: StorageRecord | null = null;

try {
record = await this.storage.get(key);
} catch (e) {
this.logger.error('Failed to get value from storage, falling back to executor', e);

return executor();
}

if (await this.isRecordValid(record)) {
this.logger.trace('hit', key);
Expand Down
17 changes: 17 additions & 0 deletions src/managers/refresh-ahead.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,23 @@ describe('RefreshAheadManager', () => {
expect(result).toEqual({ test: 123 });
});

it('get returns result from executor if storage methods throws errors', async () => {
const testStorage = new TestStorage(internalStorage);

testStorage.get.mockImplementation(async () => { throw new Error('Operation timeout after 200'); });

const testManager: any = new RefreshAheadManager({
storage: testStorage,
prefix: 'cache',
hashKeys: false,
expiresIn: 10000,
logger,
refreshAheadFactor: 0.5
});

await expect(testManager.get('test', async () => ({ test: 123 }))).resolves.toEqual({ test: 123 });
});

it('isRecordExpireSoon returns false if record is null', () => {
expect(manager.isRecordExpireSoon(null)).toEqual(false);
});
Expand Down
10 changes: 9 additions & 1 deletion src/managers/refresh-ahead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,15 @@ class RefreshAheadManager extends BaseManager {
public async get<E extends Executor>(key: string, executor: E, options: ReadWriteOptions = {}):
Promise<ValueOfExecutor<E>> {
const executorContext = { key, executor, options };
const record = await this.storage.get(key);
let record: StorageRecord | null = null;

try {
record = await this.storage.get(key);
} catch (e) {
this.logger.error('Failed to get value from storage, falling back to executor', e);

return executor();
}

if (await this.isRecordValid(record)) {
const result = deserialize((record as any).value);
Expand Down
16 changes: 16 additions & 0 deletions src/managers/write-through.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,20 @@ describe('WriteThroughManager', () => {

expect(result).toEqual({ test: 123 });
});

it('get returns result from executor if storage methods throws errors', async () => {
const testStorage = new TestStorage(internalStorage);

testStorage.get.mockImplementation(async () => { throw new Error('Operation timeout after 200'); });

const testManager: any = new WriteThroughManager({
storage: testStorage,
prefix: 'cache',
hashKeys: false,
expiresIn: 10000,
logger
});

await expect(testManager.get('test', async () => ({ test: 123 }))).resolves.toEqual({ test: 123 });
});
});
11 changes: 10 additions & 1 deletion src/managers/write-through.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ class WriteThroughManager extends BaseManager {
}

public async get<E extends Executor>(key: string, executor: E, options: ReadWriteOptions = {}): Promise<ValueOfExecutor<E>> {
const record = await this.storage.get(key);
let record: StorageRecord | null = null;

try {
record = await this.storage.get(key);
} catch (e) {
this.logger.error('Failed to get value from storage, falling back to executor', e);

return executor();
}

const executorContext = { key, executor, options };

if (await this.isRecordValid(record)) {
Expand Down

0 comments on commit 987c732

Please sign in to comment.