Skip to content

Commit

Permalink
add clear option (remove only expired items)
Browse files Browse the repository at this point in the history
  • Loading branch information
ozee31 committed Aug 31, 2016
1 parent 72d87b4 commit 958b3e3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
24 changes: 19 additions & 5 deletions src/OhMyCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,15 @@ export default class OhMyCache {

/**
* Remove all items
* @param {string} onlyExpired: if true remove only expired datas else remove all items
* @return {boolean}
*/
clear () {
try {
this.engine.clear()
clear (onlyExpired = false) {
if (onlyExpired) {
this.getAllItems()
return true
} catch (e) {
return false
} else {
return _clear(this.engine)
}
}

Expand Down Expand Up @@ -264,3 +265,16 @@ function _remove (engine, key) {
return false
}
}

/**
* Remove all items
* @return {boolean}
*/
function _clear (engine) {
try {
engine.clear()
return true
} catch (e) {
return false
}
}
22 changes: 19 additions & 3 deletions test/specs/OhMyLocalCache.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ describe('OhMyLocalCache', () => {


describe("clear()", function() {
it('clear', function() {
it('clear all', function() {
_class.set('k1', 'v1', {readonly: true})
_class.set('k2', 'v2')
sessionStorage.setItem('s1', 's2')
Expand All @@ -458,6 +458,22 @@ describe('OhMyLocalCache', () => {
expect(null).toBe(_class.get('k1'))
expect(null).toBe(_class.get('k2'))
expect('s2').toEqual(sessionStorage.getItem('s1'))
});
});
})

it('clear only expired', function() {
_class.set('k1', 'v1', {readonly: true})
_class.set('k2', 'v2')
let created = 1451602800 // 2016-01-01
let expired = created + 3600
localStorage.setItem('k3', JSON.stringify({0:'v3', 1:created, 2:expired}))
sessionStorage.setItem('s1', 's2')

_class.clear(true);

expect('v1').toBe(_class.get('k1'))
expect('v2').toBe(_class.get('k2'))
expect(null).toBe(_class.get('k3'))
expect('s2').toEqual(sessionStorage.getItem('s1'))
})
})
})
24 changes: 20 additions & 4 deletions test/specs/OhMySessionCache.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ describe('OhMySessionCache', () => {

let created = 1451602800 // 2016-01-01
let expired = created + 3600
localStorage.setItem('key3', JSON.stringify({0:'val3', 1:created, 2:expired}))
sessionStorage.setItem('key3', JSON.stringify({0:'val3', 1:created, 2:expired}))

let expected = {key2: 'val2', key1: 'val1'}
let result = _class.getAll()
Expand Down Expand Up @@ -331,7 +331,7 @@ describe('OhMySessionCache', () => {

let created = 1451602800 // 2016-01-01
let expired = created + 3600
localStorage.setItem('key3', JSON.stringify({0:'val3', 1:created, 2:expired}))
sessionStorage.setItem('key3', JSON.stringify({0:'val3', 1:created, 2:expired}))

let expected = {key2: {0: 'val2', 1: time()}, key1: {0: 'val1', 1: time()}}
let result = _class.getAllItems()
Expand Down Expand Up @@ -458,6 +458,22 @@ describe('OhMySessionCache', () => {
expect(null).toBe(_class.get('k1'))
expect(null).toBe(_class.get('k2'))
expect('s2').toEqual(localStorage.getItem('s1'))
});
});
})

it('clear only expired', function() {
_class.set('k1', 'v1', {readonly: true})
_class.set('k2', 'v2')
let created = 1451602800 // 2016-01-01
let expired = created + 3600
sessionStorage.setItem('k3', JSON.stringify({0:'v3', 1:created, 2:expired}))
localStorage.setItem('s1', 's2')

_class.clear(true);

expect('v1').toBe(_class.get('k1'))
expect('v2').toBe(_class.get('k2'))
expect(null).toBe(_class.get('k3'))
expect('s2').toEqual(localStorage.getItem('s1'))
})
})
})

0 comments on commit 958b3e3

Please sign in to comment.