forked from Carrooi/Node-CacheStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryStorage.js
More file actions
111 lines (102 loc) · 3.32 KB
/
Copy pathMemoryStorage.js
File metadata and controls
111 lines (102 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Generated by CoffeeScript 1.6.3
(function() {
var Cache, MemoryStorage, cache, expect, file, fs, path;
expect = require('chai').expect;
path = require('path');
fs = require('fs');
Cache = require('../../lib/Cache');
MemoryStorage = require('../../lib/Storage/MemoryStorage');
file = path.normalize(__dirname + '/../data/file');
cache = null;
describe('MemoryStorage', function() {
beforeEach(function() {
return cache = new Cache(new MemoryStorage, 'test');
});
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() {
cache.save('true', true, {
files: [file]
});
fs.writeFileSync(file, '');
return expect(cache.load('true')).to.be["null"];
});
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);