从Cache(Web API)获取数据

时间:2015-03-08 15:38:15

标签: jquery caching get

模特CPacket:

namespace QuoteAPI.Models
{
    public class CPacket
    {
        public string key { get; set; }
        public Value value { get; set; }
        public int duration { get; set; }
    }
    public class Value
    {
        public string code { get; set; }
        public List<List<double>> data { get; set; }
    }
}

我想从Cache获取数据。 数据:

[
  {
    "key": "key1",
    "value": {
      "code": "CTG",
      "data": [
        [
          1,
          12.7
        ],
        [
          2,
          3.12
        ]
      ]
    },
    "duration": 5
  },
  {
    "key": "key1",
    "value": {
      "code": "CTE",
      "data": [
        [
          1,
          12.8
        ],
        [
          2,
          5.12
        ]
      ]
    },
    "duration": 8
  }
]

我有功能保存缓存:

[HttpPost]
        public string Post([FromBody] string strPostData)
        {
            var model = JsonConvert.DeserializeObject<List<CPacket>>(strPostData);
            foreach (var item in model)
            {
                MemoryCache.Default.Add(item.key, item.value, DateTime.Now.AddMinutes(item.duration));
            } 
            return "OK";
        }

我想使用Jquery从Cache调用数据。现在我使用Array Key(我知道),并从控制器QuoteControllers发送密钥。我认为返回时返回(null)

[HttpPost]
        public string Post(string id)

        {
            return MemoryCache.Default.Get(id) as string;
            //return "value";
        }

我的jquery,并在console.log(data)返回null时出错。 :

$(document).ready(function () {
            var refreshId = setInterval(function () {
                var key = ['key1', 'key2'];
                $.each(key, function (indext, itemData) {
                    $.ajax({
                        type: "POST",
                        url: "api/Quote/" + itemData + "",
                        contentType: "application/x-www-form-urlencoded",
                        success: function (data) {
                            console.log(data);
                            var x = JSON.parse(data);
                            //var items = [];
                            //items.push({ key: itemData, value: data });
                            //console.log(items);
                        }
                    });
                })
            }, 5000);
        });

我想从缓存获取密钥(key1,key2),返回数据是json对象。 请帮帮我!非常感谢你!

0 个答案:

没有答案