Skip to content

Conversation

@nenharper
Copy link
Contributor

The current caching page is more guide-like than a reference page, but still not relevant to the application guide and narrative at hand.

This PR breaks this out into a documentations page and a reference page, and make the docs more narrative.

id: ID @primaryKey
}
```
Here, `expiration: 3600` means cached entries expire after an hour. Harper will automatically evict old data and refresh it when needed. By exporting this table, you instantly get a REST API for it at `http://localhost:9926/BreedCache/`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Harper just marks data as expired. It will not optimistically refresh data. The next request from a user to an expired record will result in the refetch and cache being restored.

}
class BreedAPI extends Resource {
async get() {
return (await fetch(`https://dog-api.com/${this.getId()}`)).json();
Copy link
Contributor

Choose a reason for hiding this comment

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

Lets break this up into a couple lines for clarity

Copy link
Contributor

Choose a reason for hiding this comment

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

Does dog-api actually exist? Remember this is meant to be a functional follow-along guide. Can we find a simple 3rd party resource to use here? Or is this copied from somewhere else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have been using the dog example. Not necessarily dog-api.com but it's just a placeholder

Comment on lines +38 to +40
- Check if “husky” is already in the cache.
- If not (or if it’s expired), fetch it from https://dog-api.com/husky.
- Store it in BreedCache so the next request is instant.
Copy link
Contributor

Choose a reason for hiding this comment

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

Aye this is the correct behavior i was talking about above.


When interacting with cached data, you can also use the `Cache-Control` request header to specify certain caching behaviors. When performing a PUT (or POST) method, you can use the `max-age` directive to indicate how long the resource should be cached (until stale):
- Passive caching: great for data that doesnt change often (like dog breed characteristics).
- Active caching: connect to a subscription or webhook so Harper gets notified when the source changes. Your cache updates immediately, and your users always see fresh data.
Copy link
Contributor

Choose a reason for hiding this comment

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

We do? How?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

GET /my-resource/id
Cache-Control: only-if-cached, no-store
```
Caching doesnt stop at Harper. Because every Harper cache table is a REST API, your clients can cache responses downstream too. Harper automatically tags responses with ETag headers so browsers or edge caches can hold onto data longer. That means your app is fast all the way downfrom the source, to Harper, to the client.
Copy link
Contributor

Choose a reason for hiding this comment

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

Not longer but for the "appropriate amount of time"

Copy link
Contributor

Choose a reason for hiding this comment

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

This is good progress. But what about a non-third-party caching example? Like what if I have expensive work to do on the requested resource and I want to be caching that? This page needs to be functional so the user can follow along and actually try things out. Can you show more requests demonstrating the caching? Inspecting status codes and things like that? This content mentions some cool topics too like Etags; but fails to mention that the requests have to send those same tags back to the client for subsequent requests to actually be cache hits. https://github.com/HarperDB/react-ssr-example/blob/151685affcf84e50263720ad8f1e0321d2c87fed/caching-test.js#L13-L21 without them you'll get a 200 every time.

This page barely scratches the surface for caching without ever showing off a real example. This doesn't have to go over everything or go deep into everything. I'd expect a really solid example or two and then some more high level information on the other capabilities with links out to either more examples or reference docs. If those examples don't exist yet, then put a note like "example coming soon!" so we can come back around to it. If reference docs don't exist definitely let us know so we can write them!

Copy link
Member

Choose a reason for hiding this comment

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

It looks like an example of caching a pseudo-expensive response was added, but kind of isolated and contrived. I think what we (both) really wanted was something more along lines the of https://github.com/HarperFast/react-ssr-example/blob/main/resources.js#L37-L43 which shows how to do a layered cache that is internally caching transformations.

---

# Caching
Harper provides integrated caching features for external data sources. This page documents all schema directives, configuration options, and resource APIs relevant to caching.
Copy link
Contributor

Choose a reason for hiding this comment

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

Our caching system is not just for external data sources. In fact we'd rather our users use Harper as the data source. But we support integration with 3rd party data sources because that is an important pattern for customers. Also why is this page documenting Resource api methods? Those have their own reference page. If you're trying to demonstrate how they integrate with a caching example I think thats better set for the guide page or its own example.

Copy link
Member

@kriszyp kriszyp left a comment

Choose a reason for hiding this comment

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

This looks like great progress, I think this a helpful distinction between explaining caching and providing a reference. It looks like there are still some requests for having an internal caching example and maybe a working API endpoint to use for a working example.

Copy link
Contributor

@Ethan-Arrowood Ethan-Arrowood left a comment

Choose a reason for hiding this comment

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

okay this is really strong; I'd like to get another engineer to review the reference doc for completeness and correctness but it looks good from my review. Otherwise, I really like the new example too. Nice work with the ETag headers too.


```bash
curl -i http://localhost:9926/BreedCache/husky \
-H 'If-None-Match: "1727223589-husky"'
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably good to note that the user's ETag will be different than this example

Copy link
Contributor

@Ethan-Arrowood Ethan-Arrowood left a comment

Choose a reason for hiding this comment

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

Other than randomly adding back in the table of contents reference index pages, I think this PR is good to go. Can you revert the changes to those files and just retain the caching.md changes? And then can you replicate to the relevant versions too?

Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we adding back the TOC? This was intentionally removed in #313

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants