repo_description: 🐶  React hook for making isomorphic http requests
repo_topics: fetch fetch-data graphql http isomorphic mutation query react react-cache react-fetch-hook react-hook react-hooks react-suspense react-usefetch reacthook request rest-client ssr suspense usefetch
code: import { Cache } from '../types'
  
  let inMemoryStorage: any = {}
  const getMemoryStorage = ({ cacheLife }: { cacheLife: number }): Cache => {
    const remove = async (...responseIDs: string[]) => {
      for (const responseID of responseIDs) {
        delete inMemoryStorage[responseID]
        delete inMemoryStorage[`${responseID}:ts`]
      }
    }
  
    const isExpired = (responseID: string) => {
      const expiration = inMemoryStorage[`${responseID}:ts`]
      const expired = expiration > 0 && expiration < Date.now()
      if (expired) remove(responseID)
      return expired || !inMemoryStorage[responseID]
    }
  
    const get = async (responseID: string) => {
      if (isExpired(responseID)) return
      return inMemoryStorage[responseID] as Response
    }
  
    const set = async (responseID: string, res: Response) => {
      inMemoryStorage[responseID] = res
      inMemoryStorage[`${responseID}:ts`] = cacheLife > 0 ? Date.now() + cacheLife : 0
    }
  
    const has = async (responseID: string) => !isExpired(responseID)
  
    const clear = async () => {
      inMemoryStorage = {}
    }
  
    return Object.defineProperties(inMemoryStorage, {
      get: { value: get, writable: false, configurable: true },
      set: { value: set, writable: false, configurable: true },
      has: { value: has, writable: false, configurable: true },
      delete: { value: remove, writable: false, configurable: true },
      clear: { value: clear, writable: false, configurable: true }
    })
  }
  
  export default getMemoryStorage
  