Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use LRU-Cache for faster storage

  • Loading branch information...
commit e4c9da33fcd201ec51e967fadc9e13073195468f 1 parent 65bae83
@indutny authored
View
69 lib/tracker/db.mongo.js
@@ -1,10 +1,12 @@
/**
* Init db
*/
-var mongoose = require('mongoose').Mongoose;
+var mongoose = require('mongoose').Mongoose,
+ LRU = require("lru-cache");
module.exports = function(options, callback) {
- var db = mongoose.connect(options.mongo_url);
+ var db = mongoose.connect(options.mongo_url),
+ cache = LRU(100);
mongoose.model('Peer', {
properties: ['peer_id', 'info_hash', 'short_addr', 'ip', 'port', 'left',
@@ -37,33 +39,48 @@ module.exports = function(options, callback) {
{updated_at: -1}
],
static: {
- cleanup: function cleanup(since) {
+ cleanup: function(since) {
this.remove({updated_at: {$lt: since}}, function() {});
},
- access: function access(info_hash, numwant, callback) {
- this.find({info_hash: info_hash}).one(function(cache) {
- if (!cache) {
- db.Peer
- .find({info_hash: info_hash})
- .sort([['updated_at', -1]])
- .limit(numwant)
- .all(function(docs) {
- if (!docs) return callback(Error('Cache.access error'));
-
- var peers = docs.map(peer_id).join('');
+ access: function(info_hash, cache_time, callback) {
+ var cached = cache.get(info_hash);
+
+ if (cached && (cached.updated_at < (+new Date - cache_time))) {
+ cache.del(info_hash);
+ cached = null;
+ }
+
+ if (!cached) {
+ this.find({info_hash: info_hash}).one(function(cached) {
+ if (!cached) {
+ db.Peer
+ .find({info_hash: info_hash})
+ .sort([['updated_at', -1]])
+ .limit(100)
+ .all(function(docs) {
+ if (!docs) return callback(Error('Cache.access error'));
+
+ var peers = docs.map(peer_id).join('');
+
+ callback(null, peers);
+
+ cached = new (db.Cache)({
+ info_hash: info_hash,
+ peers: escape(peers),
+ updated_at: +new Date
+ }).save();
- callback(null, peers);
-
- new (db.Cache)({
- info_hash: info_hash,
- peers: escape(peers),
- updated_at: +new Date
- }).save();
- });
- } else {
- callback(null, unescape(cache.peers));
- }
- });
+ cached.peers = peers;
+ cache.set(info_hash, cached);
+ });
+ } else {
+ callback(null, cached.peers = unescape(cached.peers));
+ cache.set(info_hash, cached);
+ }
+ });
+ } else {
+ callback(null, cached.peers);
+ }
}
}
});
View
3  lib/tracker/router.js
@@ -39,8 +39,7 @@ module.exports = function(db, options, callback) {
peer_id: helpers.decodeInfoHash(peer_id),
port: port,
left: left,
- ip: ip,
- _numwant: numwant
+ ip: ip
};
storage.route(event, peer, function(err, data) {
View
5 lib/tracker/storage.mongo.js
@@ -66,9 +66,10 @@ Storage.prototype.put = function(event, peer, callback) {
*/
Storage.prototype.list = function(peer, callback) {
var options = this._options,
- db = this._db;
+ db = this._db,
+ cache_time = this._options.announce_interval * 500;
- db.Cache.access(peer.info_hash, peer._numwant, function(err, peers) {
+ db.Cache.access(peer.info_hash, cache_time, function(err, peers) {
if (err) return callback(err);
var result = {
View
3  package.json
@@ -6,7 +6,8 @@
"vows": ">=0.5.2",
"connect": ">=0.3.0",
"mongoose": ">=0.0.3",
- "optimist": ">=0.1.1"
+ "optimist": ">=0.1.1",
+ "lru-cache": ">=1.0.2"
},
"version" : "0.1.0",
"engines" : { "node": ">=0.3.1" }
Please sign in to comment.
Something went wrong with that request. Please try again.