cache-storage
Version:
[ABANDONED] Advanced cache storage for node js
176 lines (165 loc) • 5.47 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var Cache, RedisStorage, async, cache, expect, fs, path, redis;
expect = require('chai').expect;
path = require('path');
async = require('async');
Cache = require('../../../../../lib/Cache');
RedisStorage = require('../../../../../Storage/RedisAsyncStorage');
fs = null;
cache = null;
redis = null;
describe('RedisAsyncStorage', function() {
beforeEach(function() {
cache = new Cache(new RedisStorage('/temp'), 'test');
fs = Cache.mockFs({
'temp': {},
'file': ''
});
return redis = cache.storage.client;
});
afterEach(function(done) {
Cache.restoreFs();
return redis.FLUSHDB(function() {
return done();
});
});
describe('saving/loading', function() {
it('should save true and load it', function(done) {
return cache.save('true', true, function() {
return cache.load('true', function(err, data) {
expect(data).to.be["true"];
return done();
});
});
});
it('should return null if item not exists', function(done) {
return cache.load('true', function(err, data) {
expect(data).to.be["null"];
return done();
});
});
it('should save true and delete it', function(done) {
return cache.save('true', true, function() {
return cache.remove('true', function() {
return cache.load('true', function(err, data) {
expect(data).to.be["null"];
return done();
});
});
});
});
return it('should save true to cache from fallback function in load', function(done) {
return cache.load('true', function() {
return true;
}, function(err, data) {
expect(data).to.be["true"];
return done();
});
});
});
return describe('expiration', function() {
it('should expire "true" value after file is changed', function(done) {
return cache.save('true', true, {
files: ['/file']
}, function() {
return setTimeout(function() {
fs.writeFileSync('/file', '');
return cache.load('true', function(err, data) {
expect(data).to.be["null"];
return done();
});
}, 100);
});
});
it('should remove all items with tag "article"', function(done) {
var data;
data = [['one', null, ['article']], ['two', 'two', ['category']], ['three', null, ['article']]];
return async.eachSeries(data, function(item, cb) {
return cache.save(item[0], item[0], {
tags: item[2]
}, function() {
return cb();
});
}, function() {
return cache.clean({
tags: ['article']
}, function() {
return async.eachSeries(data, function(item, cb) {
return cache.load(item[0], function(err, data) {
expect(data).to.be.equal(item[1]);
return cb();
});
}, function() {
return done();
});
});
});
});
it('should expire "true" value after 1 second"', function(done) {
return cache.save('true', true, {
expire: {
seconds: 1
}
}, function() {
return setTimeout(function() {
return cache.load('true', function(err, data) {
expect(data).to.be["null"];
return done();
});
}, 1100);
});
});
it('should expire "true" value after "first" value expire', function(done) {
return cache.save('first', 'first', function() {
return cache.save('true', true, {
items: ['first']
}, function() {
return cache.remove('first', function() {
return cache.load('true', function(err, data) {
expect(data).to.be["null"];
return done();
});
});
});
});
});
it('should expire all items with priority bellow 50', function(done) {
return cache.save('one', 'one', {
priority: 100
}, function() {
return cache.save('two', 'two', {
priority: 10
}, function() {
return cache.clean({
priority: 50
}, function() {
return cache.load('one', function(err, data) {
expect(data).to.be.equal('one');
return cache.load('two', function(err, data) {
expect(data).to.be["null"];
return done();
});
});
});
});
});
});
return it('should remove all items from cache', function(done) {
return cache.save('one', 'one', function() {
return cache.save('two', 'two', function() {
return cache.clean('all', function() {
return cache.load('one', function(err, data) {
expect(data).to.be["null"];
return cache.load('two', function(err, data) {
expect(data).to.be["null"];
return done();
});
});
});
});
});
});
});
});
}).call(this);