Caching an Observation resource
# support
m
I'm looking to improve performance by reducing unnecessary network requests to medplum for resources when navigating the application i'm building out, and i want to try and cache an array of Observation resources that i fetch from medplum, and I see there is a MedplumClient function for getting a cached resource but is there a method for me to set something in the cache? My backup plan is to utilize tanstack query for caching and data fetching but I want to explor medplum options as well.
Hi guys just following up on the thread, it came in during holidays so might be buried on top of newer support tickets.
r
HI @mikee6290 , sorry for the delay. So by default, the medplum client caches the responses to read requests, similar to ReactQuery. The behavior is defined here: https://www.medplum.com/docs/sdk/core.medplumclientoptions You can set the
cacheTime
parameter to define how long the client caches before refetching from the server
m
Hey @rahul1 , very late response from me sorry about that. I set a 24hr cache time and now the intent is to fetch from that cache. Im trying to find out how to do that but cant seem to find the correct function to access the cache? Any helpful links that can point me in the right direction?
Actually I think the getCached function may be the solution.
Is using getCached the right context for the usecase i provided?
Or to fetch an Observation resource for example that I intend on being cached from its first fetch, Id need to simply fetch it the same way I fetched it originally? The goal is to not do exxcessive api calls because im fetching multipl Observation resources, but getCached isnt returning anything for me.
r
Hi @mikee6290 , this is just a local client side cache in the Medplum Client. resources are only available in the cache after you call
readResource
or
searchResources
Can you describe the calls you are making, ideally with a code snippet?
m
I am in one component making the searchResources call to fetch multiple Observation resources by using the subjectId. This component is the first page rendered after login so it gets called right away if the Observation resources were created. Then when navigating to another page, I want to fetch those same exact Observation resources, which is why I want to utlize caching because these are excessive api calls i believe and the data at this point isnt changing or dynamic.
Any suggestions other than the getCahced method (or a reason for why it isnt working for me) on how to fetch these exact Observation resources from a cache?
Here is how I set the cache time by the way:
r
So the approach you took seems correct. Would you mind also sharing the read call you make on the 2nd page.
Some background: - When you set cacheTime > 0, every resource returned by the search is cached by its ID - on future calls to
readResource('Observation', id)
, the client will automatically read from the cache, even if you don't call
getCached
. You should be able to confirm this using the networking tab I just ran the code below as a single TS file (not in a react component) as was able to confirm that the Observation could be read from cache
Copy code
ts
const observations = await medplum.searchResources('Observation')
console.log(observations[0].id)
console.log(medplum.getCached('Observation', observations[0].id))
m
But if I have to fetch more than one resource, fetching by subjectId wont work?
r
@mikee6290 I'm not sure I'm understanding your question. The code snippet I shared allows you to search / cache multiple resources. Here's an example using the patient id:
Copy code
ts
const patientId = 'f04fde56-88cf-4713-a11a-cbe8ee9f2242'
const observations = await medplum.searchResources('Observation', `subject=Patient/${patientId}`)
console.log(observations.length)
console.log(observations[0].id)
console.log(medplum.getCached('Observation', observations[0].id))
output:
Copy code
ts
20
5abb4492-e1f0-442a-940e-7c214052f926
{
  resourceType: 'Observation',
  id: '5abb4492-e1f0-442a-940e-7c214052f926',
  meta: {
    profile: [
      'http://hl7.org/fhir/us/core/StructureDefinition/us-core-head-circumference'
    ],
    versionId: '6ba2ad0e-add2-4d06-b398-f02c17e45d4e',
    lastUpdated: '2023-08-17T18:10:32.454Z',
    author: {
      reference: 'Practitioner/996a2c29-5aaa-4810-bd6f-5f2935e90239',
      display: 'Rahul Agarwal'
    },
    project: '70721083-1dde-413a-bd2a-419771a8718e',
    compartment: [ [Object], [Object] ]
  },
  status: 'final',
  category: [ { coding: [Array] } ],
  code: { coding: [ [Object] ], text: 'Head Circumference' },
  subject: { reference: 'Patient/f04fde56-88cf-4713-a11a-cbe8ee9f2242' },
  effectiveDateTime: '2020-11-18T16:19:31-08:00',
  valueQuantity: {
    value: 51.2,
    unit: 'cm',
    system: 'http://unitsofmeasure.org',
    code: 'cm'
  }
}
157 Views