forked from Carrooi/Node-CacheStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryStorage.coffee
More file actions
76 lines (60 loc) · 2.21 KB
/
Copy pathMemoryStorage.coffee
File metadata and controls
76 lines (60 loc) · 2.21 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
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', ->
beforeEach( ->
cache = new Cache(new MemoryStorage, 'test')
)
describe 'saving/loading', ->
it 'should save true and load it', ->
cache.save 'true', true
expect(cache.load 'true').to.be.true
it 'should return null if item not exists', ->
expect(cache.load 'true').to.be.null
it 'should save true and delete it', ->
cache.save 'true', true
cache.remove 'true'
expect(cache.load 'true').to.be.null
it 'should save true to cache from fallback function in load', ->
val = cache.load 'true', -> return true
expect(val).to.be.true
describe 'expiration', ->
it 'should expire "true" value after file is changed', ->
cache.save 'true', true, {files: [file]}
fs.writeFileSync(file, '')
expect(cache.load 'true').to.be.null
it 'should remove all items with tag "article"', ->
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'
expect(cache.load 'three').to.be.null
it 'should expire "true" value after 1 second"', (done) ->
cache.save 'true', true, {expire: {seconds: 1}}
setTimeout( ->
expect(cache.load 'true').to.be.null
done()
, 1100)
it 'should expire "true" value after "first" value expire', ->
cache.save 'first', 'first'
cache.save 'true', true, {items: ['first']}
cache.remove 'first'
expect(cache.load 'true').to.be.null
it 'should expire all items with priority bellow 50', ->
cache.save 'one', 'one', {priority: 100}
cache.save 'two', 'two', {priority: 10}
cache.clean {priority: 50}
expect(cache.load 'one').to.be.equal('one')
expect(cache.load 'two').to.be.null
it 'should remove all items from cache', ->
cache.save 'one', 'one'
cache.save 'two', 'two'
cache.clean 'all'
expect(cache.load 'one').to.be.null
expect(cache.load 'two').to.be.null