Skip to content

Commit

Permalink
better usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
lagden committed Dec 29, 2018
1 parent e857623 commit eb9f7aa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,35 +71,35 @@ const cache = new Cache({}, '127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381')

const Cache = require('@tadashi/cache-redis')

const cache = new Cache({
const _cache = new Cache({
redis: {
keyPrefix: 'chatuser'
keyPrefix: 'example'
},
namespace: 'api'
})

async function find(key) {
try {
const fromCache = await cache.get(key)
if (fromCache) {
return fromCache
const cache = await _cache.get(key)
if (cache) {
return cache
}
const result = await getDataFromSomeWhere(key)
await cache.set(key, result, 3600, 'PX')
await _cache.set(key, result, 3600, 'PX')
return result
} catch (err) {
throw err
}
}

find('lagden').then(console.log)
// => getDataFromSomeWhere
find('something').then(console.log)
// => data from getDataFromSomeWhere

find('lagden').then(console.log)
// => fromCache
find('something').then(console.log)
// => data from cache
```


## License

MIT © [Thiago Lagden](http://lagden.in)
MIT © [Thiago Lagden](https://lagden.in)
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"scripts": {
"report": "nyc report --reporter=text-lcov | coveralls",
"pretest": "xo",
"test": "ava"
"test": "ava -t"
},
"repository": {
"type": "git",
Expand All @@ -20,7 +20,7 @@
"keywords": [
"cache",
"redis",
"json"
"lib"
],
"engines": {
"node": ">=8.9.1"
Expand Down

0 comments on commit eb9f7aa

Please sign in to comment.