cache-storage
Version:
[ABANDONED] Advanced cache storage for node js
120 lines (111 loc) • 3.58 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var Cache, MemoryStorage, cache, expect, fs, path;
expect = require('chai').expect;
path = require('path');
fs = null;
Cache = require('../../../../../lib/Cache');
MemoryStorage = require('../../../../../Storage/MemorySyncStorage');
cache = null;
describe('MemorySyncStorage', function() {
beforeEach(function() {
fs = Cache.mockFs({
'temp': {},
'file': ''
});
return cache = new Cache(new MemoryStorage, 'test');
});
afterEach(function() {
return Cache.restoreFs();
});
describe('saving/loading', function() {
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() {
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);