File

src/app/modules/shared/services/cache-service/cache.service.ts

Index

Methods

Methods

Public exists
exists(key?: string)
Parameters :
Name Type Optional
key string Yes
Returns : boolean
Public get
get(key?: string)
Parameters :
Name Type Optional
key string Yes
Returns : any
Public remove
remove(key: string)
Parameters :
Name Type Optional
key string No
Returns : boolean
Public removeAll
removeAll()
Returns : any
Public set
set(key: string, value: any, options?: any)
Parameters :
Name Type Optional
key string No
value any No
options any Yes
Returns : boolean
import { Injectable } from '@angular/core';

@Injectable()
export class CacheService {

    public get(key?: string) {
        try {
            let value = localStorage.getItem(key);
            return value ? JSON.parse(value) : null;
        } catch (e) {
            return false;
        }
    }

    public set(key: string, value: any, options?: any) {
        try {
            localStorage.setItem(key, JSON.stringify(value));
            return true;
        } catch (e) {
            return false;
        }
    }

    public remove(key: string) {
        try {
            localStorage.removeItem(key);
            return true;
        } catch (e) {
            return false;
        }
    }

    public removeAll() {
        return localStorage.clear();
    }

    public exists(key?: string) {
        return !!this.get(key);
    }

}

results matching ""

    No results matching ""