git ssb

0+

ev / microbay



forked from Dominic / patchbay

Commit d3e75c0a69e9cd486c63267e3e63786761c34c90

cache messages loaded via createLogStream

Dominic Tarr committed on 8/4/2016, 1:50:50 AM
Parent: 473ab796d9ba662aa1f1665d60c10838dc0c35d9

Files changed

sbot-api.jschanged
sbot-api.jsView
@@ -27,8 +27,11 @@
2727 var createFeed = require('ssb-feed')
2828 var keys = require('./keys')
2929 var ssbKeys = require('ssb-keys')
3030
31+
32+var cache = CACHE = {}
33+
3134 module.exports = function () {
3235 var opts = createConfig()
3336 var sbot = null
3437 var connection_status = []
@@ -102,15 +105,24 @@
102105 sbot_query: rec.source(function (query) {
103106 return sbot.query.read(query)
104107 }),
105108 sbot_log: rec.source(function (opts) {
106- return sbot.createLogStream(opts)
109+ return pull(
110+ sbot.createLogStream(opts),
111+ pull.through(function (e) {
112+ CACHE[e.key] = CACHE[e.key] || e.value
113+ })
114+ )
107115 }),
108116 sbot_user_feed: rec.source(function (opts) {
109117 return sbot.createUserStream(opts)
110118 }),
111119 sbot_get: rec.async(function (key, cb) {
112- sbot.get(key, cb)
120+ if(CACHE[key]) cb(null, CACHE[key])
121+ sbot.get(key, function (err, value) {
122+ if(err) return cb(err)
123+ cb(null, CACHE[key] = value)
124+ })
113125 }),
114126 sbot_publish: rec.async(function (content, cb) {
115127 if(content.recps)
116128 content = ssbKeys.box(content, content.recps.map(function (e) {
@@ -136,4 +148,6 @@
136148 })
137149 }
138150 }
139151
152+
153+

Built with git-ssb-web