Skip to content

Commit

Permalink
Merge pull request #32 from ghedamat/rename-revision-list-key
Browse files Browse the repository at this point in the history
store revision list in `keyPrefix:revisions`
  • Loading branch information
lukemelia committed Sep 16, 2015
2 parents e2a0850 + 1999ef8 commit 027297e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ So, if the `keyPrefix` was configured to be `my-app:index` and there had been 3
$ redis-cli

127.0.0.1:6379> KEYS *
1) my-app:index
1) my-app:index:revisions
2) my-app:index:9ab2021411f0cbc5ebd5ef8ddcd85cef
3) my-app:index:499f5ac793551296aaf7f1ec74b2ca79
4) my-app:index:f769d3afb67bd20ccdb083549048c86c
Expand All @@ -195,7 +195,7 @@ $ ember deploy:activate f769d3afb67bd20ccdb083549048c86c
$ redis-cli

127.0.0.1:6379> KEYS *
1) my-app:index
1) my-app:index:revisions
2) my-app:index:9ab2021411f0cbc5ebd5ef8ddcd85cef
3) my-app:index:499f5ac793551296aaf7f1ec74b2ca79
4) my-app:index:f769d3afb67bd20ccdb083549048c86c
Expand Down
11 changes: 7 additions & 4 deletions lib/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ module.exports = CoreObject.extend({

_listRevisions: function(keyPrefix) {
var client = this._client;
return client.zrevrange(keyPrefix, 0, -1);
var listKey = keyPrefix + ':revisions';
return client.zrevrange(listKey, 0, -1);
},

_validateRevisionKey: function(revisionKey, revisions) {
Expand Down Expand Up @@ -110,7 +111,8 @@ module.exports = CoreObject.extend({
_updateRecentUploadsList: function(keyPrefix, revisionKey) {
var client = this._client;
var score = new Date().getTime();
return client.zadd(keyPrefix, score, revisionKey);
var listKey = keyPrefix + ':revisions';
return client.zadd(listKey, score, revisionKey);
},

_activeRevision: function(keyPrefix) {
Expand All @@ -120,9 +122,10 @@ module.exports = CoreObject.extend({

_trimRecentUploadsList: function(keyPrefix, maxEntries) {
var client = this._client;
var listKey = keyPrefix + ':revisions';

return Promise.hash({
revisionsToBeRemoved: client.zrange(keyPrefix, 0, -(maxEntries + 1)),
revisionsToBeRemoved: client.zrange(listKey, 0, -(maxEntries + 1)),
current: this._activeRevision(keyPrefix)
}).then(function(results) {
var revisions = results.revisionsToBeRemoved;
Expand All @@ -133,7 +136,7 @@ module.exports = CoreObject.extend({
revisions.forEach(function(revision) {
if (revision !== current) {
client.del(keyPrefix + ":" + revision);
client.zrem(keyPrefix, revision);
client.zrem(listKey, revision);
}
});
});
Expand Down
7 changes: 4 additions & 3 deletions tests/helpers/fake-redis-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ module.exports = CoreObject.extend({
set: function() { },
del: function() { },
zadd: function(key, score , tag) {
this.recentRevisions.push(key + ':' + tag);
var prefix = key.replace(':revisions','');
this.recentRevisions.push(prefix + ':' + tag);
},
zrem: function(val,revision) {
var i = this.recentRevisions.indexOf(revision)
zrem: function(key, revision) {
var i = this.recentRevisions.indexOf(revision);
this.recentRevisions.splice(i,1);
},
zrange: function() {
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/lib/redis-nodetest.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ describe('redis', function() {
},
set: function(key, value) {
fileUploaded = true;
},
zadd: function(key, value) {
assert(key.match(/:revisions$/));
},
zrange: function(key, value) {
assert(key.match(/:revisions$/));
}
})));

Expand Down Expand Up @@ -87,6 +93,10 @@ describe('redis', function() {
},
zrange: function() {
return this.recentRevisions.slice(0,2);
},
zrem: function(key) {
assert(key.match(/:revisions$/));
return this._super.apply(this, arguments);
}
})));

Expand Down Expand Up @@ -233,6 +243,10 @@ describe('redis', function() {
var redis = new Redis({}, new FakeRedis(FakeClient.extend({
get: function() {
return currentRevision;
},
zrevrange: function(key) {
assert(key.match(/:revisions$/));
return this._super.apply(this, arguments);
}
})));

Expand Down

0 comments on commit 027297e

Please sign in to comment.