Giter VIP home page Giter VIP logo

factomd-cache-poc's Introduction

factomd-cache

A caching layer for the Factom API. Improves the performance of querying over and searching through chains on the Factom API using a hybrid memory+disk cache.

The cache polls for new pending entries and stores them in the cache (every 10 seconds by default).

Compatible with Factom.js by Paul Bernier for you other NodeJS heads!

Installing

Command Line

npm i factomd-cache

package.json

"dependencies": {
	"factomd-cache": "^0.2.0"
}

Examples

Initialization

const {FactomdCache} = require('factomd-cache');

//default settings: FactomdAPI on localhost:8088, localhost wallet on port 8089
var factomdCache = new FactomdCache();

//alternate configuration options
var factomdCache = new FactomdCache({
    factomdParams:{ //see https://www.npmjs.com/package/factom#instantiate-factomcli
		factomd: {
            host: 'localhost',
            port: 8088
		}
    }  	
});

Cache a Chain

Before anything meaningful can be done, the chain must be retrieved from Factom and stored! You can call cacheChain in advance when you know a chain your application will need to access often.

Calling other functions before calling cacheChain will cause an implicit call to cacheChain before the desired function's callback completes.

After a chain is cached, new and currently pending entries will be synced on a 10 second basis.

//Testnet test Chain ID:
const testChainID = 'f1be007d4b82e7093f2234efd1beb429bc5e0311e9ae98dcd580616a2046a6b3';

//use async/await
let entries = await factomdCache.cacheChain(testChainID);

//or promises
let entries = factomdCache.cacheChain(testChainID)
.then(function(entries){
    console.log('chain is cached!');
}).catch(function(err){throw err});

Get All Entries For a Chain

let entries = await factomdCache.getAllChainEntries(testChainID);

Get The Latest Entries For a Chain

//get the most recent 25 entries for the test chain
 let entries = await factomdCache.getLatestChainEntries(testChainID);

//specify count
let entries = await factomdCache.getLatestChainEntries(testChainID, 20);

Get Entries For a Chain By Index Range

You can get entries by chronological index!

//get entries from index range 5 (inclusive) to 10(exclusive)
let entries = await factomdCache.getRangedChainEntries(testChainID, 5, 10);

Listen For New Entries

You can listen for new entries by chain as they're committed to Factom. The chain must be in the cache to receive events

factomdCache.on('new-entries', testChainID, function (newEntries) {
        console.log('Got ' + newEntries.length + ' new entries in listnener for chain '+testChainID);
});

Clear A Chain From The Cache

Clear a single chain from the cache by ID. This will stop any pending entry listeners for your chain and clear the memory+disk cache.

factomdCache.clearChain(testChainID);

Close The Cache

Stops all event listeners

factomdCache.close();

Testing

Command Line

npm test

factomd-cache-poc's People

Contributors

drkatz avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.