# react-native-realm-cache-image

> A cache-image for react-native with using Realm

Latest version **1.0.10** (published 2017-10-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-native-realm-cache-image
pnpm add react-native-realm-cache-image
yarn add react-native-realm-cache-image
bun add react-native-realm-cache-image
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.10 |
| Published | 2017-10-13 |
| First published | 2016-09-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Maintainers | mpochron |
| Keywords | react-native, react-component, ios, android, cache-image, ref, cache, webcache, remobile, mobile |

## Links

- npm: https://www.npmjs.com/package/react-native-realm-cache-image
- Repository: https://github.com/machei/react-native-realm-cache-image
- Issues: https://github.com/machei/react-native-realm-cache-image/issues
- npm.io page: https://npm.io/package/react-native-realm-cache-image

## Dependencies (2)

- [react-native-fs](https://npm.io/package/react-native-fs.md) ^2.0.1-rc.2
- [react-timer-mixin](https://npm.io/package/react-timer-mixin.md) ^0.13.3

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 1.0.10 (latest) — 2017-10-13
- 1.0.9 — 2016-09-26
- 1.0.8 — 2016-09-26
- 1.0.7 — 2016-09-26
- 1.0.6 — 2016-09-26
- 1.0.5 — 2016-09-25
- 1.0.4 — 2016-09-25
- 1.0.3 — 2016-09-25
- 1.0.2 — 2016-09-25
- 1.0.1 — 2016-09-25
- 1.0.0 — 2016-09-25

## README

# React Native Realm CacheImage (forked from remobile)
    A cache-image for react-native
* When use web image, this module will find the web image's url corresponding image file is exist or not, if exist, it will use local exist file, and check this CacheImage's old url and new url, if old url is the same as new url, do noting. if not, sub old url's ref and add new url's ref, if old url's ref is 0, delete old url corresponding image file.
* it also will check total storage, total storage is 1024*1024*50, when local file total size is above total storage, it will delete the oldest local file and clear corresponding record.

## Installation

1) FS:  https://github.com/johanneslumpe/react-native-fs

2) Realm: https://realm.io/docs/react-native/latest/#installation

3) Command:

```sh
npm install --save react-native-realm-cache-image
```
## Usage

### Example
```js
'use strict';

var Realm = require('realm');
var React = require('react-native');

var {
    StyleSheet,
    View,
} = React;

var CacheImage = require('react-native-realm-cache-image');

// You can push schema wrote by self into this array.
var _realm = new Realm({schema: CacheImage.getSchemaRealm()});
CacheImage.setup(_realm);

var CacheImageIdMgr = {
    CACHE_ID_USER_HEAD: 0,
    CACHE_ID_USER_HEAD1: 1,
    CACHE_ID_USER_HEAD2: 2,
};

var SERVER = 'http://192.168.1.117:3000/';

module.exports = React.createClass({
    render: function() {
        return (
            <View style={styles.container}>
                <CacheImage
                    resizeMode='stretch'
                    defaultImage={app.img.tabnav_list}
                    url={SERVER+"1.png"}
                    style={styles.image}
                    cacheId={CacheImageIdMgr.CACHE_ID_USER_HEAD}
                    />
                <CacheImage
                    resizeMode='stretch'
                    defaultImage={app.img.tabnav_list}
                    url={SERVER+"2.png"}
                    style={styles.image}
                    cacheId={CacheImageIdMgr.CACHE_ID_USER_HEAD1}
                    />
                <CacheImage
                    resizeMode='stretch'
                    defaultImage={app.img.tabnav_list}
                    url={SERVER+"3.png"}
                    style={styles.image}
                    cacheId={CacheImageIdMgr.CACHE_ID_USER_HEAD2}
                    />
            </View>
        );
    }
});

var styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    image: {
        width:200,
        height:200,
    }
});
```

#### Props
This module supports react-native Image's props all.
- `defaultImage : PropTypes.string.isRequired` - local image for default image
- `url : PropTypes.string.isRequired` - the url of web image
    * format: 'http://{server}/{name}.png' or 'http://{server}/{name}.jpg'
    * must have jpg or png suffix
    * {name} must have version. e.g: first version is verison1.jpg, when you want to update image, you can use verison2.jpg, otherwise this module will not download image
- `cacheId : PropTypes.number.isRequired` - the unique image id for a CacheImage
    * make sure every CacheImage's cacheId is unique
    * like
    * ``` var CacheImageIdMgr = {
        CACHE_ID_USER_HEAD: 0,
        CACHE_ID_USER_HEAD1: 1,
        CACHE_ID_USER_HEAD2: 2,
    }; ```

---
_Source: https://npm.io/package/react-native-realm-cache-image · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
