Skip to content

Commit d702c72

Browse files
committed
added lru cache for statements
1 parent 75e7091 commit d702c72

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

lib/commands/change_user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ ChangeUser.prototype.start = function (packet, connection) {
3737
this.currentConfig.database = this.database;
3838
this.currentConfig.charsetNumber = this.charsetNumber;
3939
// reset prepared statements cache as all statements become invalid after changeUser
40-
connection._statements = {};
40+
connection._statements.reset();
4141
connection.writePacket(packet.toPacket());
4242
return ChangeUser.prototype.handshakeResult;
4343
};

lib/connection.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ var Tls = require('tls');
44
var Timers = require('timers');
55
var EventEmitter = require('events').EventEmitter;
66
var Queue = require('double-ended-queue');
7-
87
var SqlString = require('sqlstring');
8+
var LRU = require('lru-cache');
99

1010
var PacketParser = require('./packet_parser.js');
1111
var Packet = require('./packets/packet.js');
@@ -51,7 +51,10 @@ function Connection (opts)
5151
this._paused = false;
5252
this._paused_packets = new Queue();
5353

54-
this._statements = {};
54+
this._statements = LRU({
55+
max: 1000,
56+
dispose: function (key, statement) { statement.close(); }
57+
});
5558

5659
// TODO: make it lru cache
5760
// https://github.com/mercadolibre/node-simple-lru-cache
@@ -498,9 +501,9 @@ Connection.prototype.unprepare = function unprepare (sql) {
498501
options.sql = sql;
499502
}
500503
var key = statementKey(options);
501-
var stmt = this._statements[key];
504+
var stmt = this._statements.get(key);
502505
if (stmt) {
503-
this._statements[key] = null;
506+
this._statements.del(key);
504507
stmt.close();
505508
}
506509
return stmt;
@@ -531,7 +534,7 @@ Connection.prototype.execute = function execute (sql, values, cb) {
531534

532535
var connection = this;
533536
var key = statementKey(options);
534-
var statement = connection._statements[key];
537+
var statement = connection._statements.get(key);
535538

536539
options.statement = statement;
537540
var executeCommand = new Commands.Execute(options, cb);
@@ -547,7 +550,7 @@ Connection.prototype.execute = function execute (sql, values, cb) {
547550
return;
548551
}
549552
executeCommand.statement = stmt;
550-
connection._statements[key] = stmt;
553+
connection._statements.set(key, stmt);
551554
connection.addCommand(executeCommand);
552555
});
553556
} else {

0 commit comments

Comments
 (0)