make it actually work
This commit is contained in:
parent
5062212494
commit
1b845fd74d
1
.gitignore
vendored
1
.gitignore
vendored
@ -12,3 +12,4 @@ dist/*.map
|
|||||||
orbit-db.json
|
orbit-db.json
|
||||||
dist/orbitdb.js
|
dist/orbitdb.js
|
||||||
orbit-db.cache
|
orbit-db.cache
|
||||||
|
ipfs/
|
@ -19,6 +19,7 @@
|
|||||||
"fs-pull-blob-store": "^0.4.1",
|
"fs-pull-blob-store": "^0.4.1",
|
||||||
"idb-pull-blob-store": "^0.5.1",
|
"idb-pull-blob-store": "^0.5.1",
|
||||||
"ipfs-log": "^1.5.2",
|
"ipfs-log": "^1.5.2",
|
||||||
|
"lock": "^0.1.3",
|
||||||
"logplease": "^1.2.9",
|
"logplease": "^1.2.9",
|
||||||
"orbit-db-counterstore": "0.1.7",
|
"orbit-db-counterstore": "0.1.7",
|
||||||
"orbit-db-docstore": "0.0.8",
|
"orbit-db-docstore": "0.0.8",
|
||||||
|
51
src/Cache.js
51
src/Cache.js
@ -2,27 +2,31 @@
|
|||||||
|
|
||||||
const pull = require('pull-stream')
|
const pull = require('pull-stream')
|
||||||
const BlobStore = require('fs-pull-blob-store')
|
const BlobStore = require('fs-pull-blob-store')
|
||||||
|
const Lock = require('lock')
|
||||||
|
|
||||||
let filePath
|
let filePath
|
||||||
let store
|
let store
|
||||||
let cache = {}
|
let cache = {}
|
||||||
|
const lock = new Lock()
|
||||||
|
|
||||||
class Cache {
|
class Cache {
|
||||||
static set(key, value) {
|
static set(key, value) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
cache[key] = value
|
cache[key] = value
|
||||||
if(filePath && store) {
|
if(filePath && store) {
|
||||||
// console.log("write cache:", filePath, JSON.stringify(cache, null, 2))
|
lock(filePath, (release) => {
|
||||||
pull(
|
// console.log("write cache:", filePath, JSON.stringify(cache, null, 2))
|
||||||
pull.values([cache]),
|
pull(
|
||||||
pull.map((v) => JSON.stringify(v, null, 2)),
|
pull.values([cache]),
|
||||||
store.write(filePath, (err) => {
|
pull.map((v) => JSON.stringify(v, null, 2)),
|
||||||
if (err) {
|
store.write(filePath, release((err) => {
|
||||||
return reject(err)
|
if (err) {
|
||||||
}
|
return reject(err)
|
||||||
resolve()
|
}
|
||||||
})
|
resolve()
|
||||||
)
|
}))
|
||||||
|
)
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
resolve()
|
resolve()
|
||||||
}
|
}
|
||||||
@ -36,6 +40,8 @@ class Cache {
|
|||||||
static loadCache(cacheFile = 'orbit-db.cache') {
|
static loadCache(cacheFile = 'orbit-db.cache') {
|
||||||
cache = {}
|
cache = {}
|
||||||
store = new BlobStore(cacheFile)
|
store = new BlobStore(cacheFile)
|
||||||
|
filePath = cacheFile
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
// console.log("load cache:", cacheFile)
|
// console.log("load cache:", cacheFile)
|
||||||
@ -44,17 +50,20 @@ class Cache {
|
|||||||
return resolve()
|
return resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
filePath = cacheFile
|
lock(cacheFile, (release) => {
|
||||||
pull(
|
pull(
|
||||||
store.read(cacheFile),
|
store.read(cacheFile),
|
||||||
pull.collect((err, res) => {
|
pull.collect(release((err, res) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return reject(err)
|
return reject(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve(JSON.parse(res[0].toString() || '{}'))
|
cache = JSON.parse(Buffer.concat(res).toString() || '{}')
|
||||||
})
|
|
||||||
)
|
resolve()
|
||||||
|
}))
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user