-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1.0] Replace using levelup for caching with lowdb to avoid native de…
…pendency (#1142) * Replace leveldb with lowdb to avoid native dependency * Add missing slash
- Loading branch information
1 parent
acbc22e
commit d182f94
Showing
5 changed files
with
47 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const cache = require(`../cache`) | ||
cache.initCache() | ||
|
||
describe(`site cache`, () => { | ||
it(`can set and get cache items`, async () => { | ||
await cache.set(`a key`, `value`) | ||
await cache.set(`a boolean key`, true) | ||
const value = await cache.get(`a key`) | ||
const value2 = await cache.get(`a boolean key`) | ||
expect(value).toBe(`value`) | ||
expect(value2).toBe(true) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,46 @@ | ||
let levelup | ||
if (process.env.NODE_ENV !== `test`) { | ||
levelup = require(`level`) | ||
} | ||
const Promise = require(`bluebird`) | ||
const low = require(`lowdb`) | ||
const fs = require(`fs-extra`) | ||
|
||
let db | ||
exports.initCache = () => { | ||
fs.ensureDirSync(`${process.cwd()}/.cache/cache`) | ||
let directory | ||
if (process.env.NODE_ENV === `test`) { | ||
db = { | ||
get: () => false, | ||
set: () => false, | ||
} | ||
directory = require(`os`).tmpdir() | ||
} else { | ||
db = levelup(`${process.cwd()}/.cache/cache`, { | ||
keyEncoding: `json`, | ||
valueEncoding: `json`, | ||
}) | ||
directory = process.cwd() + `/.cache/cache` | ||
} | ||
db = low(`${directory}/site-cache.json`, { | ||
storage: require("lowdb/lib/storages/file-async"), | ||
format: { | ||
serialize: obj => JSON.stringify(obj), | ||
deserialize: str => JSON.parse(str), | ||
}, | ||
}) | ||
db._.mixin(require("lodash-id")) | ||
|
||
db.defaults({ keys: [] }).write() | ||
} | ||
|
||
exports.get = key => | ||
new Promise((resolve, reject) => { | ||
db.get(key, (err, value) => { | ||
if (err && !err.notFound) { | ||
reject(err) | ||
} else { | ||
resolve(value) | ||
} | ||
}) | ||
let pair | ||
try { | ||
pair = db.get(`keys`).getById(key).value() | ||
} catch (e) { | ||
// ignore | ||
} | ||
|
||
if (pair) { | ||
resolve(pair.value) | ||
} else { | ||
resolve() | ||
} | ||
}) | ||
|
||
exports.set = (key, value) => | ||
new Promise((resolve, reject) => { | ||
db.put(key, value, err => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
resolve(`OK`) | ||
} | ||
}) | ||
db.get(`keys`).upsert({ id: key, value }).write() | ||
resolve(`Ok`) | ||
}) |