File

app/core/client-side-cache/client-side-cache.service.ts

Index

Properties
Methods

Constructor

constructor(http: HttpClient)

Cached values are stored in sessionStorage in the form {key:value} Cached values are available to the users of the service in the form of observables

Parameters :
Name Type Optional
http HttpClient No

Methods

Public getValue
getValue(key: string, url: string, timeToLive?: number)
Parameters :
Name Type Optional Description
key string No

The key of the desired

url string No

Specifies the url where the value can be found if it is not present in the cache

timeToLive number Yes

Cache invalidation time in minutes - only used if cache is stale

Returns : Observable<any>
Public setValue
setValue(key: string, value: any, timeToLive?: number)
Parameters :
Name Type Optional Description
key string No

The key that can later be used to get the value

value any No

The value to be cached. Can later be retrived by using the key parameter

timeToLive number Yes

Cache invalidation time in minutes

Returns : void

Properties

Static Readonly CACHE_TIME
Type : number
Default value : 10
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';

@Injectable()
export class ClientSideCacheService {
	/**
	 * Cached values are stored in sessionStorage in the form {key:value}
	 * Cached values are available to the users of the service in the form of observables
	 */

	// Need to read all cachedValues into memory here
	constructor(private http: HttpClient) {
	}

	static readonly CACHE_TIME: number = 10; // Default cache time in minutes

	/**
	 *
	 * @param key The key of the desired
	 * @param url Specifies the url where the value can be found if it is not present in the cache
	 * @param timeToLive Cache invalidation time in minutes - only used if cache is stale
	 */
	public getValue(key: string, url: string, timeToLive?: number): Observable<any> {

		// Check if we have the value cached
		const value = JSON.parse(sessionStorage.getItem(key));
		let obsValue: Observable<any> = new Observable<any>();

		if (value) {
			obsValue = of(value);
		}

		// Read value if it is not yet read or has been invalidated
		// (this works, because no value = less than now)
		const isStale = new Date(sessionStorage.getItem(`${key}-invalidation-time`)) < new Date();
		if (isStale) {
			if (value) {
				this.http.get(url).subscribe( data => {
					if (data) {
						this.setValue(key, data, timeToLive);
					}
				});
			} else {
				obsValue = new Observable((observer) => {
					this.http.get(url).subscribe( data => {
						observer.next(data);
						observer.complete();
						this.setValue(key, data, timeToLive);
					});
				});
			}
		}

		return obsValue;
	}

	/**
	 * @param key The key that can later be used to get the value
	 * @param value The value to be cached. Can later be retrived by using the key parameter
	 * @param timeToLive Cache invalidation time in minutes
	 */
	public setValue(key: string, value: any, timeToLive?: number): void {
		sessionStorage.setItem(key, JSON.stringify(value));
		sessionStorage.setItem(
			`${key}-invalidation-time`,
			new Date(new Date().getTime() + ((timeToLive !== undefined)? timeToLive  : ClientSideCacheService.CACHE_TIME) * 60 * 1000).toString());
	}

}

result-matching ""

    No results matching ""