Redux Resource
  • Home
  • Introduction
    • Motivation
    • Core Concepts
    • Similar Projects
    • Examples
  • Resources
    • Resource Reducers
    • Resource Objects
    • Meta
    • Lists
    • Modifying Resources
  • Requests
    • Request Objects
    • Keys
    • Names
    • Statuses
    • Request Actions
      • Updating Lists
      • Reading Resources
      • Updating Resources
      • Creating Resources
      • Deleting Resources
  • Other Guides
    • Usage With React
    • Tracking Request Statuses
    • Using Request Statuses
    • Custom Action Types
    • Migration Guides
  • Recipes
    • Forms
    • Canceling Requests
    • Unauthorized Responses
    • User Feedback
    • Related Resources
    • Caching
  • Ecosystem Extras
    • Redux Resource Action Creators
    • Redux Resource XHR
    • Redux Resource Prop Types
    • Redux Resource Plugins
      • HTTP Status Codes
      • Selection
      • Reset
      • Included Resources
  • FAQ
    • General
    • State Tree
    • Actions
    • Lists
  • API Reference
    • resourceReducer
    • getStatus
    • getResources
    • upsertResources
    • setResourceMeta
    • actionTypes
    • requestStatuses
Powered by GitBook
On this page
  • Caching using requests
  • Accessing data from a dynamic key
  • The role of lists
  1. Recipes

Caching

PreviousRelated ResourcesNextEcosystem Extras

Last updated 7 years ago

Caching server responses can improve the responsiveness of your application.

Because the main Redux Resource library does not provide tools to make HTTP requests, it is not possible for the main library to provide a caching mechanism.

With that said, bindings for view libraries, such as React, are the perfect place for caching to be implemented. Official React bindings for Redux Resource are in the works, and they will be built using , a powerful, declarative HTTP library for React.

This recipe contains tips that could help you if you're interested in writing your own caching implementation, either by using React Request or by writing your own system.

Caching using requests

How can you know if a response has already been returned for a given request? The way that we recommend doing it is by using .

Here's how it works with React Request:

React Request implements its own . Its caching is powered by a string called a based on the request configuration you pass to it. Two requests with the same key are considered identical.

This automatically-generated "request key" will be used as the Redux Resource requestKey, which is what ties the two libraries together.

Accessing data from a dynamic key

Tools like getStatus are more difficult to use directly when using a dynamic request name.

In the official React bindings for Redux Resource, the solution to this problem will be a wrapping component around React Request will automatically pull the details of that request from the Redux store, and pass it to you in a render prop function. It will also pull the resources, resource meta, and lists that the request affected.

Because of this, you will rely a lot less on directly using getStatus and getResources when using React Redux Resource, although they will still be there should you need them.

The role of lists

Sometimes, requests contribute to a list. For instance, if you fetch a user's favorite books, and then they create a new favorite book, you may have a list called "favoriteBooks", which is what your component renders out.

What do you do in this situation? Simply continue to cache at the request level.

By using a cached response, you are making the claim that a response from the server for that request could not provide any more information for the list that you already have.

As a result, you do not make the request, and you continue to render out the locally-cached list.

React Request
request objects
caching system
"request key"