Skip to content

Instantly share code, notes, and snippets.

@dinigo
Last active December 14, 2017 23:44
Show Gist options
  • Save dinigo/ebe022ae18e715e7312901bf06cbeae9 to your computer and use it in GitHub Desktop.
Save dinigo/ebe022ae18e715e7312901bf06cbeae9 to your computer and use it in GitHub Desktop.

Revisions

  1. dinigo revised this gist Dec 14, 2017. 1 changed file with 3 additions and 5 deletions.
    8 changes: 3 additions & 5 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@ If you retry the request in a time shorter than the period it returns the cached
    You can also set a postprocess function and another function that runs every time.

    ### Simple example
    Usage
    ```javascript
    const simpleCahedRun = new CachedRun(2500, ()=>Math.random());
    simpleCahedRun.start().then(res => console.log(res)); // run instantly
    @@ -13,11 +14,8 @@ setTimeout(() => simpleCahedRun.start().then(res => console.log(res)),3000); //
    setTimeout(() => simpleCahedRun.refresh(),3500); // force refresh
    setTimeout(() => simpleCahedRun.start().then(res => console.log(res)),4000); // run after 4 secons```
    ```

    403
    VM382:66
    VM382:67
    VM382:69
    Output
    ```
    > 0.06756653717504979 // run instantly: new cache until 2.5s
    > 0.06756653717504979 // run after 1 second: get from cache
    > 0.04633045049386242 // run after 3 seconds: new cache until 5s
  2. dinigo revised this gist Dec 14, 2017. 1 changed file with 13 additions and 5 deletions.
    18 changes: 13 additions & 5 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -6,15 +6,23 @@ You can also set a postprocess function and another function that runs every tim

    ### Simple example
    ```javascript
    const simpleCahedRun = new CachedRun(3000, ()=>Math.random());
    const simpleCahedRun = new CachedRun(2500, ()=>Math.random());
    simpleCahedRun.start().then(res => console.log(res)); // run instantly
    setTimeout(() => simpleCahedRun.start().then(res => console.log(res)),1000); // run after 1 second
    setTimeout(() => simpleCahedRun.start().then(res => console.log(res)),3000); // run after 3 secons
    setTimeout(() => simpleCahedRun.refresh(),3500); // force refresh
    setTimeout(() => simpleCahedRun.start().then(res => console.log(res)),4000); // run after 4 secons```
    ```
    ```
    > 0.123423 // run instantly
    > 0.123423 // run after 1 second
    > 0.687382 // run after 3 seconds

    403
    VM382:66
    VM382:67
    VM382:69
    > 0.06756653717504979 // run instantly: new cache until 2.5s
    > 0.06756653717504979 // run after 1 second: get from cache
    > 0.04633045049386242 // run after 3 seconds: new cache until 5s
    // force refresh
    > 0.20170561495875194 // run after 4 seconds: forced refresh
    ```
    ### Complete Example
  3. dinigo revised this gist Dec 13, 2017. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions cached-run.js
    Original file line number Diff line number Diff line change
    @@ -53,4 +53,11 @@ class CachedRun {
    return (new Date() - this.lastCall > this.period)?
    this.getFreshResults().then(res => this.cacheResults(res)) : this.cacheResults();
    }

    /**
    * Forces the next run to refresh the cache
    */
    refresh(){
    this.lastCall = 0;
    }
    }
  4. dinigo revised this gist Dec 13, 2017. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,6 @@ You can also set a postprocess function and another function that runs every tim
    ### Simple example
    ```javascript
    const simpleCahedRun = new CachedRun(3000, ()=>Math.random());
    undefined
    simpleCahedRun.start().then(res => console.log(res)); // run instantly
    setTimeout(() => simpleCahedRun.start().then(res => console.log(res)),1000); // run after 1 second
    setTimeout(() => simpleCahedRun.start().then(res => console.log(res)),3000); // run after 3 secons
  5. dinigo revised this gist Dec 13, 2017. 2 changed files with 46 additions and 5 deletions.
    20 changes: 18 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,25 @@
    **Cached Runner** receives a period of time and a request function, and performs all async operations to get the data.
    ## Cached Runner
    Receives a period of time and a request function, and performs all async operations to get the data.
    If you retry the request in a time shorter than the period it returns the cached result instead.

    You can also set a postprocess function and another function that runs every time.

    Here you have an example:
    ### Simple example
    ```javascript
    const simpleCahedRun = new CachedRun(3000, ()=>Math.random());
    undefined
    simpleCahedRun.start().then(res => console.log(res)); // run instantly
    setTimeout(() => simpleCahedRun.start().then(res => console.log(res)),1000); // run after 1 second
    setTimeout(() => simpleCahedRun.start().then(res => console.log(res)),3000); // run after 3 secons
    ```
    ```
    > 0.123423 // run instantly
    > 0.123423 // run after 1 second
    > 0.687382 // run after 3 seconds
    ```

    ### Complete Example
    Here we set all available options.
    ```javascript
    const period = 2000; // ms
    const allways = data => +new Date() + ' --> ' + data; // an allways run function that adds a timestamp
    31 changes: 28 additions & 3 deletions cached-run.js
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,39 @@
    class CachedRun {
    constructor(period, request, postProcess, allwaysRun){

    /**
    * Constructor for the class. Receives a period and a request function to run.
    *
    * @param {number} period - Number of millis to keep the cache.
    * @param {function} request - Function that returns the requested value.
    * @param {function} [process] - Run data from the request through this function before caching.
    * @param {function} [allways] - Run data through this allways at the end of the pipeline.
    */
    constructor(period, request, process, allways){
    this.period = period;
    this.request = request;
    this.postProcess = postProcess || (el=>el);
    this.allwaysRun = allwaysRun || (el=>el);
    this.postProcess = process || (el=>el);
    this.allwaysRun = allways || (el=>el);
    this.lastCall = 0;
    this.self = this;
    }

    /**
    * Run the request function and the postprocess function (if there's one defined).
    *
    * @return {Promise} Promise with the requested values allready processed.
    */
    getFreshResults(){
    return Promise
    .resolve(this.request())
    .then(res => this.postProcess(res));
    }

    /**
    * Stores cache and manages timer.
    *
    * @param {Object} [results] - Results from the request and further postprocess.
    * @param {Promise} Promise with the cached results processed.
    */
    cacheResults(results){
    if(results) {
    this.cache = results;
    @@ -24,6 +44,11 @@ class CachedRun {
    .then(data => this.allwaysRun(data));
    }

    /**
    * Run the pipeline according to the configuration.
    *
    * @return {Promise} Data run through all the pipeline.
    */
    start(){
    return (new Date() - this.lastCall > this.period)?
    this.getFreshResults().then(res => this.cacheResults(res)) : this.cacheResults();
  6. dinigo revised this gist Dec 13, 2017. No changes.
  7. dinigo revised this gist Dec 13, 2017. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions cached-run.js
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,9 @@ class CachedRun {
    this.period = period;
    this.request = request;
    this.postProcess = postProcess || (el=>el);
    this.allwaysRun = allwaysRun || (el=>el);
    this.allwaysRun = allwaysRun || (el=>el);
    this.lastCall = 0;
    this.self = this;
    this.self = this;
    }

    getFreshResults(){
    @@ -20,12 +20,12 @@ class CachedRun {
    this.lastCall = +new Date();
    }
    return Promise
    .resolve(this.cache)
    .then(data => this.allwaysRun(data));
    .resolve(this.cache)
    .then(data => this.allwaysRun(data));
    }

    start(){
    return (new Date() - this.lastCall > this.period)?
    this.getFreshResults().then(res => this.cacheResults(res)) : this.cacheResults();
    }
    }
    }
  8. dinigo created this gist Dec 13, 2017.
    31 changes: 31 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    **Cached Runner** receives a period of time and a request function, and performs all async operations to get the data.
    If you retry the request in a time shorter than the period it returns the cached result instead.

    You can also set a postprocess function and another function that runs every time.

    Here you have an example:
    ```javascript
    const period = 2000; // ms
    const allways = data => +new Date() + ' --> ' + data; // an allways run function that adds a timestamp
    const request = () => Math.random(); // a request function that generates a random number
    const process = num => `~(${Math.ceil(num*100)})~`;
    const cachedRun = new CachedRun(period, request, process, allways);
    cachedRun.start().then(res => console.log(res));
    setTimeout(() => cachedRun.start().then(res => console.log(res)),1000);
    setTimeout(() => cachedRun.start().then(res => console.log(res)),3000);
    ```

    Inmediate after invocation `request -> process -> cached -> allways -> returned`. New timestamp and new random number:
    ```
    > "1513186989013 --> ~(4)~"
    ```
    One second after invocation data isn't refreshed since period hasn't expired so: `cached -> allways -> returned`.
    New timestamp and cached random number:
    ```
    > "1513186990016 --> ~(4)~"
    ```
    Three secons after first invocation cache has expired, so request is rerun:
    `request -> process -> cached -> allways -> returned`. New timestamp and new random number:
    ```
    > "1513186992017 --> ~(90)~"
    ```
    31 changes: 31 additions & 0 deletions cached-run.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    class CachedRun {
    constructor(period, request, postProcess, allwaysRun){
    this.period = period;
    this.request = request;
    this.postProcess = postProcess || (el=>el);
    this.allwaysRun = allwaysRun || (el=>el);
    this.lastCall = 0;
    this.self = this;
    }

    getFreshResults(){
    return Promise
    .resolve(this.request())
    .then(res => this.postProcess(res));
    }

    cacheResults(results){
    if(results) {
    this.cache = results;
    this.lastCall = +new Date();
    }
    return Promise
    .resolve(this.cache)
    .then(data => this.allwaysRun(data));
    }

    start(){
    return (new Date() - this.lastCall > this.period)?
    this.getFreshResults().then(res => this.cacheResults(res)) : this.cacheResults();
    }
    }