UNPKG

@pscoped/ng2-cache

Version:

> ng2-cache library compatible with AoT compilation & Tree shaking like an official package.

402 lines (392 loc) 12.2 kB
import * as i0 from '@angular/core'; import { NgModule, Injectable, Optional } from '@angular/core'; import { CommonModule } from '@angular/common'; class Ng2CacheModule { static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: Ng2CacheModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.1.4", ngImport: i0, type: Ng2CacheModule, imports: [CommonModule] }); static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: Ng2CacheModule, imports: [CommonModule] }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: Ng2CacheModule, decorators: [{ type: NgModule, args: [{ imports: [CommonModule], }] }] }); var CacheStoragesEnum; (function (CacheStoragesEnum) { CacheStoragesEnum[CacheStoragesEnum["LOCAL_STORAGE"] = 0] = "LOCAL_STORAGE"; CacheStoragesEnum[CacheStoragesEnum["SESSION_STORAGE"] = 1] = "SESSION_STORAGE"; CacheStoragesEnum[CacheStoragesEnum["MEMORY"] = 2] = "MEMORY"; })(CacheStoragesEnum || (CacheStoragesEnum = {})); /** * Abstract cache storage */ class CacheStorageAbstract { } /** * Service for storing data in session storage */ class CacheSessionStorage extends CacheStorageAbstract { getItem(key) { let value = sessionStorage.getItem(key); return value ? JSON.parse(value) : null; } setItem(key, value) { try { sessionStorage.setItem(key, JSON.stringify(value)); return true; } catch (e) { return false; } } removeItem(key) { sessionStorage.removeItem(key); } clear() { sessionStorage.clear(); } type() { return CacheStoragesEnum.SESSION_STORAGE; } isEnabled() { try { sessionStorage.setItem('test', 'test'); sessionStorage.removeItem('test'); return true; } catch (e) { return false; } } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: CacheSessionStorage, deps: null, target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: CacheSessionStorage }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: CacheSessionStorage, decorators: [{ type: Injectable }] }); /** * Service for storing data in local storage */ class CacheLocalStorage extends CacheStorageAbstract { getItem(key) { const value = localStorage.getItem(key); return value ? JSON.parse(value) : null; } setItem(key, value) { try { localStorage.setItem(key, JSON.stringify(value)); return true; } catch (e) { return false; } } removeItem(key) { localStorage.removeItem(key); } clear() { localStorage.clear(); } type() { return CacheStoragesEnum.LOCAL_STORAGE; } isEnabled() { try { localStorage.setItem('test', 'test'); localStorage.removeItem('test'); return true; } catch (e) { return false; } } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: CacheLocalStorage, deps: null, target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: CacheLocalStorage }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: CacheLocalStorage, decorators: [{ type: Injectable }] }); /** * Service for storing data in local storage */ class CacheMemoryStorage extends CacheStorageAbstract { _data = {}; getItem(key) { return this._data[key] ? this._data[key] : null; } setItem(key, value) { this._data[key] = value; return true; } removeItem(key) { delete this._data[key]; } clear() { this._data = []; } type() { return CacheStoragesEnum.MEMORY; } isEnabled() { return true; } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: CacheMemoryStorage, deps: null, target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: CacheMemoryStorage }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: CacheMemoryStorage, decorators: [{ type: Injectable }] }); const CACHE_PREFIX = 'CacheService'; const DEFAULT_STORAGE = CacheStoragesEnum.SESSION_STORAGE; const DEFAULT_ENABLED_STORAGE = CacheStoragesEnum.MEMORY; class CacheService { _storage; /** * Default cache options */ _defaultOptions = { expires: Number.MAX_VALUE, maxAge: Number.MAX_VALUE }; /** * Cache prefix */ _prefix = CACHE_PREFIX; constructor(_storage) { this._storage = _storage; this._validateStorage(); } /** * Set data to cache * @param key * @param value * @param options */ set(key, value, options) { let storageKey = this._toStorageKey(key); options = options ? options : this._defaultOptions; if (this._storage.setItem(storageKey, this._toStorageValue(value, options))) { if (!this._isSystemKey(key) && options.tag) { this._saveTag(options.tag, storageKey); } return true; } return false; } /** * Get data from cache * @param key * @returns any */ get(key) { let storageValue = this._storage.getItem(this._toStorageKey(key)), value = null; if (storageValue) { if (this._validateStorageValue(storageValue)) { value = storageValue.value; } else { this.remove(key); } } return value; } /** * Check if value exists * @param key * @returns boolean */ exists(key) { return !!this.get(key); } /** * Remove item from cache * @param key */ remove(key) { this._storage.removeItem(this._toStorageKey(key)); this._removeFromTag(this._toStorageKey(key)); } /** * Remove all from cache */ removeAll() { this._storage.clear(); } /** * Get all tag data * @param tag * @returns Array */ getTagData(tag) { let tags = this.get(this._tagsStorageKey()) || {}, result = {}; if (tags[tag]) { tags[tag].forEach((key) => { let data = this.get(this._fromStorageKey(key)); if (data) { result[this._fromStorageKey(key)] = data; } }); } return result; } /** * Create a new instance of cache with needed storage * @param type * returns CacheService */ useStorage(type) { let service = new CacheService(this._initStorage(type)); service.setGlobalPrefix(this._getCachePrefix()); return service; } /** * Remove all by tag * @param tag */ removeTag(tag) { let tags = this.get(this._tagsStorageKey()) || {}; if (tags[tag]) { tags[tag].forEach((key) => { this._storage.removeItem(key); }); delete tags[tag]; this.set(this._tagsStorageKey(), tags); } } /** * Set global cache key prefix * @param prefix */ setGlobalPrefix(prefix) { this._prefix = prefix; } /** * Validate cache storage */ _validateStorage() { if (!this._storage) { this._storage = this._initStorage(DEFAULT_STORAGE); } if (!this._storage.isEnabled()) { this._storage = this._initStorage(DEFAULT_ENABLED_STORAGE); } } /** * Remove key from tags keys list * @param key */ _removeFromTag(key) { let tags = this.get(this._tagsStorageKey()) || {}, index; for (let tag in tags) { index = tags[tag].indexOf(key); if (index !== -1) { tags[tag].splice(index, 1); this.set(this._tagsStorageKey(), tags); break; } } } /** * Init storage by type * @param type * @returns CacheStorageAbstract */ _initStorage(type) { let storage; switch (type) { case CacheStoragesEnum.SESSION_STORAGE: storage = new CacheSessionStorage(); break; case CacheStoragesEnum.LOCAL_STORAGE: storage = new CacheLocalStorage(); break; default: storage = new CacheMemoryStorage(); } return storage; } _toStorageKey(key) { return this._getCachePrefix() + key; } _fromStorageKey(key) { return key.replace(this._getCachePrefix(), ''); } /** * Prepare value to set to storage * @param value * @param options * returns {value: any, options: CacheOptionsInterface} */ _toStorageValue(value, options) { return { value: value, options: this._toStorageOptions(options) }; } /** * Prepare options to set to storage * @param options * @returns CacheOptionsInterface */ _toStorageOptions(options) { var storageOptions = {}; storageOptions.expires = options.expires ? options.expires : (options.maxAge ? Date.now() + (options.maxAge * 1000) : this._defaultOptions.expires); storageOptions.maxAge = options.maxAge ? options.maxAge : this._defaultOptions.maxAge; return storageOptions; } /** * Validate storage value * @param value * @returns boolean */ _validateStorageValue(value) { return !!value.options.expires && value.options.expires > Date.now(); } /** * check if its system cache key * @param key * returns boolean */ _isSystemKey(key) { return [this._tagsStorageKey()].indexOf(key) !== -1; } /** * Save tag to list of tags * @param tag * @param key */ _saveTag(tag, key) { let tags = this.get(this._tagsStorageKey()) || {}; if (!tags[tag]) { tags[tag] = [key]; } else { tags[tag].push(key); } this.set(this._tagsStorageKey(), tags); } /** * Get global cache prefix * returns {string} * private */ _getCachePrefix() { return this._prefix; } _tagsStorageKey() { return 'CacheService_tags'; } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: CacheService, deps: [{ token: CacheStorageAbstract, optional: true }], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: CacheService }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.4", ngImport: i0, type: CacheService, decorators: [{ type: Injectable }], ctorParameters: () => [{ type: CacheStorageAbstract, decorators: [{ type: Optional }] }] }); /** * Generated bundle index. Do not edit. */ export { CacheLocalStorage, CacheMemoryStorage, CacheService, CacheSessionStorage, CacheStorageAbstract, CacheStoragesEnum, Ng2CacheModule }; //# sourceMappingURL=pscoped-ng2-cache.mjs.map