react-native-file-cache

1.0.12 • Public • Published

文件缓存模块

依赖以下包

  • react-native-requests
  • react-native-omfs
  • react-native-sqlite-storage

必须在项目显示安装以上依赖包,否则react-native link命令无法链接到工程。

安装说明

npm install react-native-filecache --save

使用说明

模块对外导出两部分:FileCache级FileType.

FileCache一共四个方法:

  • fetch 获取uri缓存,如果不存在则自动下载到缓存。
  • save 保存本地文件到缓存
  • remove 删除缓存
  • clear 清空缓存

以上方法使用说明参见filecache.js源文件中的注释。

FileType定义了内置的文件类型:

  • Image 图片
  • Video 视频
  • Audio 音频
  • Voice 语音
  • Other 其他

以上类型都为字符串。

fetch使用示例如下:

import { FileCache,FileType,CacheImage } from 'react-native-file-cache';

//定义fetch option
let option = {
		//定义下载进度
        onProgress: (received, total) => {
            console.log('FileCache progress', received, total);
        },
        //扩展名
        ext:'png',
        type: FileType.Image
    };
    
//获取缓存
let fetchTask = FileCache.fetch('http://www.baidu.com/', option);
fetchTask.then((path) => {
        console.log('FileCacheManager,done 1', path);
    })
    .catch((error) => {
        console.log('FileCacheManager,error 1', error);
    });

.
.
.

//取消获取缓存操作
fetchTask.cancel();

//CacheImage为自定义组件,用于为网络图片增加缓存

save使用示例如下:

import { FileCache,FileType } from 'react-native-filecache';

FileCache.save(
	'http://www.baidu.com',
	'/Users/lcg/data/image/1.png',
	{ type: FileType.Other,ext: 'png'})
	.then((path) => {
		console.log('FileCacheManager,done 6',path);
	})
	.catch((error) => {
		console.log('FileCacheManager,error 6',error);
	});

Package Sidebar

Install

npm i react-native-file-cache

Weekly Downloads

1

Version

1.0.12

License

ISC

Last publish

Collaborators

  • npm_ibrain