如何使用Ajax保存缓存文件?

时间:2013-06-05 15:52:18

标签: javascript ajax json jquery

我有一个javascript / ajax函数,用于查找托管在另一台服务器上的json文件。我需要我的函数来执行以下操作:

  1. 从外部服务器获取json文件
  2. 将json文件保存在自己的本地服务器上
  3. 检查json文件是否超过1小时
  4. 如果不超过1小时,则使用该数据
  5. 如果超过1小时 - 从外部服务器重新下载并覆盖本地版本
  6. 目前,我的函数每次调用时都会从外部服务器获取数据。我需要添加这个缓存功能,但我不确定如何删除它。

    有人可以提供任何建议吗?

    这是我的代码:

           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; 
                    }               
                });    
    

    提前致谢!

1 个答案:

答案 0 :(得分: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
                }
            });
        }
     }

将正常的“成功”操作放在单独的函数中以避免重复代码是有益的。如果您这样做,则使用缓存变量(示例代码中为x),而不是使用productchoice变量(即x.file['brand' + widgetId])。

相关问题