mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-03-30 15:08:28 +00:00
Merge pull request #134 from dignifiedquire/blob-store
use pull-blob-store for caching
This commit is contained in:
commit
b53d9697fe
3
.gitignore
vendored
3
.gitignore
vendored
@ -10,3 +10,6 @@ examples/browser/bundle.js
|
||||
examples/browser/*.map
|
||||
dist/*.map
|
||||
orbit-db.json
|
||||
dist/orbitdb.js
|
||||
orbit-db.cache
|
||||
ipfs/
|
@ -21,7 +21,6 @@ module.exports = {
|
||||
path.join(__dirname, '../node_modules')
|
||||
],
|
||||
alias: {
|
||||
'fs': path.join(__dirname, '../node_modules', 'html5-fs'),
|
||||
http: 'stream-http',
|
||||
https: 'https-browserify',
|
||||
Buffer: 'buffer'
|
||||
|
@ -25,7 +25,6 @@ module.exports = {
|
||||
path.join(__dirname, '../node_modules')
|
||||
],
|
||||
alias: {
|
||||
'fs': path.join(__dirname, '../node_modules', 'html5-fs'),
|
||||
http: 'stream-http',
|
||||
https: 'https-browserify',
|
||||
Buffer: 'buffer'
|
||||
|
@ -21,7 +21,7 @@ module.exports = {
|
||||
resolve: {
|
||||
modules: [
|
||||
path.join(__dirname, '../node_modules')
|
||||
],
|
||||
]
|
||||
},
|
||||
module: {
|
||||
loaders: [
|
||||
|
8901
dist/orbitdb.js
vendored
8901
dist/orbitdb.js
vendored
File diff suppressed because it is too large
Load Diff
19
dist/orbitdb.min.js
vendored
19
dist/orbitdb.min.js
vendored
File diff suppressed because one or more lines are too long
28
package.json
28
package.json
@ -12,37 +12,39 @@
|
||||
"node": "^6.x.x"
|
||||
},
|
||||
"browser": {
|
||||
"fs": "html5-fs"
|
||||
"fs-pull-blob-store": "idb-pull-blob-store"
|
||||
},
|
||||
"main": "src/OrbitDB.js",
|
||||
"dependencies": {
|
||||
"logplease": "^1.2.9",
|
||||
"orbit-db-store": "^0.1.9",
|
||||
"fs-pull-blob-store": "^0.4.1",
|
||||
"idb-pull-blob-store": "^0.5.1",
|
||||
"ipfs-log": "^1.5.2",
|
||||
"lock": "^0.1.3",
|
||||
"logplease": "^1.2.9",
|
||||
"orbit-db-counterstore": "0.1.7",
|
||||
"orbit-db-docstore": "0.0.8",
|
||||
"orbit-db-eventstore": "0.1.8",
|
||||
"orbit-db-feedstore": "0.1.7",
|
||||
"orbit-db-kvstore": "0.1.6",
|
||||
"orbit-db-pubsub": "0.0.6",
|
||||
"orbit-db-docstore": "0.0.8"
|
||||
"orbit-db-store": "^0.1.9",
|
||||
"pull-stream": "^3.4.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"asyncawait": "^1.0.6",
|
||||
"babel-core": "^6.11.4",
|
||||
"babel-loader": "^6.2.4",
|
||||
"babel-plugin-transform-runtime": "^6.8.0",
|
||||
"babel-core": "^6.18.0",
|
||||
"babel-loader": "^6.2.7",
|
||||
"babel-plugin-transform-runtime": "^6.15.0",
|
||||
"babel-polyfill": "^6.16.0",
|
||||
"babel-preset-es2015": "^6.6.0",
|
||||
"babel-preset-es2015": "^6.18.0",
|
||||
"bluebird": "^3.4.6",
|
||||
"exports-loader": "^0.6.3",
|
||||
"fs-pull-blob-store": "^0.4.1",
|
||||
"html5-fs": "https://github.com/haadcode/html5-fs.git",
|
||||
"ipfs-daemon": "0.1.1",
|
||||
"ipfs-test-apis": "0.0.6",
|
||||
"json-loader": "^0.5.4",
|
||||
"lodash": "^4.3.0",
|
||||
"mocha": "^3.1.0",
|
||||
"stream-http": "^2.2.1",
|
||||
"lodash": "^4.16.4",
|
||||
"mocha": "^3.1.2",
|
||||
"stream-http": "^2.4.1",
|
||||
"webpack": "^2.1.0-beta.25"
|
||||
},
|
||||
"scripts": {
|
||||
|
82
src/Cache.js
82
src/Cache.js
@ -1,18 +1,32 @@
|
||||
'use strict'
|
||||
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const pull = require('pull-stream')
|
||||
const BlobStore = require('fs-pull-blob-store')
|
||||
const Lock = require('lock')
|
||||
|
||||
let filePath
|
||||
let store
|
||||
let cache = {}
|
||||
const lock = new Lock()
|
||||
|
||||
class Cache {
|
||||
static set(key, value) {
|
||||
return new Promise((resolve, reject) => {
|
||||
cache[key] = value
|
||||
if(filePath) {
|
||||
// console.log("write cache:", filePath, JSON.stringify(cache, null, 2))
|
||||
fs.writeFile(filePath, JSON.stringify(cache, null, 2) + "\n", resolve)
|
||||
if(filePath && store) {
|
||||
lock(filePath, (release) => {
|
||||
// console.log("write cache:", filePath, JSON.stringify(cache, null, 2))
|
||||
pull(
|
||||
pull.values([cache]),
|
||||
pull.map((v) => JSON.stringify(v, null, 2)),
|
||||
store.write(filePath, release((err) => {
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
resolve()
|
||||
}))
|
||||
)
|
||||
})
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
@ -23,48 +37,34 @@ class Cache {
|
||||
return cache[key]
|
||||
}
|
||||
|
||||
static loadCache(cacheFile) {
|
||||
static loadCache(cacheFile = 'orbit-db.cache') {
|
||||
cache = {}
|
||||
store = new BlobStore(cacheFile)
|
||||
filePath = cacheFile
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
// console.log("load cache:", cacheFile)
|
||||
if(cacheFile) {
|
||||
Cache.initFs().then(() => {
|
||||
filePath = cacheFile
|
||||
fs.exists(cacheFile, (res) => {
|
||||
if(res) {
|
||||
fs.readFile(cacheFile, (err, res) => {
|
||||
cache = JSON.parse(res)
|
||||
// console.log("cache:", cache)
|
||||
resolve()
|
||||
})
|
||||
} else {
|
||||
// console.log("cache file doesn't exist")
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
})
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
}
|
||||
store.exists(cacheFile, (err, exists) => {
|
||||
if (err || !exists) {
|
||||
return resolve()
|
||||
}
|
||||
|
||||
static initFs() {
|
||||
const isNodejs = process && process.version ? true : false
|
||||
return new Promise((resolve, reject) => {
|
||||
if(!isNodejs) {
|
||||
fs.init(1 * 1024 * 1024, (err) => {
|
||||
if(err) {
|
||||
console.error("Couldn't initialize file system:", err)
|
||||
} else {
|
||||
// console.debug("FileSystem initialized")
|
||||
}
|
||||
resolve()
|
||||
lock(cacheFile, (release) => {
|
||||
pull(
|
||||
store.read(cacheFile),
|
||||
pull.collect(release((err, res) => {
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
|
||||
cache = JSON.parse(Buffer.concat(res).toString() || '{}')
|
||||
|
||||
resolve()
|
||||
}))
|
||||
)
|
||||
})
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user