mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-03-30 15:08:28 +00:00
Add Cache to OrbitDB
This commit is contained in:
parent
d6af22ce71
commit
fcd0acaecd
@ -14,10 +14,10 @@
|
|||||||
"main": "src/OrbitDB.js",
|
"main": "src/OrbitDB.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"logplease": "^1.2.7",
|
"logplease": "^1.2.7",
|
||||||
"orbit-db-counterstore": "0.1.0",
|
"orbit-db-counterstore": "0.1.1",
|
||||||
"orbit-db-eventstore": "0.1.0",
|
"orbit-db-eventstore": "0.1.1",
|
||||||
"orbit-db-feedstore": "0.1.0",
|
"orbit-db-feedstore": "0.1.1",
|
||||||
"orbit-db-kvstore": "0.1.0",
|
"orbit-db-kvstore": "0.1.1",
|
||||||
"socket.io-client": "^1.4.5"
|
"socket.io-client": "^1.4.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
55
src/Cache.js
Normal file
55
src/Cache.js
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const fs = require('fs')
|
||||||
|
const path = require('path')
|
||||||
|
|
||||||
|
let filePath
|
||||||
|
let cache = {}
|
||||||
|
|
||||||
|
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)
|
||||||
|
} else {
|
||||||
|
resolve()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
static get(key) {
|
||||||
|
return cache[key]
|
||||||
|
}
|
||||||
|
|
||||||
|
static loadCache(cacheFile) {
|
||||||
|
cache = {}
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
// console.log("load cache:", cacheFile)
|
||||||
|
if(cacheFile) {
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
static reset() {
|
||||||
|
cache = {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Cache
|
@ -8,6 +8,7 @@ const FeedStore = require('orbit-db-feedstore');
|
|||||||
const KeyValueStore = require('orbit-db-kvstore');
|
const KeyValueStore = require('orbit-db-kvstore');
|
||||||
const CounterStore = require('orbit-db-counterstore');
|
const CounterStore = require('orbit-db-counterstore');
|
||||||
const PubSub = require('./PubSub');
|
const PubSub = require('./PubSub');
|
||||||
|
const Cache = require('./Cache');
|
||||||
|
|
||||||
class OrbitDB {
|
class OrbitDB {
|
||||||
constructor(ipfs) {
|
constructor(ipfs) {
|
||||||
@ -54,10 +55,10 @@ class OrbitDB {
|
|||||||
const replicate = options.subscribe !== undefined ? options.subscribe : true;
|
const replicate = options.subscribe !== undefined ? options.subscribe : true;
|
||||||
const store = new Store(this._ipfs, this.user.username, dbname, options);
|
const store = new Store(this._ipfs, this.user.username, dbname, options);
|
||||||
this.stores[dbname] = store;
|
this.stores[dbname] = store;
|
||||||
return this._subscribe(store, dbname, replicate);
|
return this._subscribe(store, dbname, replicate, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
_subscribe(store, dbname, subscribe, callback) {
|
_subscribe(store, dbname, subscribe, options) {
|
||||||
if(subscribe === undefined) subscribe = true
|
if(subscribe === undefined) subscribe = true
|
||||||
|
|
||||||
store.events.on('data', this._onData.bind(this))
|
store.events.on('data', this._onData.bind(this))
|
||||||
@ -69,6 +70,11 @@ class OrbitDB {
|
|||||||
else
|
else
|
||||||
store.loadHistory().catch((e) => logger.error(e.stack));
|
store.loadHistory().catch((e) => logger.error(e.stack));
|
||||||
|
|
||||||
|
Cache.loadCache(options.cacheFile).then(() => {
|
||||||
|
const hash = Cache.get(dbname)
|
||||||
|
store.loadHistory(hash).catch((e) => logger.error(e.stack))
|
||||||
|
})
|
||||||
|
|
||||||
return store
|
return store
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,40 +83,43 @@ class OrbitDB {
|
|||||||
|
|
||||||
_onConnected(dbname, hash) {
|
_onConnected(dbname, hash) {
|
||||||
// console.log(".CONNECTED", dbname, hash, this.user.username);
|
// console.log(".CONNECTED", dbname, hash, this.user.username);
|
||||||
const store = this.stores[dbname];
|
const store = this.stores[dbname]
|
||||||
store.loadHistory(hash).catch((e) => logger.error(e.stack));
|
store.loadHistory(hash).catch((e) => logger.error(e.stack))
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Replication request from the message broker */
|
/* Replication request from the message broker */
|
||||||
|
|
||||||
_onMessage(dbname, hash) {
|
_onMessage(dbname, hash) {
|
||||||
// console.log(".MESSAGE", dbname, hash, this.user.username);
|
// console.log(".MESSAGE", dbname, hash, this.user.username)
|
||||||
const store = this.stores[dbname];
|
const store = this.stores[dbname]
|
||||||
store.sync(hash).catch((e) => logger.error(e.stack));
|
store.sync(hash)
|
||||||
|
.then((res) => Cache.set(dbname, hash))
|
||||||
|
.catch((e) => logger.error(e.stack))
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Data events */
|
/* Data events */
|
||||||
|
|
||||||
_onWrite(dbname, hash) {
|
_onWrite(dbname, hash) {
|
||||||
// 'New entry written to database...', after adding a new db entry locally
|
// 'New entry written to database...', after adding a new db entry locally
|
||||||
// console.log(".WRITE", dbname, hash, this.user.username);
|
// console.log(".WRITE", dbname, hash, this.user.username)
|
||||||
if(!hash) throw new Error("Hash can't be null!");
|
if(!hash) throw new Error("Hash can't be null!")
|
||||||
if(this._pubsub) this._pubsub.publish(dbname, hash);
|
if(this._pubsub) this._pubsub.publish(dbname, hash)
|
||||||
|
Cache.set(dbname, hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
_onData(dbname, item) {
|
_onData(dbname, item) {
|
||||||
// 'New database entry...', after a new entry was added to the database
|
// 'New database entry...', after a new entry was added to the database
|
||||||
// console.log(".SYNCED", dbname, items.length);
|
// console.log(".SYNCED", dbname, items.length);
|
||||||
this.events.emit('data', dbname, item);
|
this.events.emit('data', dbname, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
_onClose(dbname) {
|
_onClose(dbname) {
|
||||||
if(this._pubsub) this._pubsub.unsubscribe(dbname);
|
if(this._pubsub) this._pubsub.unsubscribe(dbname)
|
||||||
delete this.stores[dbname];
|
delete this.stores[dbname]
|
||||||
}
|
}
|
||||||
|
|
||||||
_connect(hash, username, password, allowOffline) {
|
_connect(hash, username, password, allowOffline) {
|
||||||
if(allowOffline === undefined) allowOffline = false;
|
if(allowOffline === undefined) allowOffline = false
|
||||||
|
|
||||||
const readNetworkInfo = (hash) => {
|
const readNetworkInfo = (hash) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
@ -17,6 +17,7 @@ const network = 'localhost:3333';
|
|||||||
const username = 'testrunner';
|
const username = 'testrunner';
|
||||||
const username2 = 'rennurtset';
|
const username2 = 'rennurtset';
|
||||||
const ipfsPath = '/tmp/orbittests';
|
const ipfsPath = '/tmp/orbittests';
|
||||||
|
const cacheFile = path.join(process.cwd(), '/test/orbit-db-cache.json')
|
||||||
|
|
||||||
let ipfs, ipfsDaemon;
|
let ipfs, ipfsDaemon;
|
||||||
const IpfsApis = [
|
const IpfsApis = [
|
||||||
@ -94,7 +95,7 @@ IpfsApis.forEach(function(ipfsApi) {
|
|||||||
.then((res) => {
|
.then((res) => {
|
||||||
ipfs = res;
|
ipfs = res;
|
||||||
return Promise.map([username, username2], (login) => {
|
return Promise.map([username, username2], (login) => {
|
||||||
return OrbitDB.connect(network, login, '', ipfs, { allowOffline: false, cacheFile: './orbit-db-cache.json' });
|
return OrbitDB.connect(network, login, '', ipfs, { allowOffline: false, cacheFile: cacheFile });
|
||||||
}).then((clients) => {
|
}).then((clients) => {
|
||||||
client1 = clients[0];
|
client1 = clients[0];
|
||||||
client2 = clients[1];
|
client2 = clients[1];
|
||||||
@ -111,31 +112,31 @@ IpfsApis.forEach(function(ipfsApi) {
|
|||||||
if(client1) client1.disconnect();
|
if(client1) client1.disconnect();
|
||||||
if(client2) client2.disconnect();
|
if(client2) client2.disconnect();
|
||||||
ipfsApi.stop().then(() => {
|
ipfsApi.stop().then(() => {
|
||||||
rimraf('./orbit-db-cache.json', done)
|
rimraf(cacheFile, done)
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('counters', function() {
|
describe('counters', function() {
|
||||||
it('increases a counter value', function(done) {
|
it('increases a counter value', function(done) {
|
||||||
const timeout = setTimeout(() => done(new Error('event was not fired')), 2000)
|
const timeout = setTimeout(() => done(new Error('event was not fired')), 2000)
|
||||||
const counter = client1.counter('counter test', { subscribe: false, cacheFile: './orbit-db-cache.json' })
|
const counter = client1.counter('counter test', { subscribe: false, cacheFile: cacheFile })
|
||||||
counter.events.on('ready', () => {
|
counter.events.on('ready', () => {
|
||||||
Promise.map([13, 1], (f) => counter.inc(f), { concurrency: 1 })
|
Promise.map([13, 1], (f) => counter.inc(f), { concurrency: 1 })
|
||||||
.then(() => {
|
.then(() => {
|
||||||
clearTimeout(timeout)
|
|
||||||
assert.equal(counter.value(), 14)
|
assert.equal(counter.value(), 14)
|
||||||
|
clearTimeout(timeout)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
.catch(done)
|
.catch(done)
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
it('creates a new counter from cached data', (done) => {
|
it('creates a new counter from cached data', function(done) {
|
||||||
const timeout = setTimeout(() => done(new Error('event was not fired')), 2000)
|
const timeout = setTimeout(() => done(new Error('event was not fired')), 2000)
|
||||||
const counter = client1.counter('counter test', { subscribe: false, cacheFile: './orbit-db-cache.json' })
|
const counter = client1.counter('counter test', { subscribe: false, cacheFile: cacheFile })
|
||||||
counter.events.on('ready', () => {
|
counter.events.on('ready', () => {
|
||||||
clearTimeout(timeout)
|
|
||||||
assert.equal(counter.value(), 14)
|
assert.equal(counter.value(), 14)
|
||||||
|
clearTimeout(timeout)
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"orbit-db.test": "QmdQEoYzjGGQYTBueU6B7s6Qr19qRdxWRuj9QUiQP17NRt"
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user