cache-storage
Version:
[ABANDONED] Advanced cache storage for node js
147 lines (138 loc) • 4.48 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var Cache, FileStorage, cache, expect, fs, path;
expect = require('chai').expect;
path = require('path');
Cache = require('../../../../../lib/Cache');
FileStorage = require('../../../../../Storage/FileSyncStorage');
fs = null;
cache = null;
describe('FileSyncStorage', function() {
beforeEach(function() {
return fs = Cache.mockFs({
'temp': {},
'file': ''
});
});
afterEach(function() {
return Cache.restoreFs();
});
describe('#constructor()', function() {
it('should throw an error if path does not exists', function() {
return expect(function() {
return new FileStorage('./unknown/path');
}).to["throw"]();
});
return it('should throw an error if path is not directory', function() {
return expect(function() {
return new FileStorage('/file');
}).to["throw"]();
});
});
describe('saving/loading', function() {
beforeEach(function() {
return cache = new Cache(new FileStorage('/temp'), 'test');
});
afterEach(function() {
if (fs.existsSync('/temp/__test.json')) {
return fs.unlinkSync('/temp/__test.json');
}
});
it('should save true and load it', function() {
cache.save('true', true);
return expect(cache.load('true')).to.be["true"];
});
it('should return null if item not exists', function() {
return expect(cache.load('true')).to.be["null"];
});
it('should save true and delete it', function() {
cache.save('true', true);
cache.remove('true');
return expect(cache.load('true')).to.be["null"];
});
return it('should save true to cache from fallback function in load', function() {
var val;
val = cache.load('true', function() {
return true;
});
return expect(val).to.be["true"];
});
});
return describe('expiration', function() {
beforeEach(function() {
return cache = new Cache(new FileStorage('/temp'), 'test');
});
afterEach(function() {
if (fs.existsSync('/temp/__test.json')) {
return fs.unlinkSync('/temp/__test.json');
}
});
it('should expire "true" value after file is changed', function(done) {
cache.save('true', true, {
files: ['/file']
});
return setTimeout(function() {
fs.writeFileSync('/file', '');
expect(cache.load('true')).to.be["null"];
return done();
}, 100);
});
it('should remove all items with tag "article"', function() {
cache.save('one', 'one', {
tags: ['article']
});
cache.save('two', 'two', {
tags: ['category']
});
cache.save('three', 'three', {
tags: ['article']
});
cache.clean({
tags: ['article']
});
expect(cache.load('one')).to.be["null"];
expect(cache.load('two')).to.be.equal('two');
return expect(cache.load('three')).to.be["null"];
});
it('should expire "true" value after 1 second"', function(done) {
cache.save('true', true, {
expire: {
seconds: 1
}
});
return setTimeout(function() {
expect(cache.load('true')).to.be["null"];
return done();
}, 1100);
});
it('should expire "true" value after "first" value expire', function() {
cache.save('first', 'first');
cache.save('true', true, {
items: ['first']
});
cache.remove('first');
return expect(cache.load('true')).to.be["null"];
});
it('should expire all items with priority bellow 50', function() {
cache.save('one', 'one', {
priority: 100
});
cache.save('two', 'two', {
priority: 10
});
cache.clean({
priority: 50
});
expect(cache.load('one')).to.be.equal('one');
return expect(cache.load('two')).to.be["null"];
});
return it('should remove all items from cache', function() {
cache.save('one', 'one');
cache.save('two', 'two');
cache.clean('all');
expect(cache.load('one')).to.be["null"];
return expect(cache.load('two')).to.be["null"];
});
});
});
}).call(this);