2013-06-05 3 views
0

다른 서버에서 호스팅되는 json 파일을 찾는 javascript/ajax 함수가 있습니다. JSON 파일보다 오래된 1 시간 Ajax를 사용하여 캐시 파일을 저장하려면 어떻게해야합니까?

  • 있는지 확인하기 위해 자신의 로컬 서버에서 외부 서버에서
  • 확인
  • 저장 JSON 파일을

    1. 가져 오기 JSON 파일 : 나는 다음과 같은 작업을 수행 할 제 기능을해야합니다 이 1 시간보다 오래된 밤은 경우 사용하는 데이터
    2. 이 1 시간보다 오래된 경우 - 외부 서버에서 다시 다운로드 및 덮어 쓰기 로컬 버전

    현재 내 함수는 호출 될 때마다 외부 서버에서 데이터를 가져옵니다. 이 캐싱 기능을 추가해야하지만 어떻게 처리해야하는지 확신 할 수 없습니다.

    누구든지 조언을 제공 할 수 있습니까? 여기

    내 코드 : 사전에

     function ajaxLoad(page,url,type,variety,category , widgetClickedId) 
         {   
    
          /* 
          * Checking the clicked id is the same as passed in one 
          * TODO refactor so the clicked id only ever gets passed 
          */ 
          if(widgetId != widgetClickedId && widgetClickedId != undefined) 
          { 
           return false; 
          } 
    
          var website = $('#button-print'+widgetId).data("website");   
          var page = $('#button-print'+widgetId).data("page"); 
    
    
          $.ajax({ 
           type: 'GET',     
           url: 'www.myothersite.com/api/v1/productchoice.json?website='+website, 
           async: true, 
           jsonp: 'callback', 
           dataType: 'jsonp', 
           success: function(productchoice) 
           { 
    
            if(productchoice['brand'+widgetId] == 'null' || productchoice['brand'+widgetId] == undefined) 
            { 
             productchoice['brand'+widgetId] = ''; 
            } 
            //check that all values are not null , if not , then show the widget      
            if(  productchoice['brand'+widgetId] == ''          
              && productchoice['subject'+widgetId] == '' 
              && productchoice['market'+widgetId] == '' 
              && productchoice['type'+widgetId] == '' 
              && productchoice['bookazinebrands'+widgetId] == '') 
            {      
    
             //is this corect?       
             $('#gdmContainer'+widgetId).hide(); 
             //return false; 
            } 
            else 
            {     
             $('#gdmContainer'+widgetId).show();       
            } 
    
            setRibbons(productchoice.ribbonTop , productchoice.ribbonBottom); 
            getProductChoice(productchoice);  
            setLoveTitle(productchoice); 
            setDefaultBtn(productchoice['defaultBtn'+widgetId]);      
            return productchoice; 
           }    
          });  
    

    감사합니다!

  • 답변

    0

    Ajax 요청에서 리턴 된 데이터로 javascript 오브젝트를 작성하면됩니다. 해당 개체의 일부로 개체가 캐시 된 날짜를 저장합니다. 그런 다음 함수를 호출 할 때마다 해당 날짜를 현재 시간과 비교할 수 있습니다. 여기

    내가이 문제를 해결할 수있는 방법은 다음과 같습니다

     // Variable to contain cached object. Make sure it name it better... 
        var x = null; 
    
        function ajaxLoad(page, url, type, variety,category, widgetClickedId) 
        { 
         /* 
         * Checking the clicked id is the same as passed in one 
         * TODO refactor so the clicked id only ever gets passed 
         */ 
         if(widgetId != widgetClickedId && widgetClickedId != undefined) 
         { 
          return false; 
         } 
    
         var website = $('#button-print'+widgetId).data("website");   
         var page = $('#button-print'+widgetId).data("page"); 
    
         // Creating a new date and adding an hour to it. 
         var d = new Date(); 
         d.setHours(d.getHours() + 1); 
    
         // Test to ensure x is not null and it's not older than an hour old 
         if (x != null && Date.parse(x.date) < d) { 
          // Do your stuff with the cached file here 
         } 
         else { 
          $.ajax({ 
           // set up your ajax request 
           ... 
           success: function (productchoice) { 
            // Make sure to cache the object 
            x.file = productchoice; 
            x.date = new Date(); 
    
            // Do your stuff with the cached file here 
           } 
          }); 
         } 
        } 
    

    코드를 반복하지 않도록하기 위해 별도의 기능에 정상 "성공"작업을 넣어 도움이 될 것입니다. 이 경우 productchoice 변수 (예 : x.file['brand' + widgetId]) 대신 캐시 된 변수 (예제 ​​코드에서 x)를 사용합니다.

    관련 문제