npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mutagen-d/react-native-file-cache

v0.2.0

Published

Caching files to disk in react-native apps

Downloads

7

Readme

RNFileCache

Build Status

Caching files to disk in react-native apps

Installation

This package depends on rn-fetch-blob. First install it.

npm i @mutagen-d/react-native-file-cache
// or
yarn add @mutagen-d/react-native-file-cache

Usage

  • first load files from disk
await RNFileCache.load()
  • then get file path from url or download file
if (RNFileCache.exists(url)) {
  const filepath = RNFileCache.getPath(url)
} else {
  const file = await RNFileCache.download({ url })
  const filepath = file.path
}
  • lock file to prevent from removing
RNFileCache.lock("<lock id>", filepath)
  • unlock file when needed
RNFileCache.unlock("<lock id>")
import { Logger } from "react-native-file-cache"
Logger.setLevel(Logger.DEBUG)

API

| Method | Return type | Description | | ----------------------------------------- | ----------- | -------------------------------------------------------------------------- | | maxSize | number | maximum size of cached files, defaults 512 MiB | | totalSize | number | current size of cached files | | load() | | loads files from cache | | exists() | boolean | return if file exists in cache | | getPath() | string | get file path, regardless of whether file exists or not * | | download() | File | download file | | cancelDownload() | | cancel active download, partially downlaoded file will be removed | | pruneCache() | | removes old files if cache size is reached maxSize | | setClearingRatio() | | set size of cache that will be removed when pruning cache, defaults 0.15 | | getClearingRatio() | number | | | lock() | | locks file from being deleted | | unlock() | | unlock locked file | | remove() | | removes file by url | | removeFile() | | removes file by path | | isRemoving() | boolean | | | onRemoved() | | set listener on file remove | | removeAll() | | removes all files from cache |

* If file doesn't exist and url doesn't contain extension part, then getPath(url) returns filepath without file extension. If file exists, then getPath(url) returns filepath with file extension, even when url does not contain extension part

load

console.log(RNFileCache.isReady()) // `false`
await RNFileCache.load()
console.log(RNFileCache.isReady()) // `true`

exists

const exists = RNFileCache.exists("https://example.com/image.png")

getPath

const path = RNFileCache.getPath("https://example.com/image.png")

download

try {
  const file = await RNFileCache.download({
    method: "GET",
    url: "https://example.com/image.png",
    headers: {
      Authorization: "Bearer <token>",
    },
    onProgress: (loaded: number, total: number) => {
      console.log("progress", loaded / total)
    },
    onLoad: (file: File) => {
      console.log("file", file)
    },
    onError: (error: any) => {
      console.log("error", error)
    },
    onLoadEnd: () => {},
    onCancel: () => {
      console.log("file downloading canceled")
    },
  })
} catch (e) {
  console.log("error", e)
}

cancelDownload

await RNFileCache.cancelDownload("https://example.com/image.png")

setClearingRatio

RNFileCache.setClearingRatio(0.4)
RNFileCache.pruneCache()

assert(RNFileCache.totalSize <= RNFileCache.maxSize * 0.6)

lock

const lockId1 = "<id1>"
const lockId2 = "<id2>"
RNFileCache.lock(lockId1, file.path)
RNFileCache.lock(lockId2, file.path)
// file is not deleted untill `unlock` it
await RNFileCache.removeFile(file.path)

unlock

RNFileCache.unlock("<id1>")
RNFileCache.unlock("<id2>")

remove

await RNFileCache.remove("https://example.com/image.png")

removeFile

await RNFileCache.removeFile(file.path)