How to use caching with the riot-lol-api npm package?











up vote
1
down vote

favorite












QUESTION:



After reading this:



https://www.npmjs.com/package/riot-lol-api#caching



I am still confused. This is my first time trying to cache api responses.



For example, I do not know what values are available for YOUR_CACHE_STRATEGY and it is not explained in the docs.



Essentially, I would be looking for an example, like how can I cache and serve for 5 min the response from /lol/summoner/v3/summoners/by-name/ ?





CODE:



riotRequest.request(region.toLowerCase(), 'summoner', '/lol/summoner/v3/summoners/by-name/'+encodeURI(player), function(err, data) {
if (!err) {
var summonerID = data.id;
} else {
console.error("ERROR1: "+err);
res.render("page", {errorMessage: "Player not found !"});
}
});









share|improve this question


























    up vote
    1
    down vote

    favorite












    QUESTION:



    After reading this:



    https://www.npmjs.com/package/riot-lol-api#caching



    I am still confused. This is my first time trying to cache api responses.



    For example, I do not know what values are available for YOUR_CACHE_STRATEGY and it is not explained in the docs.



    Essentially, I would be looking for an example, like how can I cache and serve for 5 min the response from /lol/summoner/v3/summoners/by-name/ ?





    CODE:



    riotRequest.request(region.toLowerCase(), 'summoner', '/lol/summoner/v3/summoners/by-name/'+encodeURI(player), function(err, data) {
    if (!err) {
    var summonerID = data.id;
    } else {
    console.error("ERROR1: "+err);
    res.render("page", {errorMessage: "Player not found !"});
    }
    });









    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      QUESTION:



      After reading this:



      https://www.npmjs.com/package/riot-lol-api#caching



      I am still confused. This is my first time trying to cache api responses.



      For example, I do not know what values are available for YOUR_CACHE_STRATEGY and it is not explained in the docs.



      Essentially, I would be looking for an example, like how can I cache and serve for 5 min the response from /lol/summoner/v3/summoners/by-name/ ?





      CODE:



      riotRequest.request(region.toLowerCase(), 'summoner', '/lol/summoner/v3/summoners/by-name/'+encodeURI(player), function(err, data) {
      if (!err) {
      var summonerID = data.id;
      } else {
      console.error("ERROR1: "+err);
      res.render("page", {errorMessage: "Player not found !"});
      }
      });









      share|improve this question













      QUESTION:



      After reading this:



      https://www.npmjs.com/package/riot-lol-api#caching



      I am still confused. This is my first time trying to cache api responses.



      For example, I do not know what values are available for YOUR_CACHE_STRATEGY and it is not explained in the docs.



      Essentially, I would be looking for an example, like how can I cache and serve for 5 min the response from /lol/summoner/v3/summoners/by-name/ ?





      CODE:



      riotRequest.request(region.toLowerCase(), 'summoner', '/lol/summoner/v3/summoners/by-name/'+encodeURI(player), function(err, data) {
      if (!err) {
      var summonerID = data.id;
      } else {
      console.error("ERROR1: "+err);
      res.render("page", {errorMessage: "Player not found !"});
      }
      });






      javascript node.js caching






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 13:50









      TheProgrammer

      313118




      313118
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          The documentation is not very detailed indeed. What you need to do is basically implement the cache object as specified in the code sample from the doc (the commented area).



          Below is an example of caching to an array (in memory). You could also save this array to a file or into a Redis database as suggested in the doc.



          //cacheData holds objects of type {key: 123, value: "request data"}
          var cacheData =
          var cacheIndex = 0

          function deleteFromCache(key) {
          for (var i = 0; i < cacheData.length; i++) {
          if (cacheData[i].key == key) {
          cacheData.splice(i, 1);
          return;
          }
          }
          }

          var cache = {
          get: function(region, endpoint, cb) {
          for (var entry of cacheData) {
          if (entry.value == data) {
          //we have a cache hit
          return cb(null, entry.value);
          }
          }
          return cb(null, null);
          },
          set: function(region, endpoint, cacheStrategy, data) {
          var key = cacheIndex++;
          var value = data;

          cacheData.push({key, value});

          //cacheStrategy is a number representing the number of seconds to keep the data in cache
          setTimeout(() => {
          deleteFromCache(key);
          }, cacheStrategy * 1000);
          }
          };


          YOUR_CACHE_STRATEGY is an object that is passed to your set function in the cacheStrategy parameter. They suggest it can be a number representing the lifespan of the cache entry, so I implemented a timer to delete the cache entry after a number of seconds equal to cacheStrategy.



          You would call the request using this number:



          riotRequest.request(region.toLowerCase(), 'summoner', '/lol/summoner/v3/summoners/by-name/'+encodeURI(player), 30, function(err, data) {//.....


          To enable caching you need to pass the cache object to the constructor of RiotRequest :



          var riotRequest = new RiotRequest('my_api_key', cache);





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226995%2fhow-to-use-caching-with-the-riot-lol-api-npm-package%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            The documentation is not very detailed indeed. What you need to do is basically implement the cache object as specified in the code sample from the doc (the commented area).



            Below is an example of caching to an array (in memory). You could also save this array to a file or into a Redis database as suggested in the doc.



            //cacheData holds objects of type {key: 123, value: "request data"}
            var cacheData =
            var cacheIndex = 0

            function deleteFromCache(key) {
            for (var i = 0; i < cacheData.length; i++) {
            if (cacheData[i].key == key) {
            cacheData.splice(i, 1);
            return;
            }
            }
            }

            var cache = {
            get: function(region, endpoint, cb) {
            for (var entry of cacheData) {
            if (entry.value == data) {
            //we have a cache hit
            return cb(null, entry.value);
            }
            }
            return cb(null, null);
            },
            set: function(region, endpoint, cacheStrategy, data) {
            var key = cacheIndex++;
            var value = data;

            cacheData.push({key, value});

            //cacheStrategy is a number representing the number of seconds to keep the data in cache
            setTimeout(() => {
            deleteFromCache(key);
            }, cacheStrategy * 1000);
            }
            };


            YOUR_CACHE_STRATEGY is an object that is passed to your set function in the cacheStrategy parameter. They suggest it can be a number representing the lifespan of the cache entry, so I implemented a timer to delete the cache entry after a number of seconds equal to cacheStrategy.



            You would call the request using this number:



            riotRequest.request(region.toLowerCase(), 'summoner', '/lol/summoner/v3/summoners/by-name/'+encodeURI(player), 30, function(err, data) {//.....


            To enable caching you need to pass the cache object to the constructor of RiotRequest :



            var riotRequest = new RiotRequest('my_api_key', cache);





            share|improve this answer



























              up vote
              1
              down vote



              accepted










              The documentation is not very detailed indeed. What you need to do is basically implement the cache object as specified in the code sample from the doc (the commented area).



              Below is an example of caching to an array (in memory). You could also save this array to a file or into a Redis database as suggested in the doc.



              //cacheData holds objects of type {key: 123, value: "request data"}
              var cacheData =
              var cacheIndex = 0

              function deleteFromCache(key) {
              for (var i = 0; i < cacheData.length; i++) {
              if (cacheData[i].key == key) {
              cacheData.splice(i, 1);
              return;
              }
              }
              }

              var cache = {
              get: function(region, endpoint, cb) {
              for (var entry of cacheData) {
              if (entry.value == data) {
              //we have a cache hit
              return cb(null, entry.value);
              }
              }
              return cb(null, null);
              },
              set: function(region, endpoint, cacheStrategy, data) {
              var key = cacheIndex++;
              var value = data;

              cacheData.push({key, value});

              //cacheStrategy is a number representing the number of seconds to keep the data in cache
              setTimeout(() => {
              deleteFromCache(key);
              }, cacheStrategy * 1000);
              }
              };


              YOUR_CACHE_STRATEGY is an object that is passed to your set function in the cacheStrategy parameter. They suggest it can be a number representing the lifespan of the cache entry, so I implemented a timer to delete the cache entry after a number of seconds equal to cacheStrategy.



              You would call the request using this number:



              riotRequest.request(region.toLowerCase(), 'summoner', '/lol/summoner/v3/summoners/by-name/'+encodeURI(player), 30, function(err, data) {//.....


              To enable caching you need to pass the cache object to the constructor of RiotRequest :



              var riotRequest = new RiotRequest('my_api_key', cache);





              share|improve this answer

























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                The documentation is not very detailed indeed. What you need to do is basically implement the cache object as specified in the code sample from the doc (the commented area).



                Below is an example of caching to an array (in memory). You could also save this array to a file or into a Redis database as suggested in the doc.



                //cacheData holds objects of type {key: 123, value: "request data"}
                var cacheData =
                var cacheIndex = 0

                function deleteFromCache(key) {
                for (var i = 0; i < cacheData.length; i++) {
                if (cacheData[i].key == key) {
                cacheData.splice(i, 1);
                return;
                }
                }
                }

                var cache = {
                get: function(region, endpoint, cb) {
                for (var entry of cacheData) {
                if (entry.value == data) {
                //we have a cache hit
                return cb(null, entry.value);
                }
                }
                return cb(null, null);
                },
                set: function(region, endpoint, cacheStrategy, data) {
                var key = cacheIndex++;
                var value = data;

                cacheData.push({key, value});

                //cacheStrategy is a number representing the number of seconds to keep the data in cache
                setTimeout(() => {
                deleteFromCache(key);
                }, cacheStrategy * 1000);
                }
                };


                YOUR_CACHE_STRATEGY is an object that is passed to your set function in the cacheStrategy parameter. They suggest it can be a number representing the lifespan of the cache entry, so I implemented a timer to delete the cache entry after a number of seconds equal to cacheStrategy.



                You would call the request using this number:



                riotRequest.request(region.toLowerCase(), 'summoner', '/lol/summoner/v3/summoners/by-name/'+encodeURI(player), 30, function(err, data) {//.....


                To enable caching you need to pass the cache object to the constructor of RiotRequest :



                var riotRequest = new RiotRequest('my_api_key', cache);





                share|improve this answer














                The documentation is not very detailed indeed. What you need to do is basically implement the cache object as specified in the code sample from the doc (the commented area).



                Below is an example of caching to an array (in memory). You could also save this array to a file or into a Redis database as suggested in the doc.



                //cacheData holds objects of type {key: 123, value: "request data"}
                var cacheData =
                var cacheIndex = 0

                function deleteFromCache(key) {
                for (var i = 0; i < cacheData.length; i++) {
                if (cacheData[i].key == key) {
                cacheData.splice(i, 1);
                return;
                }
                }
                }

                var cache = {
                get: function(region, endpoint, cb) {
                for (var entry of cacheData) {
                if (entry.value == data) {
                //we have a cache hit
                return cb(null, entry.value);
                }
                }
                return cb(null, null);
                },
                set: function(region, endpoint, cacheStrategy, data) {
                var key = cacheIndex++;
                var value = data;

                cacheData.push({key, value});

                //cacheStrategy is a number representing the number of seconds to keep the data in cache
                setTimeout(() => {
                deleteFromCache(key);
                }, cacheStrategy * 1000);
                }
                };


                YOUR_CACHE_STRATEGY is an object that is passed to your set function in the cacheStrategy parameter. They suggest it can be a number representing the lifespan of the cache entry, so I implemented a timer to delete the cache entry after a number of seconds equal to cacheStrategy.



                You would call the request using this number:



                riotRequest.request(region.toLowerCase(), 'summoner', '/lol/summoner/v3/summoners/by-name/'+encodeURI(player), 30, function(err, data) {//.....


                To enable caching you need to pass the cache object to the constructor of RiotRequest :



                var riotRequest = new RiotRequest('my_api_key', cache);






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 10 at 20:49

























                answered Nov 10 at 20:43









                mihai

                22.7k73968




                22.7k73968






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226995%2fhow-to-use-caching-with-the-riot-lol-api-npm-package%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Full-time equivalent

                    さくらももこ

                    13 indicted, 8 arrested in Calif. drug cartel investigation