0.2.0 • Published 6 years ago

ang-extras-caching v0.2.0

Weekly downloads
-
License
Apache-2.0
Repository
gitlab
Last release
6 years ago

ang-extras-caching

This is an Angular library for simple caching of observable values.

The service will check the back-end storage for the cached value, if it exists and is within the cache expiry time return it as an observable.

If the cached value does not exist or have expired, the supplied observable will be called and the returned value cached before returning it to the caller.

Cached items are currently stored in the browser localStorage.

License

Copyright 2019 A. Palsson

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Example

Below is an example of a service generated by Swagger used to retrieve a list of countries. The second method demonstrates the use of the caching service.

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { CountryDTO, CountryResourceService } from '../client';
import { CachingService } from 'ang-extras-caching';

@Injectable({
  providedIn: 'root'
})
export class CountryService {

  constructor(
    private countryResource: CountryResourceService,
    private cachingService: CachingService,
  ) {
  }

  public fetchCountries(): Observable<CountryDTO[]> {
    return this.countryResource.getAllCountriesUsingGET();
  }

  public fetchCountriesCached(): Observable<CountryDTO[]> {
    return this.cachingService
      .get('countries',
        this.countryResource.getAllCountriesUsingGET(),
        600);
  }

}
0.2.0

6 years ago

0.1.0

6 years ago