title: What`s the best approach for caching db results for slide show in php/ jquery
body: <p> 
  Hi, im implementing an slide show from a database, similar to facebook modal, but with just a image/video and a description. </p>
  
  <p>
  I decided to use cached results for better performance, for now, im caching all results in an array and just ++/-- the index when the user press the arrow buttons. My code looks like :
  </p>
  
  <pre><code>var filesPrev, filesNext, initalFile = new Array();
  var currentSlide; // represents the index/key of the current showed file
  $(".imgLink").click(function() {
    idFile = $(this).attr("idFile");
    initialFile = load(idFile);  // pseudo code, it loads the first file
    loadImagesBefore(idFile).then(function(answer) { // get 10 images where id &lt; idFile
      filesPrev = answer; // cache the results in an array
      loadImagesAfter(idFile).then(function(answer) { // get 10 images where id &gt; idFile
              filesNext = answer.reverse(); // reverse just for ordering purposes
              //Now add both arrays (next/prev) and the firstFile(array) to slides array
              slides = filesNext;
              slides.push(initialFile);
              currentSlide = slides.length -1; // set current index/key to file(clicked)
              for (var i = 0; i &lt; filesPrev.length; i++) {
                                    slides.push(filesPrev[i]); // push filesPrev to slides array;
              };
              openSlide(); // format view of first image
  
      });
    });
  }
  </code></pre>
  
  <p>pseudo code for slide controller:</p>
  
  <pre><code>  leftkey.click(function() {
        currentSlide--;
        formatSlide(slide[currentSlide]);
        if (currentSlide == 1) {
          loadMoreFiles('next'); //caching again
        }
    });
    rightkey.click(function() {
        currentSlide++;
        formatSlide(slide[currentSlide]);
        if (currentSlide == slide.length) {
          loadMoreFiles('prev'); //caching again
        }
    });
  </code></pre>
  
  <p>
  The problem lies when there`s no more files to show left or right, and i want the slide to get the "otherside" results (like facebook, when you click the first photo, and press left, it will get you to the last photo). </p>
  
  <p>I currently checking if the new loaded results wont match with the idFiles of the last filesPrev results. But im thinking if i cant do it in another way. Maybe with 2 arrays, one for prev, and other for next files, what you think is the better way to go? Thanks in advance
  </p>