Giter Club home page Giter Club logo

ng2-cache's Introduction

ng2-cache

Client side caching service for Angular2

Installation

To install this library, run:

$ npm install ng2-cache --save

Usage:

import {Component} from '@angular/core';
import {CacheService, CacheStoragesEnum} from 'ng2-cache/ng2-cache';

declare var BUILD_VERSION: string;

@Component({
    selector: 'some-selector',
    template: '<div>Template</div>',
    providers: [ CacheService ]
})
export class ExampleComponent {

    constructor(private _cacheService: CacheService) {}

    public func() {

        //set global prefix as build version
        this._cacheService.setGlobalPrefix(BUILD_VERSION);

        //put some data to cache "forever"
        //returns true is data was cached successfully, otherwise - false
        let result: boolean = this._cacheService.set('key', ['some data']);

        //put some data to cache for 5 minutes (maxAge - in seconds)
        this._cacheService.set('key', ['some data'], {maxAge: 5 * 60});

        //put some data to cache for 1 hour (expires - timestamp with milliseconds)
        this._cacheService.set('key', {'some': 'data'}, {expires: Date.now() + 1000 * 60 * 60});

        //put some data to cache with tag "tag"
        this._cacheService.set('key', 'some data', {tag: 'tag'});
        
        //get some data by key, null - if there is no data or data has expired
        let data: any|null = this._cacheService.get('key');

        //check if data exists in cache
        let exists: boolean = this._cacheService.exists('key');

        //remove all data from cache with tag "tag"
        this._cacheService.removeTag('tag');

        //remove all from cache
        this._cacheService.removeAll();

        //get all data related to tag "tag" :
        // {'key' => 'key data', ...}
        this._cacheService.getTagData('tag');
        
        //change storage (returns new instance of service with needed storage)
        this._cacheService.useStorage(CacheStoragesEnum.LOCAL_STORAGE);

    }
}

By default service store data in session storage, you could select one of storages:

  • session storage
  • local storage
  • memory

If current storage is not available - service will choose memory storage.

To change storage to local storage:

import {Component, provide} from 'angular2/core';
import {CacheService, CacheStorageAbstract, CacheLocalStorage} from 'ng2-cache/ng2-cache';

@Component({
    selector: 'some-selector',
    template: '<div>Template</div>',
    providers: [
        CacheService,
        {provide: CacheStorageAbstract, useClass:CacheLocalStorage}
    ]
})

License

ISC © Romanov Evgeny

ng2-cache's People

Contributors

jackson88 avatar ctg-mmulder avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.