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

Caching idea #249

Open
g105b opened this issue Sep 8, 2021 · 1 comment
Open

Caching idea #249

g105b opened this issue Sep 8, 2021 · 1 comment

Comments

@g105b
Copy link
Member

g105b commented Sep 8, 2021

It would be really useful to be able to link data structures to root nodes of the DOM in some way that can cache the contents of the node depending on the data structure's content.

Say a URL /shop/apple/iphone12 performs a database lookup, then builds up an entity and then binds it to the DOM, it would be useful to intercept the whole data cycle, compare it to a cache key (in this case, the URL (maybe the database response on more dynamic websites)), then not bother having to do any binding at all, bypassing any expensive operations.

Just an idea for now. Let's see if I can introduce this concept into any real world projects any time soon.

@g105b g105b added this to the v4 milestone Feb 20, 2023
@g105b
Copy link
Member Author

g105b commented Feb 20, 2023

Example of how this could look:

function go(DynamicPath $dynamicPath, DocumentBinder $binder, SlowDatabase $db):void {
// A cache with the name of the element tag name is a good start.
	$cache = new BinderCacheThing("shop-item-details");
	$shopItemDetailsElement = $document->querySelector("shop-item-details");

// Load from the cache according to the key - in this case, the dynamic path's "item".
// but if the cache is invalid (missing or stale), run the slow function.
	$data = $cache->load($dynamicPath->get("item"), function(string $itemName)use($db) {
		return $db->getItemFromSlowDatabase($itemName);
	});

// The bindData function can be used as normal, but the $data is always going to be typed
// as a "cached" data. The bindData function can detect that this is not a typical KVP
// and if the cached data is fresh, load the innerHTML from the cache, so no manipulation or
// looping is at all necessary.
	$binder->bindData($data, $shopItemDetailsElement);
}

Note: it's not just the loading of the data that takes time. On huge, complex data structures, the building up of the DOM takes a non-zero amount of time. If all of this could be skipped, then slow spots could be significantly sped up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant