Skip to content

Commit

Permalink
Fixed memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
Acanguven committed Apr 13, 2019
1 parent 254d08c commit 9c6115a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [1.0.2] - 2017-04-13

## [1.2.2] - 2017-04-13
### Fixed
- Cache leak for memory plugin


## [1.2.1] - 2017-04-13
### Fixed
- Added getting Enum values from string implementing cache strategy
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Warden
`Under Development`

Warden is an outgoing request optimizer for creating fast and scalable applications. Warden is being used in [PuzzleJs](https://github.com/puzzle-js/puzzle-js) framework for gateway communication.

Expand Down
28 changes: 22 additions & 6 deletions src/memory-cache.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import {CachePlugin} from "./cache-factory";

type CacheEntry = {
value: unknown;
expire: number | null;
};

class MemoryCache implements CachePlugin {
constructor() {
this.invalidate = this.invalidate.bind(this);
setInterval(this.invalidate, 60000);
}

cache: {
[key: string]: {
value: unknown;
expire: number | null;
}
[key: string]: CacheEntry
} = {};

async get<T>(key: string): Promise<T | null> {
if (!this.cache[key]) return null;

if (this.cache[key].expire && this.cache[key].expire! < Date.now()) {
if (this.isExpired(this.cache[key])) {
delete this.cache[key];
return null;
}
Expand All @@ -27,7 +33,17 @@ class MemoryCache implements CachePlugin {
};
}

// todo Clear storage when expired with timer
invalidate() {
for (const key in this.cache) {
if (this.isExpired(this.cache[key])) {
delete this.cache[key];
}
}
}

private isExpired(cacheEntry: CacheEntry) {
return cacheEntry.expire && cacheEntry.expire < Date.now();
}
}

export {
Expand Down
30 changes: 30 additions & 0 deletions test/memory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,34 @@ describe("[memory.ts]", () => {
// Assert
expect(cachedValue).to.eq(null);
});

it("should invalidate cache and remove entries", async () => {
// Arrange
const key = faker.random.word();
const value = faker.random.word();
const memoryCache = new MemoryCache();
const expire = -20; //Auto expire

// Act
await memoryCache.set(key, value, expire);
memoryCache.invalidate();

// Assert
expect(Object.keys(memoryCache.cache).length).to.eq(0);
});

it("should invalidate without removing valid entries", async () => {
// Arrange
const key = faker.random.word();
const value = faker.random.word();
const memoryCache = new MemoryCache();
const expire = Date.now();

// Act
await memoryCache.set(key, value, expire);
memoryCache.invalidate();

// Assert
expect(Object.keys(memoryCache.cache).length).to.eq(1);
});
});

0 comments on commit 9c6115a

Please sign in to comment.