Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cache peer locations for 1 week #732

Merged
merged 3 commits into from
Aug 1, 2018
Merged

Conversation

hacdias
Copy link
Member

@hacdias hacdias commented Jul 19, 2018

Taking in account what I said on #730, I tried to do a little experiment with Peer Locations. It doesn't add much more code and it is clean and improves performance over time. I set the peer location cached for a week, but maybe we could set it to expire in more time or even don't expire at all.

It uses IndexedDB, which in case of not being available in that browser, just fails silently. This cache is supposed to be something to improve the app and not really required to be used.

License: MIT
Signed-off-by: Henrique Dias hacdias@gmail.com

@hacdias hacdias self-assigned this Jul 19, 2018
@hacdias hacdias requested review from olizilla, alanshaw and fsdiogo July 19, 2018 21:06
@hacdias hacdias force-pushed the feat/revamp/caching branch from 9279ebd to 07d8766 Compare July 19, 2018 21:12
@hacdias hacdias force-pushed the feat/revamp/caching branch from 07d8766 to e5a890e Compare July 20, 2018 13:04
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
@hacdias hacdias force-pushed the feat/revamp/caching branch from e5a890e to d8a0630 Compare July 20, 2018 13:10
@hacdias
Copy link
Member Author

hacdias commented Jul 26, 2018

@olizilla @alanshaw @fsdiogo what's your thoughts on this PR?

@olizilla
Copy link
Member

@hacdias did you see https://reduxbundler.com/api/included-bundles.html#createcachebundlecachingfunction
and bundle.persistActions ?

With those configured we'd have a mechanism to cache entire bundle states after specified actions occur. That is a different idea to having certain data look ups cached, so it might be that we need both techniques, but I wanted to check that you'd seen the built in mechanism, as it may be that we can refactor things to just use that.

Copy link
Contributor

@fsdiogo fsdiogo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

But isn't one week a bit too much? 🤔

@hacdias
Copy link
Member Author

hacdias commented Jul 27, 2018

@olizilla I did try that. And then some objects who had functions (mainly objects from Big.js) would cause lots of errors because with serialization and deserialization they would lost add and other fns. But I may have done sth wrong though

@hacdias
Copy link
Member Author

hacdias commented Jul 27, 2018

@fsdiogo I don't think it's too much time for locations. I doubt that every peer is moving from city to city every day 😄

@olizilla
Copy link
Member

@hacdias that's interesting! We shouldn't be storing non-serializable things in the redux tree, as a lot of the extra magic of redux assumes that the tree is safe to serialize. If we fixed the other bundles to only strore serializable stuff, would you be able to use the existing caching features of redux-bundler to achieve this?

@hacdias
Copy link
Member Author

hacdias commented Jul 27, 2018

@olizilla I guess so

@hacdias
Copy link
Member Author

hacdias commented Aug 1, 2018

Some more thoughts: I have been trying with createCacheBundler and I've hit only one issue: on Peers Location bundle, for example, we have the following state:

{
	locations: [...],
	queuingPeers: [...],
    resolvingPeers: [...]
}

And the cache bundle saves it all even though we just want to cache the locations themselves and not anything else. We would need to make some adaptations to the bundle.

Second point: if we're not using createAsyncBundle the expire date will be the same for all data. Otherwise we would need to create a mechanism to check that.

Third point: since it saves everything on one entry on IndexedDB, if we set the expire date for one week, all of the data might happen to expire at the same time or never expire if we're always updating that bundle data (hence updating the last modified date).

I hope I've been clear enough because I know I wasn't crystal clear with this message 😞

To finish: I'd say the idea on this PR is much more flexible but at the same time it doesn't have the initial data all set up. I'm not sure if we want to have all data cached. So I have two suggestions:

  1. If a bundle wants to have a cache, it will get it through a selector or even something more generic like this:

export bundleCreator(opts) {
	opts.cache = opts.cache || {}
	const cache = getCache(opts.cache)

	return {
		...
		// use the cache in the bundle
		...
	}
}
  1. Adapt createCacheBundle in order to support overriding/removing unwanted fields.

I'd like to hear your opinions.

Which types of data do we want to cache right now? Peers locations? Bandwidth? Explore data (since it's static)?

name: 'peerLocations',

cacheOptions: {
maxAge: ms.weeks(1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we can provide a version number here in the future if we want to invalidate existing caches, say if we totally change the structure of the data. Very cool.

https://github.com/HenrikJoreteg/redux-bundler-example/blob/0f23da51a32853a2d83aaf26e39e78f34997debc/src/utils/cache.js#L5-L14

That example also talks about adding in some unique identify where cached data is specific to a given user... we might want to cache info specific to a given ipfs node identity at some point so it's nice to know that there is a way to manage that. ✨

@olizilla
Copy link
Member

olizilla commented Aug 1, 2018

@hacdias I think this is a cool solution. Your write up of how this compares with the existing caching solution is super helpful.

I think we can use both caching styles. The proposal in the PR makes sense for storing specific lookups, and doing so explicitly and procedurally, so the dev has full control of when it happens.

For simpler situations where the bundle is created with caching in mind, then we can just use the built in persistActions support and have it store the whole bundle.

As for "how long should we cache peer locations for" I think a week is fine. As a future PR if we cached the look up of IP address to location rather than peer address to location, we could cache them for much longer as the mapping will only change when the geoip db gets updated.

Copy link
Member

@olizilla olizilla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is rad ✨ Update the conflict and lets get it merged.

@hacdias hacdias merged commit bdf61b4 into revamp Aug 1, 2018
@hacdias hacdias deleted the feat/revamp/caching branch August 1, 2018 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants