Skip to content

Latest commit

 

History

History
43 lines (34 loc) · 1.11 KB

README.md

File metadata and controls

43 lines (34 loc) · 1.11 KB

Components

cache-service

Use this service to cache network responses or any other object for a several amount of time.

Note: This services doesn't invalidate cached items on its own.

ViewModel

import {CacheService} from "cache-service";
import {autoinject} from 'aurelia-framework';
import {HttpClient} from 'aurelia-fetch-client';
import {toMilliseconds} from "./time";

@autoinject()
export class ViewModel {
  constructor(
      private readonly _cacheService: CacheService,
      private readonly _httpClient: HttpClient
  ) {
    const defaultCacheTttl = {seconds: 60};
    this._cacheService.setDefaultCacheTtl(defaultCacheTttl);
    
    // Invalidate outdated keys periodically
    window.setInterval(() => {
      this._cacheService.invalidateOutdated();
    }, toMilliseconds(defaultCacheTttl))
  }

  loadSomething() {
    const loadingLambda = () => {
      return this._httpClient.fetch('http://example.com/anything.json');
    };

    this._cacheService.getForKeyWithLoadingFunction('cache-id', loadingLambda)
        .then(response => {
          console.log(response);
        });
  }
}