jaggedsoft / node-binance-api

Node Binance API is an asynchronous node.js library for the Binance API designed to be easy to use.
MIT License
1.58k stars 768 forks source link

Control cache size for book depthCache #838

Open Max-SDSharp opened 2 years ago

Max-SDSharp commented 2 years ago

Hello, I searched and didn't find how to control the cache size for the book depthCache. The object is updated with Binance data, but there is no memory size control, it is only possible to use the sortAsks and sortBids functions with the maximum sampling value, correct? I would like to suggest an update to the clearDepthCache function to allow controlling the size of the cache instead of just clearing it all like the current one. If there is another way I would like suggestions. Follow the code below.

            clearDepthCache(symbols, limit = 200, clearAll = false) {
                if (!clearAll) {
                    const cache = getDepthCache(symbols);
                    if (Object.keys(cache.asks).length > limit) {
                        const sortedAsks = Object.keys(cache.asks).sort((a, b) => parseFloat(a) - parseFloat(b));
                        if (sortedAsks.length > limit) {
                            const sortedAsksOver = sortedAsks.slice(limit);
                            sortedAsksOver.map((asks) => {
                                delete Binance.depthCache[symbols].asks[asks];
                            });
                        }
                    }
                    if (Object.keys(cache.bids).length > limit) {
                        const sortedBids = Object.keys(cache.bids).sort((a, b) => parseFloat(b) - parseFloat(a));
                        if (sortedBids.length > limit) {
                            const sortedBidsOver = sortedBids.slice(limit);
                            sortedBidsOver.map((bids) => {
                                delete Binance.depthCache[symbols].bids[bids];
                            });
                        }
                    }
                }
                else {
                    const symbolsArr = Array.isArray(symbols) ? symbols : [symbols];
                    symbolsArr.forEach(thisSymbol => {
                        delete Binance.depthCache[thisSymbol];
                    });
                }
            },