Skip to content

Commit

Permalink
Merge pull request #1 from ozee31/dev
Browse files Browse the repository at this point in the history
v1.1.0
  • Loading branch information
ozee31 authored Aug 31, 2016
2 parents e462fd9 + 3eea7fe commit a3bd0e7
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 15 deletions.
30 changes: 25 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ OhMyCache.Session.set('key', 'val')
### In html
```html
<html>
<head>
<head>
<!-- npm -->
<script src="node_modules/ohmycache/dist/bundle.js"></script>

<!-- bower -->
<script src="bower_components//ohmycache/dist/bundle.js"></script>

<!-- other method -->
<script src="lib-path/ohmycache/dist/bundle.js"></script>
</head>
<script src="lib-path/ohmycache/dist/bundle.js"></script>
</head>
</html>
```

Expand Down Expand Up @@ -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)`
Expand All @@ -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
Expand Down
72 changes: 67 additions & 5 deletions src/OhMyCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,54 @@ 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
let items = {}

for (let i = 0; i < len; i++) {
let val = this.get(keys[i])

if (val) {
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
}
}

return items
}

/**
* Return all item's keys
* @return {array}
*/
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
Expand Down Expand Up @@ -100,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 @@ -216,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
}
}
92 changes: 89 additions & 3 deletions test/specs/OhMyLocalCache.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,76 @@ 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)
})

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("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')
_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'
Expand Down Expand Up @@ -376,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 @@ -388,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'))
})
})
})
90 changes: 88 additions & 2 deletions test/specs/OhMySessionCache.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,76 @@ 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
sessionStorage.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("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
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()

expect(expected).toEqual(result)
})
})



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'
Expand Down Expand Up @@ -388,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 a3bd0e7

Please sign in to comment.