PillowPillow/ng2-webstorage

Add expire?: time to the keys

Opened this issue · 5 comments

Is your feature request related to a problem? Please describe.
I would like the option to have a expire arg in the .store(key, value, expire?)

Describe the solution you'd like
A clear and concise description of what you want to happen.
current:

store(key: string, value: any): Observable<any> {
		return this.strategy.set(StorageKeyManager.normalize(key), value);
	}

solution:

store(key: string, value: any,expire?: Date): Observable<any> {
                 // Set the expire date in the strategy below
		return this.strategy.set(StorageKeyManager.normalize(key), value);
	}
// baseSyncStorage.ts
get(key: string): Observable<any> {
		let data: any = this.cache.get(this.name, key);
		if (data !== undefined) return of(data);

		try {
			const item: any = this.storage.getItem(key);
			if (item !== null) {
				data = JSON.parse(item);
                                 // NEW CODE
                                 if(data.expire !== null) {
                                     if (moment(data.expire).diff(moment()) < 0) // date.expire is over Date.now() 
                                     //remove key form storage
                                 }
				this.cache.set(this.name, key, data);
			}
		} catch(err) {
			console.warn(err);
		}

		return of(data);
	}

Describe alternatives you've considered
Alternatives: every value will be a object {value, expire: Date}
then when accessing the value I'll need to check the expire expire.diff(NOW) > 0 ==> expired so delete the key.

Thanks for the great work.

Hi @T04435, I understand your needs but I m not sure that it should be implemented in this library.
I'll let this issue opened and waits for other feedbacks to see if it would be used by more than one people..

FYI - You always can override the LocalStorageStrategy by providing another strategy with the same name to the STORAGE_STRATEGIES provider.
The LocalStorageService will use your custom strategy instead of the native one.

ex :

export class CustomLocalStorageStrategy extends BaseSyncStorageStrategy {

	// !! important - you have to use this key to override the existing strategy
	static readonly strategyName: string = StorageStrategies.Local;

	readonly name: string = CustomLocalStorageStrategy.strategyName;

	constructor(@Inject(LOCAL_STORAGE) protected storage: WebStorage,
	            protected cache: StrategyCacheService,
	            @Inject(PLATFORM_ID) protected platformId: any,
	            protected zone: NgZone) {
		super(storage, cache);
		if (isPlatformBrowser(this.platformId)) this.listenExternalChanges();
	}

	// override whatever you want

	protected listenExternalChanges() {
		window.addEventListener('storage', (event: StorageEvent) => this.zone.run(() => {
			if (event.storageArea !== this.storage) return;
			const key: string = event.key;
			if (key !== null) this.cache.del(this.name, event.key);
			else this.cache.clear(this.name);
			this.keyChanges.next(key);
		}));
	}

}

@NgModule({
	// ...
	providers: [
		{provide: STORAGE_STRATEGIES, useClass: CustomLocalStorageStrategy, multi: true},
	]
	// ...
})
export class MyAppModule {
	// ...
}

Please forgive me for resurrecting this old issue but it's still open and I have a similar need.

When I try to override the LocalStorageStrategy as you've detailed I get

Can't resolve 'ngx-webstorage/lib/core/interfaces/webStorage'
Can't resolve 'ngx-webstorage/lib/strategies/baseSyncStorage'

What am I missing?

I think this is an interesting feature as well, one could always create a wrapper around your library to do the caching, but it would be v nice to have it directly in the store method as an optional parameter

I second that such a feature would be really nice to have :)

hey guys!
Here is pull request for this issue:
#157