All Projects → Nodonisko → Ionic Cache

Nodonisko / Ionic Cache

Licence: mit
Ionic and Angular cache service with IndexedDB, SQLite and WebSQL support

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Ionic Cache

Storage Based Queue
Javascript queue library with persistent storage based queue mechanism for the browsers environments. Specially designed for offline.
Stars: ✭ 33 (-86.69%)
Mutual labels:  offline, ionic
Blog
lizhonghui's blog
Stars: ✭ 109 (-56.05%)
Mutual labels:  ionic, angular2
Offline Gallery
🎈 A 16kb Preact & Redux based Progressive Web App that offers an offline gallery experience of external images.
Stars: ✭ 90 (-63.71%)
Mutual labels:  cache, offline
Ng Lottie
Render After Effects animations on Angular based on lottie-web
Stars: ✭ 311 (+25.4%)
Mutual labels:  ionic, angular2
Ionic3 Components
A project full of ionic 3 components and samples - to make life easier :)
Stars: ✭ 1,689 (+581.05%)
Mutual labels:  ionic, angular2
Ion2 Calendar
📅 A date picker components for ionic2 /ionic3 / ionic4
Stars: ✭ 537 (+116.53%)
Mutual labels:  ionic, angular2
Ion Digit Keyboard V2
A digital keyboard plugin to use in Ionic 2 applications.
Stars: ✭ 97 (-60.89%)
Mutual labels:  ionic, angular2
clean-to-the-core
🍏 A gluten free, accessible, offline-first, progressive web app for creating healthy meals.
Stars: ✭ 12 (-95.16%)
Mutual labels:  offline, angular2
Cachewebview
Custom implement Android WebView cache, offline website, let cahe config more simple and flexible
Stars: ✭ 1,767 (+612.5%)
Mutual labels:  cache, offline
Meteor Service Worker
An universal service worker for meteor apps
Stars: ✭ 132 (-46.77%)
Mutual labels:  cache, offline
Kinto.js
An Offline-First JavaScript Client for Kinto.
Stars: ✭ 268 (+8.06%)
Mutual labels:  cache, offline
Ionic2 Rating
⭐️ Angular star rating bar. Built for Ionic 2+.
Stars: ✭ 177 (-28.63%)
Mutual labels:  ionic, angular2
ionic-modal-custom-transitions
Add Custom Transitions to Ionic Modals.
Stars: ✭ 22 (-91.13%)
Mutual labels:  ionic, angular2
Offix
GraphQL Offline Client and Server
Stars: ✭ 694 (+179.84%)
Mutual labels:  cache, offline
angular-progress-bar
This component allow you to easy incorporate progress-bar to angular/ionic project, providing binding and color options
Stars: ✭ 26 (-89.52%)
Mutual labels:  ionic, angular2
Next Offline
make your Next.js application work offline using service workers via Google's workbox
Stars: ✭ 1,306 (+426.61%)
Mutual labels:  cache, offline
ionic-uuchat
基于ionic3,angular4的实时聊天app,兼容web端。该项目只是前端部分,所有数据需要请求后端服务器,需要配套express-uuchat-api使用。
Stars: ✭ 14 (-94.35%)
Mutual labels:  ionic, angular2
sync-client
SyncProxy javascript client with support for all major embedded databases (IndexedDB, SQLite, WebSQL, LokiJS...)
Stars: ✭ 30 (-87.9%)
Mutual labels:  ionic, offline
Ionic2 Reddit Reader
Ionic 2 Sample App
Stars: ✭ 128 (-48.39%)
Mutual labels:  ionic, angular2
Ionic2 Pokedex
🎮 Pokédex sample app developed with Ionic 2, Angular 2 and Apache Cordova. Using Pokéapi as source for data.
Stars: ✭ 143 (-42.34%)
Mutual labels:  ionic, angular2

Ionic cache service

Ionic cache service that can cache almost everything. It caches requests, observables, promises and classic data. It uses Ionic Storage so we support IndexedDB, SQLite (Cordova), WebSQL in this order. It can be used separatelety in Angular 2 application.

Key features:

  • Request caching
  • Delayed observable caching (see docs for more info)
  • Don't invalidate cache if is browser offline
  • Set and invalidate groups of entries
  • Supports IndexedDB, SQLite (Cordova), WebSQL via Ionic Storage

Please report all bugs to bug report or fix it and send pull request :)

Big thanks to all contributors for help:

Install

Via NPM:

npm install ionic-cache @ionic/storage --save

or Yarn:

yarn add ionic-cache @ionic/storage

You can optionally add Cordova SQLite.

And inject service to your app:

app.module.ts

import { CacheModule } from "ionic-cache";

@NgModule({
  ...
  imports: [
    CacheModule.forRoot()
  ],
})

app.component.ts

import { CacheService } from "ionic-cache";

@Component({
    templateUrl: "build/app.html"
})
class MyApp {
    constructor(cache: CacheService) {
        ...
        cache.setDefaultTTL(60 * 60); //set default cache TTL for 1 hour
        ....
    }
    ...
}

Usage

Config

Starting with version 3.0.2, CacheModule.forRoot() optionally accepts a config object.

The config object currently accepts a keyPrefix, which is the the internal key prefix to use when storing items.

For backwards compatibility this defaults to '', but it's recommended to set this to a different value in order to prevent issues with clearAll().

@NgModule({
  ...
  imports: [
    CacheModule.forRoot({ keyPrefix: 'my-app-cache' })
  ],
})

Observables

Cache request

...
import { CacheService } from "ionic-cache";

@Injectable()
export class SomeProvider {
    constructor(private http: HttpClient, private cache: CacheService) {}

    loadList() {
        let url = "http://ip.jsontest.com";
        let cacheKey = url;
        let request = this.http.get(url);

        return this.cache.loadFromObservable(cacheKey, request);
    }
    ...

Cache whole request response

If you need to cache the whole response, for example if you need to access the Headers, you can pass in an object with the observe key set to 'response', i.e. { observe: 'response' }. Then you can use .pipe(map(res => res.body)) to extract the response body.

...
let request = this.http.get(url, { observe: 'response' });
return this.cache.loadFromObservable(cacheKey, request).pipe(map(res => res.body));
...

Cache with custom Observable operators

loadFromObservable accepts an Observable and returns an Observable, so you are free to use all of the Observable operators. For example error handling (on error, retry request every 6 seconds if fails):

...
let request = this.http.get(url)
.pipe(retryWhen(error => error.timer(6000)));

return this.cache.loadFromObservable(cacheKey, request);
...

Delayed observable caching

loadFromDelayedObservable shows off the full power of observables. When you call this method and it will return the cached data (even if it's expired) and immediately send a request to the server and then return the new data.

...
    let request = this.http.get(url);
    let delayType = 'all'; // this indicates that it should send a new request to the server every time, you can also set it to 'none' which indicates that it should only send a new request when it's expired

    let response = this.cache.loadFromDelayedObservable(cacheKey, request, groupKey, ttl, delayType);

    response.subscribe(data => {
        console.log("Data:" data);
    });

    //result will look like this:
    // Data: "Hello world from cache"
    // Data: "Hello world from server"
...

Promises & Classic data

Cache promises

...
let key = 'some-promise';
let data = await this.cache.getOrSetItem(key, () => somePromiseFunction());
console.log("Saved data: ", data);
...

Cache classic data (arrays, objects, strings, numbers etc.)

Similarly, you can use getOrSetItem or getItem with classic data.

...
let key = 'heavily-calculated-function';

let data = await this.cache.getOrSetItem(key, () => heavilyCalculatedFunction());
console.log('Saved data: ', data);
...

If you need more control in the event that the item is expired or doesn't exist, you can use the getItem method with error handling.

...
let key = 'heavily-calculated-function';

let data = await this.cache.getItem(key)
.catch(() => {
    console.log("Oh no! My promise is expired or doesn't exist!");

    let result = heavilyCalculatedFunction();
    return this.cache.saveItem(key, result);
});

console.log('Saved data: ', data);
...

Removing cached items

You can also remove cached items by using the removeItem method.

...
let key = 'some-promise';

this.cache.removeItem(key);
...

Removing multiple cached items

You can utilize the removeItems method to remove multiple items based on a wildcard pattern.

...
await Promise.all([
    service.saveItem('movies/comedy/1', 'Scott Pilgrim vs. The World'),
    service.saveItem('movies/comedy/2', 'The Princess Bride'),
    service.saveItem('songs/metal/1', 'Who Bit the Moon'),
    service.saveItem('songs/metal/2', 'Deception - Concealing Fate, Pt. 2'),
]);

this.cache.removeItems('songs/metal/*');
...

Cached promise existence

If you need to check whether or not an item has been cached, ignoring whether or not it's expired, you can use the itemExists method.

...
let key = 'some-promise';

let exists = await this.cache.itemExists(key); // returns either a boolean indicating whether it exists or not, or an error message
...

Raw cached item

If you ever need to get a cached item regardless of whether it's expired or not, you can use the getRawItem method.

...
let key = 'some-promise';

let item = await this.cache.getRawItem(key);
...

There's also the getRawItems method, which returns an array of the raw cached items.

...
let rawItems = await this.cache.getRawItems();
let firstItem = rawItems[0]; //Has the properties: key, value, expires, type, groupKey
...

Other

Cache entries grouping

At times you may need to clear certain groups of cached items. For example, if you have an infinite scroll list with a lot of items and the user triggers a pull to refresh, you may want to delete all of the cached list items. To do this, you can supply a group key as the 3rd parameter of loadFromObservable.

...
loadList(pageNumber) {
    let url = "http://google.com/?page=" + pageNumber;
    let cacheKey = url;
    let groupKey = "googleSearchPages"

    let request = this.http.get(url);
    return this.cache.loadFromObservable(cacheKey, request, groupKey);
}
...

Then when pull to refresh is triggered, you can use the clearGroup method and pass in your group key.

...
pullToRefresh() {
    this.cache.clearGroup("googleSearchPages");
}
...

Set custom TTL for single request

If you want a custom TTL for a single request, you can pass it as the fourth parameter.

let ttl = 60 * 60 * 24 * 7; // TTL in seconds for one week
let request = this.http.get(url);

return this.cache.loadFromObservable(cacheKey, request, groupKey, ttl);

Set default TTL

this.cache.setDefaultTTL(60 * 60); //set the default cache TTL for 1 hour

Delete expired entries

It's automatically done on every startup, but you can do it manually.

this.cache.clearExpired();

Delete all entries

!Important!

Make sure that you have a keyPrefix set in the CacheModule config, otherwise this will clear everything in Ionic Storage.

this.cache.clearAll();

Disable cache

You can disable cache without any issues, it will pass all of the original Observables through and all Promises will be rejected.

this.cache.enableCache(false);

Disable offline invalidation

You can also disable invalidating cached items when the device is offline.

this.cache.setOfflineInvalidate(false);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].