mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-10-07 22:57:07 +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
|
examples/browser/*.map
|
||||||
dist/*.map
|
dist/*.map
|
||||||
orbit-db.json
|
orbit-db.json
|
||||||
|
dist/orbitdb.js
|
||||||
|
orbit-db.cache
|
||||||
|
ipfs/
|
@ -21,7 +21,6 @@ module.exports = {
|
|||||||
path.join(__dirname, '../node_modules')
|
path.join(__dirname, '../node_modules')
|
||||||
],
|
],
|
||||||
alias: {
|
alias: {
|
||||||
'fs': path.join(__dirname, '../node_modules', 'html5-fs'),
|
|
||||||
http: 'stream-http',
|
http: 'stream-http',
|
||||||
https: 'https-browserify',
|
https: 'https-browserify',
|
||||||
Buffer: 'buffer'
|
Buffer: 'buffer'
|
||||||
|
@ -25,7 +25,6 @@ module.exports = {
|
|||||||
path.join(__dirname, '../node_modules')
|
path.join(__dirname, '../node_modules')
|
||||||
],
|
],
|
||||||
alias: {
|
alias: {
|
||||||
'fs': path.join(__dirname, '../node_modules', 'html5-fs'),
|
|
||||||
http: 'stream-http',
|
http: 'stream-http',
|
||||||
https: 'https-browserify',
|
https: 'https-browserify',
|
||||||
Buffer: 'buffer'
|
Buffer: 'buffer'
|
||||||
|
@ -21,7 +21,7 @@ module.exports = {
|
|||||||
resolve: {
|
resolve: {
|
||||||
modules: [
|
modules: [
|
||||||
path.join(__dirname, '../node_modules')
|
path.join(__dirname, '../node_modules')
|
||||||
],
|
]
|
||||||
},
|
},
|
||||||
module: {
|
module: {
|
||||||
loaders: [
|
loaders: [
|
||||||
|
8891
dist/orbitdb.js
vendored
8891
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"
|
"node": "^6.x.x"
|
||||||
},
|
},
|
||||||
"browser": {
|
"browser": {
|
||||||
"fs": "html5-fs"
|
"fs-pull-blob-store": "idb-pull-blob-store"
|
||||||
},
|
},
|
||||||
"main": "src/OrbitDB.js",
|
"main": "src/OrbitDB.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"logplease": "^1.2.9",
|
"fs-pull-blob-store": "^0.4.1",
|
||||||
"orbit-db-store": "^0.1.9",
|
"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",
|
||||||
"orbit-db-counterstore": "0.1.7",
|
"orbit-db-counterstore": "0.1.7",
|
||||||
|
"orbit-db-docstore": "0.0.8",
|
||||||
"orbit-db-eventstore": "0.1.8",
|
"orbit-db-eventstore": "0.1.8",
|
||||||
"orbit-db-feedstore": "0.1.7",
|
"orbit-db-feedstore": "0.1.7",
|
||||||
"orbit-db-kvstore": "0.1.6",
|
"orbit-db-kvstore": "0.1.6",
|
||||||
"orbit-db-pubsub": "0.0.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": {
|
"devDependencies": {
|
||||||
"asyncawait": "^1.0.6",
|
"asyncawait": "^1.0.6",
|
||||||
"babel-core": "^6.11.4",
|
"babel-core": "^6.18.0",
|
||||||
"babel-loader": "^6.2.4",
|
"babel-loader": "^6.2.7",
|
||||||
"babel-plugin-transform-runtime": "^6.8.0",
|
"babel-plugin-transform-runtime": "^6.15.0",
|
||||||
"babel-polyfill": "^6.16.0",
|
"babel-polyfill": "^6.16.0",
|
||||||
"babel-preset-es2015": "^6.6.0",
|
"babel-preset-es2015": "^6.18.0",
|
||||||
"bluebird": "^3.4.6",
|
"bluebird": "^3.4.6",
|
||||||
"exports-loader": "^0.6.3",
|
"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-daemon": "0.1.1",
|
||||||
"ipfs-test-apis": "0.0.6",
|
"ipfs-test-apis": "0.0.6",
|
||||||
"json-loader": "^0.5.4",
|
"json-loader": "^0.5.4",
|
||||||
"lodash": "^4.3.0",
|
"lodash": "^4.16.4",
|
||||||
"mocha": "^3.1.0",
|
"mocha": "^3.1.2",
|
||||||
"stream-http": "^2.2.1",
|
"stream-http": "^2.4.1",
|
||||||
"webpack": "^2.1.0-beta.25"
|
"webpack": "^2.1.0-beta.25"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
74
src/Cache.js
74
src/Cache.js
@ -1,18 +1,32 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const fs = require('fs')
|
const pull = require('pull-stream')
|
||||||
const path = require('path')
|
const BlobStore = require('fs-pull-blob-store')
|
||||||
|
const Lock = require('lock')
|
||||||
|
|
||||||
let filePath
|
let filePath
|
||||||
|
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) {
|
if(filePath && store) {
|
||||||
|
lock(filePath, (release) => {
|
||||||
// console.log("write cache:", filePath, JSON.stringify(cache, null, 2))
|
// console.log("write cache:", filePath, JSON.stringify(cache, null, 2))
|
||||||
fs.writeFile(filePath, JSON.stringify(cache, null, 2) + "\n", resolve)
|
pull(
|
||||||
|
pull.values([cache]),
|
||||||
|
pull.map((v) => JSON.stringify(v, null, 2)),
|
||||||
|
store.write(filePath, release((err) => {
|
||||||
|
if (err) {
|
||||||
|
return reject(err)
|
||||||
|
}
|
||||||
|
resolve()
|
||||||
|
}))
|
||||||
|
)
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
resolve()
|
resolve()
|
||||||
}
|
}
|
||||||
@ -23,48 +37,34 @@ class Cache {
|
|||||||
return cache[key]
|
return cache[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
static loadCache(cacheFile) {
|
static loadCache(cacheFile = 'orbit-db.cache') {
|
||||||
cache = {}
|
cache = {}
|
||||||
|
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)
|
||||||
if(cacheFile) {
|
store.exists(cacheFile, (err, exists) => {
|
||||||
Cache.initFs().then(() => {
|
if (err || !exists) {
|
||||||
filePath = cacheFile
|
return resolve()
|
||||||
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()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static initFs() {
|
lock(cacheFile, (release) => {
|
||||||
const isNodejs = process && process.version ? true : false
|
pull(
|
||||||
return new Promise((resolve, reject) => {
|
store.read(cacheFile),
|
||||||
if(!isNodejs) {
|
pull.collect(release((err, res) => {
|
||||||
fs.init(1 * 1024 * 1024, (err) => {
|
if (err) {
|
||||||
if(err) {
|
return reject(err)
|
||||||
console.error("Couldn't initialize file system:", err)
|
|
||||||
} else {
|
|
||||||
// console.debug("FileSystem initialized")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cache = JSON.parse(Buffer.concat(res).toString() || '{}')
|
||||||
|
|
||||||
resolve()
|
resolve()
|
||||||
|
}))
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
} else {
|
|
||||||
resolve()
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user