Skip to content

Commit

Permalink
Merge pull request #43 from supriyaamisshra/streams-cache-invalidation
Browse files Browse the repository at this point in the history
Added support for TTL based expiry of stream cached values.
  • Loading branch information
codenamejason authored Mar 10, 2022
2 parents 7587fef + f961e2a commit c2a4045
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions packages/react-app/src/views/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,25 @@ import { SimpleStreamABI } from "../contracts/external_ABI";
import { useHistory } from "react-router";
import { Link } from "react-router-dom";

const STREAMS_CACHE_TTL_MILLIS=Number.parseInt(process.env.REACT_APP_STREAMS_CACHE_TTL_MILLIS) || 43200000; // 12h ttl by default
// the actual cache
const streamsCache = {};

class CachedValue {
constructor(value) {
this.value = value;
this.updatedAt = Date.now();
}

isStale = () => {
return (this.updatedAt + STREAMS_CACHE_TTL_MILLIS) <= Date.now();
}
}

async function resolveStreamSummary(streamAddress, mainnetProvider) {
if (streamsCache[streamAddress]) {
return streamsCache[streamAddress];
const cachedStream = streamsCache[streamAddress];
if (cachedStream && cachedStream instanceof CachedValue && !cachedStream.isStale()) {
return cachedStream.value;
}

var contract = new ethers.Contract(
Expand All @@ -48,7 +62,7 @@ async function resolveStreamSummary(streamAddress, mainnetProvider) {
((Number(result._hex) * 0.000000000000000001) / data.cap) * 100)
);

streamsCache[streamAddress] = data;
streamsCache[streamAddress] = new CachedValue(data);
return data;
}

Expand Down

0 comments on commit c2a4045

Please sign in to comment.