ng2-webstorage icon indicating copy to clipboard operation
ng2-webstorage copied to clipboard

Add expire?: time to the keys

Open T04435 opened this issue 5 years ago • 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.

T04435 avatar Mar 02 '19 13:03 T04435

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 {
	// ...
}

PillowPillow avatar Mar 11 '19 10:03 PillowPillow

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?

brunnels avatar Jan 21 '20 13:01 brunnels

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

mebibou avatar Aug 18 '20 12:08 mebibou

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

mackelito avatar Sep 07 '20 12:09 mackelito

hey guys! Here is pull request for this issue: https://github.com/PillowPillow/ng2-webstorage/pull/157

sergeyglazyrindev avatar Oct 16 '21 11:10 sergeyglazyrindev