나는 내가 (내가 두 변수 jsonDataCache
및 jsonData
을 가지고 있지만 내가 예를 들어 하나, jsonData
을 갖고 싶어) 그런 식으로 채우는하고 익명 형식의 변수가 있습니다익명 형식을 어떻게 정의 할 수 있습니까?
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (from b in myData
select new
{
//do things
}).ToArray()
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
을하지만 난 처음이 변수를 정의하려면 내가 그것을 채우기 전에, 기본적으로 나는 다음과 같이하고 싶다.
var jsonData = null;
//check if jsonData in cache and if it is return Json(jsonData, JsonRequestBehavior.AllowGet);
jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (from b in myData
select new
{
//do things
}).ToArray()
};
//put jsonData in cache by key
return Json(jsonData, JsonRequestBehavior.AllowGet);
어떻게 할 수 있을까?
내가 캐시를 소개하기 때문에 그 이유를 알고 싶습니다. 그래서 캐시에서 확인하는 것보다 변수를 먼저 정의해야합니다. 그렇지 않으면 위의 것을 수행 할 것입니다. 현재 두 변수를 사용했는데 bot 하나만 사용하려고합니다. 여기
내가 현재 수행하는 방법입니다 :
public virtual JsonResult GetSomething(int id, int type)
{
string keyPrefix = GetKeyPrefix(id, type);
var jsonDataCache = CacheManager.Get<object>(keyPrefix);
if(jsonDataCache != null)
return Json(jsonDataCache, JsonRequestBehavior.AllowGet);
var myData = GetFromDataase();
var jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (from b in myData
select new
{
//do things
}).ToArray()
};
CacheManager.Set<object>(keyPrefix, jsonData);
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
UPDATE :
public virtual JsonResult GetSomething(int id, int type)
{
string keyPrefix = GetKeyPrefix(id, type);
var jsonData = CacheManager.Get<object>(keyPrefix);
if(jsonData != null)
return Json(jsonData , JsonRequestBehavior.AllowGet);
var myData = GetFromDataase();
jsonData = new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (from b in myData
select new
{
//do things
}).ToArray()
};
CacheManager.Set<object>(keyPrefix, jsonData);
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
: 내가 그런 것을해야한다고 생각하는 모든 당신의 도움이 후, 잘하면 갈 올바른 방법입니다
* 왜 * 원하십니까? 이 질문은 아마 –
질문이 업데이트 된 질문을 업데이 트 – jason