Skip to content

Commit

Permalink
change memory cacher to map version
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Apr 24, 2017
1 parent 267824c commit 2da889d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
11 changes: 2 additions & 9 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,11 @@

## v0.7.x
- [x] [Distributed request timeout](https://www.datawire.io/guide/traffic/deadlines-distributed-timeouts-microservices/). Reduce the original timeout value in every calls.
- [ ] official API gateway service (Native, Express, [Aero](https://github.com/aerojs/aero), [uWebsocket HTTP](https://github.com/uWebSockets/bindings/blob/master/nodejs/examples/http_sillybenchmark.js), [benchmark](https://github.com/blitzprog/webserver-benchmarks))

- [x] metricsRate: doesn't measure every request
- [x] add nodeID to EVENT package and it will be the 3rd param in event handlers

- [ ] handleExceptions: true option
Catch unhandled exceptions and send an event with details. Can be catch in metrics, alert or logger services ([example](https://github.com/winstonjs/winston/blob/master/lib/winston/exception.js))

- [x] move heart-beat handling to `Transit` class.

- [x] meta in context which will be transported. (for user, session, state...etc)

- [x] create d.ts file

- [x] pluggable serializer for transport
- [x] JSON parse/stringify
- [x] [Avro](https://github.com/mtth/avsc)
Expand All @@ -31,6 +22,8 @@
- [ ] https://github.com/segmentio/metalsmith
- [ ] add lru features to Memory and Redis cachers

- [ ] official API gateway service (Native, Express, [Aero](https://github.com/aerojs/aero), [uWebsocket HTTP](https://github.com/uWebSockets/bindings/blob/master/nodejs/examples/http_sillybenchmark.js), [benchmark](https://github.com/blitzprog/webserver-benchmarks))

## v1.0.x
It will be the first stable release. Afterwards the version numbers should follow semver versioning.

Expand Down
12 changes: 6 additions & 6 deletions benchmark/suites/cachers.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ Platform info:
Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz × 8
Suite: Set & get 1k data with cacher
√ Memory* 2,409,090 rps
√ MemoryMap 9,968,747 rps
√ Redis* 10,992 rps
√ Memory* 2,428,800 rps
√ MemoryMap 4,733,539 rps
√ Redis* 11,050 rps
Memory* -75.83% (2,409,090 rps) (avg: 415ns)
MemoryMap 0% (9,968,747 rps) (avg: 100ns)
Redis* -99.89% (10,992 rps) (avg: 90μs)
Memory* -48.69% (2,428,800 rps) (avg: 411ns)
MemoryMap 0% (4,733,539 rps) (avg: 211ns)
Redis* -99.77% (11,050 rps) (avg: 90μs)
-----------------------------------------------------------------------
*/
4 changes: 2 additions & 2 deletions src/cachers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"use strict";

module.exports = {
Memory: require("./memory"),
//MemoryMap: require("./memory-map"),
//Memory: require("./memory"), - slower
Memory: require("./memory-map"),
Redis: require("./redis")
};
22 changes: 12 additions & 10 deletions src/cachers/memory-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ class MemoryMapCacher extends BaseCacher {
* @memberOf MemoryMapCacher
*/
get(key) {
this.logger.debug(`Get ${key}`);
//this.logger.debug(`Get ${key}`);

if (this.cache.has(key)) {
this.logger.debug(`Found ${key}`);
//this.logger.debug(`Found ${key}`);

let item = this.cache.get(key);

if (this.opts.ttl) {
// Update expire time (hold in the cache if we are using it)
item.expire = Date.now() + this.opts.ttl * 1000;
}
return item.data;
return Promise.resolve(item.data);
}
return null;
return Promise.resolve(null);
}

/**
Expand All @@ -81,7 +81,7 @@ class MemoryMapCacher extends BaseCacher {
expire: this.opts.ttl ? Date.now() + this.opts.ttl * 1000 : null
});
this.logger.debug(`Set ${key}`);
return data;
return Promise.resolve(data);
}

/**
Expand All @@ -95,7 +95,7 @@ class MemoryMapCacher extends BaseCacher {
del(key) {
this.cache.delete(key);
this.logger.debug(`Delete ${key}`);
return ;
return Promise.resolve();
}

/**
Expand All @@ -106,14 +106,16 @@ class MemoryMapCacher extends BaseCacher {
* @memberOf Cacher
*/
clean(match = "**") {
this.logger.debug(`Clean ${match}`);
this.logger.debug(`CLEAN ${match}`);

this.cache.keys.forEach((key) => {
if (micromatch.isMatch(key, match))
if (micromatch.isMatch(key, match)) {
this.logger.debug(`REMOVE ${key}`);
this.del(key);
}
});

return ;
return Promise.resolve();
}

/**
Expand All @@ -128,7 +130,7 @@ class MemoryMapCacher extends BaseCacher {
let item = this.cache.get(key);

if (item.expire && item.expire < now) {
this.logger.debug(`Expired ${key}`);
this.logger.debug(`EXPIRED ${key}`);
self.cache.delete(key);
}
});
Expand Down

0 comments on commit 2da889d

Please sign in to comment.