From 92489d413403ac0cd5f8d4574ef102ab8875144c Mon Sep 17 00:00:00 2001 From: Ozee31 Date: Tue, 30 Aug 2016 17:21:22 +0200 Subject: [PATCH 1/6] add keys fonction --- src/OhMyCache.js | 7 +++++++ test/specs/OhMyLocalCache.spec.js | 14 ++++++++++++++ test/specs/OhMySessionCache.spec.js | 14 ++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/src/OhMyCache.js b/src/OhMyCache.js index ad7d7cc..270747a 100644 --- a/src/OhMyCache.js +++ b/src/OhMyCache.js @@ -38,6 +38,13 @@ export default class OhMyCache { return (item) ? item[VALUE] : null } + /** + * Return all item's keys + */ + keys () { + return Object.keys(this.getEngine()) + } + /** * Add the key to the storage, or update that key's value if it already exists * @param {string} key diff --git a/test/specs/OhMyLocalCache.spec.js b/test/specs/OhMyLocalCache.spec.js index 726d5fb..894a002 100644 --- a/test/specs/OhMyLocalCache.spec.js +++ b/test/specs/OhMyLocalCache.spec.js @@ -286,6 +286,20 @@ describe('OhMyLocalCache', () => { + describe("keys()", function() { + it('return all items key', function() { + _class.set('key1', 'val1') + _class.set('key2', 'val2') + + let expected = ['key2', 'key1'] + let result = _class.keys() + + expect(expected).toEqual(result) + }) + }) + + + describe("remove()", function() { it('remove', function() { let key = 'hello' diff --git a/test/specs/OhMySessionCache.spec.js b/test/specs/OhMySessionCache.spec.js index 14ae38d..b66893e 100644 --- a/test/specs/OhMySessionCache.spec.js +++ b/test/specs/OhMySessionCache.spec.js @@ -286,6 +286,20 @@ describe('OhMySessionCache', () => { + describe("keys()", function() { + it('return all items key', function() { + _class.set('key1', 'val1') + _class.set('key2', 'val2') + + let expected = ['key2', 'key1'] + let result = _class.keys() + + expect(expected).toEqual(result) + }) + }) + + + describe("remove()", function() { it('remove', function() { let key = 'hello' From 92d6455fef923292e65cb4bb5f41b4a2e1379721 Mon Sep 17 00:00:00 2001 From: Ozee31 Date: Tue, 30 Aug 2016 17:57:29 +0200 Subject: [PATCH 2/6] starting getAll function --- src/OhMyCache.js | 12 ++++++++++++ test/specs/OhMyLocalCache.spec.js | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/OhMyCache.js b/src/OhMyCache.js index 270747a..63fe7e4 100644 --- a/src/OhMyCache.js +++ b/src/OhMyCache.js @@ -38,6 +38,18 @@ export default class OhMyCache { return (item) ? item[VALUE] : null } + getAll () { + let keys = this.keys() + let len = keys.length + let items = {} + + for (let i = 0; i < len; i++) { + items[keys[i]] = this.get(keys[i]) + } + + return items + } + /** * Return all item's keys */ diff --git a/test/specs/OhMyLocalCache.spec.js b/test/specs/OhMyLocalCache.spec.js index 894a002..6cefca7 100644 --- a/test/specs/OhMyLocalCache.spec.js +++ b/test/specs/OhMyLocalCache.spec.js @@ -286,6 +286,20 @@ describe('OhMyLocalCache', () => { + describe("getAll()", function() { + it('return all items key', function() { + _class.set('key1', 'val1') + _class.set('key2', 'val2') + + let expected = {key2: 'val2', key1: 'val1'} + let result = _class.getAll() + + expect(expected).toEqual(result) + }) + }) + + + describe("keys()", function() { it('return all items key', function() { _class.set('key1', 'val1') From 0077122c9b39a7738794e9ef04eff35398327929 Mon Sep 17 00:00:00 2001 From: Ozee31 Date: Wed, 31 Aug 2016 09:01:52 +0200 Subject: [PATCH 3/6] add getAll function --- src/OhMyCache.js | 6 +++++- test/specs/OhMyLocalCache.spec.js | 14 ++++++++++++++ test/specs/OhMySessionCache.spec.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/OhMyCache.js b/src/OhMyCache.js index 63fe7e4..1f5de62 100644 --- a/src/OhMyCache.js +++ b/src/OhMyCache.js @@ -44,7 +44,11 @@ export default class OhMyCache { let items = {} for (let i = 0; i < len; i++) { - items[keys[i]] = this.get(keys[i]) + let val = this.get(keys[i]) + + if (val) { + items[keys[i]] = this.get(keys[i]) + } } return items diff --git a/test/specs/OhMyLocalCache.spec.js b/test/specs/OhMyLocalCache.spec.js index 6cefca7..b5edebe 100644 --- a/test/specs/OhMyLocalCache.spec.js +++ b/test/specs/OhMyLocalCache.spec.js @@ -296,6 +296,20 @@ describe('OhMyLocalCache', () => { expect(expected).toEqual(result) }) + + it('no return expired item', function() { + _class.set('key1', 'val1') + _class.set('key2', 'val2') + + let created = 1451602800 // 2016-01-01 + let expired = created + 3600 + localStorage.setItem('key3', JSON.stringify({0:'val3', 1:created, 2:expired})) + + let expected = {key2: 'val2', key1: 'val1'} + let result = _class.getAll() + + expect(expected).toEqual(result) + }) }) diff --git a/test/specs/OhMySessionCache.spec.js b/test/specs/OhMySessionCache.spec.js index b66893e..6c7bde8 100644 --- a/test/specs/OhMySessionCache.spec.js +++ b/test/specs/OhMySessionCache.spec.js @@ -286,6 +286,34 @@ describe('OhMySessionCache', () => { + describe("getAll()", function() { + it('return all items key', function() { + _class.set('key1', 'val1') + _class.set('key2', 'val2') + + let expected = {key2: 'val2', key1: 'val1'} + let result = _class.getAll() + + expect(expected).toEqual(result) + }) + + it('no return expired item', function() { + _class.set('key1', 'val1') + _class.set('key2', 'val2') + + let created = 1451602800 // 2016-01-01 + let expired = created + 3600 + localStorage.setItem('key3', JSON.stringify({0:'val3', 1:created, 2:expired})) + + let expected = {key2: 'val2', key1: 'val1'} + let result = _class.getAll() + + expect(expected).toEqual(result) + }) + }) + + + describe("keys()", function() { it('return all items key', function() { _class.set('key1', 'val1') From 72d87b4f0298fc6264aa53d6c15fc3d12169d3ac Mon Sep 17 00:00:00 2001 From: Ozee31 Date: Wed, 31 Aug 2016 09:23:07 +0200 Subject: [PATCH 4/6] add getAllItems function --- src/OhMyCache.js | 27 ++++++++++++++++++++++++++- test/specs/OhMyLocalCache.spec.js | 28 ++++++++++++++++++++++++++++ test/specs/OhMySessionCache.spec.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/src/OhMyCache.js b/src/OhMyCache.js index 1f5de62..c92846b 100644 --- a/src/OhMyCache.js +++ b/src/OhMyCache.js @@ -38,6 +38,10 @@ export default class OhMyCache { return (item) ? item[VALUE] : null } + /** + * Get all values, remove if expired + * @return {object} + */ getAll () { let keys = this.keys() let len = keys.length @@ -47,7 +51,27 @@ export default class OhMyCache { let val = this.get(keys[i]) if (val) { - items[keys[i]] = this.get(keys[i]) + items[keys[i]] = val + } + } + + return items + } + + /** + * Get all complete items by key with value, date, expiration and readonly options, remove if expired + * @return {object} + */ + getAllItems () { + let keys = this.keys() + let len = keys.length + let items = {} + + for (let i = 0; i < len; i++) { + let item = this.getItem(keys[i]) + + if (item) { + items[keys[i]] = item } } @@ -56,6 +80,7 @@ export default class OhMyCache { /** * Return all item's keys + * @return {array} */ keys () { return Object.keys(this.getEngine()) diff --git a/test/specs/OhMyLocalCache.spec.js b/test/specs/OhMyLocalCache.spec.js index b5edebe..660dfd3 100644 --- a/test/specs/OhMyLocalCache.spec.js +++ b/test/specs/OhMyLocalCache.spec.js @@ -314,6 +314,34 @@ describe('OhMyLocalCache', () => { + describe("getAllItems()", function() { + it('return all items key', function() { + _class.set('key1', 'val1') + _class.set('key2', 'val2') + + let expected = {key2: {0: 'val2', 1: time()}, key1: {0: 'val1', 1: time()}} + let result = _class.getAllItems() + + expect(expected).toEqual(result) + }) + + it('no return expired item', function() { + _class.set('key1', 'val1') + _class.set('key2', 'val2') + + let created = 1451602800 // 2016-01-01 + let expired = created + 3600 + localStorage.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() + + expect(expected).toEqual(result) + }) + }) + + + describe("keys()", function() { it('return all items key', function() { _class.set('key1', 'val1') diff --git a/test/specs/OhMySessionCache.spec.js b/test/specs/OhMySessionCache.spec.js index 6c7bde8..765aff9 100644 --- a/test/specs/OhMySessionCache.spec.js +++ b/test/specs/OhMySessionCache.spec.js @@ -314,6 +314,34 @@ describe('OhMySessionCache', () => { + describe("getAllItems()", function() { + it('return all items key', function() { + _class.set('key1', 'val1') + _class.set('key2', 'val2') + + let expected = {key2: {0: 'val2', 1: time()}, key1: {0: 'val1', 1: time()}} + let result = _class.getAllItems() + + expect(expected).toEqual(result) + }) + + it('no return expired item', function() { + _class.set('key1', 'val1') + _class.set('key2', 'val2') + + let created = 1451602800 // 2016-01-01 + let expired = created + 3600 + localStorage.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() + + expect(expected).toEqual(result) + }) + }) + + + describe("keys()", function() { it('return all items key', function() { _class.set('key1', 'val1') From 958b3e39c67aa738e7ee7b83a7b4de22bddb8791 Mon Sep 17 00:00:00 2001 From: Ozee31 Date: Wed, 31 Aug 2016 09:42:53 +0200 Subject: [PATCH 5/6] add clear option (remove only expired items) --- src/OhMyCache.js | 24 +++++++++++++++++++----- test/specs/OhMyLocalCache.spec.js | 22 +++++++++++++++++++--- test/specs/OhMySessionCache.spec.js | 24 ++++++++++++++++++++---- 3 files changed, 58 insertions(+), 12 deletions(-) diff --git a/src/OhMyCache.js b/src/OhMyCache.js index c92846b..fecb4d0 100644 --- a/src/OhMyCache.js +++ b/src/OhMyCache.js @@ -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) } } @@ -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 + } +} diff --git a/test/specs/OhMyLocalCache.spec.js b/test/specs/OhMyLocalCache.spec.js index 660dfd3..a6ab334 100644 --- a/test/specs/OhMyLocalCache.spec.js +++ b/test/specs/OhMyLocalCache.spec.js @@ -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') @@ -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')) + }) + }) }) \ No newline at end of file diff --git a/test/specs/OhMySessionCache.spec.js b/test/specs/OhMySessionCache.spec.js index 765aff9..2e17f0f 100644 --- a/test/specs/OhMySessionCache.spec.js +++ b/test/specs/OhMySessionCache.spec.js @@ -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() @@ -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() @@ -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')) + }) + }) }) \ No newline at end of file From 3eea7fe496ea66fa6d64943606c0f7aa0bd982a5 Mon Sep 17 00:00:00 2001 From: Ozee31 Date: Wed, 31 Aug 2016 10:03:47 +0200 Subject: [PATCH 6/6] update readme --- README.md | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5bb4340..7b6df09 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ OhMyCache.Session.set('key', 'val') ### In html ```html - + @@ -61,8 +61,8 @@ OhMyCache.Session.set('key', 'val') - - + + ``` @@ -156,6 +156,23 @@ OhMyCache.Local.set('key', 'value', {expire: 1}) OhMyCache.Local.get('key') // null ``` +### GetAll +Get all items, remove all expired items +`getAll()` + +#### Return +**Object** : all items {key: value, key2: value2...} + +#### Exemples +```JS +OhMyCache.Local.set('k1', 'v1') +OhMyCache.Local.set('k2', 'v2', {readonly: true}) +OhMyCache.Local.set('k3', 'v3', {expire: 1}) + +// sleep 2 secondes or more +OhMyCache.Local.getAll() // {k1: 'v1', k2: 'v2'} +``` + ### Remove Remove item if don't readonly `remove(key)` @@ -182,10 +199,13 @@ OhMyCache.Local.get('key') // 'value' ### Clear Remove all items -`clear()` +`clear(onlyExpired)` + +#### Parameters +- {boolean} **onlyExpired** : if true remove only expired items else remove all items, default false #### Return -true +**Boolean** : success #### Exemples ```JS