Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lru cache #401

Merged
merged 5 commits into from
Sep 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions documentation/Prepared-Statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ connection.prepare('select ? + ? as tests', function(err, statement) {
});
```
Note that you should not use statement after connection reset (`changeUser()` or disconnect). Statement scope is connection, you need to prepare statement for each new connection in order to use it.

# Configuration

`maxPreparedStatements` : We keep the cached statements in a [lru-cache](https://github.com/isaacs/node-lru-cache). Default size is `16000` but you can use this option to override it. Any statements that are dropped from cache will be `closed`.
2 changes: 1 addition & 1 deletion lib/commands/change_user.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ ChangeUser.prototype.start = function (packet, connection) {
this.currentConfig.database = this.database;
this.currentConfig.charsetNumber = this.charsetNumber;
// reset prepared statements cache as all statements become invalid after changeUser
connection._statements = {};
connection._statements.reset();
connection.writePacket(packet.toPacket());
return ChangeUser.prototype.handshakeResult;
};
Expand Down
15 changes: 9 additions & 6 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ var Tls = require('tls');
var Timers = require('timers');
var EventEmitter = require('events').EventEmitter;
var Queue = require('double-ended-queue');

var SqlString = require('sqlstring');
var LRU = require('lru-cache');

var PacketParser = require('./packet_parser.js');
var Packet = require('./packets/packet.js');
Expand Down Expand Up @@ -51,7 +51,10 @@ function Connection (opts)
this._paused = false;
this._paused_packets = new Queue();

this._statements = {};
this._statements = LRU({
max: this.config.maxPreparedStatements,
dispose: function (key, statement) { statement.close(); }
});

// TODO: make it lru cache
// https://github.com/mercadolibre/node-simple-lru-cache
Expand Down Expand Up @@ -498,9 +501,9 @@ Connection.prototype.unprepare = function unprepare (sql) {
options.sql = sql;
}
var key = statementKey(options);
var stmt = this._statements[key];
var stmt = this._statements.get(key);
if (stmt) {
this._statements[key] = null;
this._statements.del(key);
stmt.close();
}
return stmt;
Expand Down Expand Up @@ -531,7 +534,7 @@ Connection.prototype.execute = function execute (sql, values, cb) {

var connection = this;
var key = statementKey(options);
var statement = connection._statements[key];
var statement = connection._statements.get(key);

options.statement = statement;
var executeCommand = new Commands.Execute(options, cb);
Expand All @@ -547,7 +550,7 @@ Connection.prototype.execute = function execute (sql, values, cb) {
return;
}
executeCommand.statement = stmt;
connection._statements[key] = stmt;
connection._statements.set(key, stmt);
connection.addCommand(executeCommand);
});
} else {
Expand Down
1 change: 1 addition & 0 deletions lib/connection_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function ConnectionConfig (options) {
options.flags || '');

this.connectAttributes = options.connectAttributes;
this.maxPreparedStatements = options.maxPreparedStatements || 16000;
}

ConnectionConfig.mergeFlags = function (default_flags, user_flags) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"double-ended-queue": "2.1.0-0",
"iconv-lite": "^0.4.13",
"long": "^3.2.0",
"lru-cache": "^4.0.1",
"named-placeholders": "1.1.1",
"object-assign": "^4.1.0",
"readable-stream": "2.1.5",
Expand Down
7 changes: 3 additions & 4 deletions test/integration/connection/test-execute-cached.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ connection.execute(q, [123], function (err, _rows, _fields) {
throw err;
}
rows2 = _rows;
assert(Object.keys(connection._statements).length == 1);
assert(connection._statements[key].query == q);
assert(connection._statements[key].parameters.length == 1);
assert(connection._statements.length == 1);
assert(connection._statements.get(key).query == q);
assert(connection._statements.get(key).parameters.length == 1);
connection.end();
});
});
Expand All @@ -38,4 +38,3 @@ process.on('exit', function () {
assert.deepEqual(rows1, [{'test': 125}]);
assert.deepEqual(rows2, [{'test': 126}]);
});