Today I Learned - Series four
Let’s see what interesting and or unique things you have learned so we can all learn a little something today!
Sats for knowledge sharing!
Let’s see if we can keep the knowledge sharing until I can make this a territory!
Previous series post #706284
Today I learned that the best abstraction for a simple cache is likely a closure. I was trying to figure out a generalized way to cache things like bitcoin's blockheight and the exchange rate.
function cachedFetcher(fetcher, { cacheExpiry, forceRefreshThreshold }) { let cache = null; return async function cachedFetch(...args) { const now = Date.now(); async function fetchAndCache() { const result = await fetcher(...args); cache = { data: result, createdAt: now }; return result; } if (cache) { const age = now - cache.createdAt; if (age < cacheExpiry) { return cache.data; } else if (age < forceRefreshThreshold) { fetchAndCache().catch(console.error); return cache.data; } } return await fetchAndCache(); }; } const getBlockHeight = createCachedFetcher(fetchBlockHeight, { cacheExpiry: 60 * 1000, forceRefreshThreshold: 10 * 60 * 1000, })
reply
TIL the shortest war in history?
The Anglo-Zanzibar War was the shortest war in recorded history, lasting only 38 minutes.
It began on August 27, 1896, when Britain declared war on the Sultanate of Zanzibar in response to the death of the pro-British sultan, Hamad bin Thuwaini, who was succeeded by Khalid bin Bargash, a less pro-British candidate.
The British, fearing a shift in power, launched an attack on Zanzibar, which was quickly overwhelmed by the superior British naval and military forces.
The war ended with a British victory, as Zanzibar formally surrendered.
The war was a result of a succession dispute and British efforts to maintain influence over Zanzibar and its strategic location on the East African coast.
reply