Im는 C#의 제네릭에 익숙하지 않으며 내 프로그램의 다른 부분에서 모델 객체를 요청할 수있는 저장소를 만들려고합니다. 아이디어는 내 캐시 클래스에 개체가있는 경우 날짜가 확인되고 개체가 10 분 이상 경과하지 않은 경우 반환합니다. 10 분이 지난 경우 온라인으로 서버에서 업데이트 된 모델을 다운로드합니다. 개체를 다운로드하여 반환합니다.개체를 캐시하는 클래스를 만들려면 어떻게해야합니까?
그러나 내 개체를 DateTime과 페어링하여 모든 항목을 일반화하고 있습니다.
// model
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
Person p = new Person();
Cache c = new Cache();
p = c.Get<Person>(p);
}
}
public class Cache
{
struct DatedObject<T>
{
public DateTime Time { get; set; }
public T Obj { get; set; }
}
List<DatedObject<T>> objects;
public Cache()
{
objects = new List<DatedObject<T>>();
}
public T Get<T>(T obj)
{
bool found = false;
// search to see if the object is stored
foreach(var elem in objects)
if(elem.ToString().Equals(obj.ToString()))
{
// the object is found
found = true;
// check to see if it is fresh
TimeSpan sp = DateTime.Now - elem.Time;
if(sp.TotalMinutes <= 10)
return elem;
}
// object was not found or out of date
// download object from server
var ret = JsonConvert.DeserializeObject<T>("DOWNLOADED JSON STRING");
if(found)
{
// redate the object and replace it in list
foreach(var elem in objects)
if(elem.Obj.ToString().Equals(obj.ToString()))
{
elem.Obj = ret;
elem.Time = DateTime.Now;
}
}
else
{
// add the object to the list
objects.Add(new DatedObject<T>() { Time = DateTime.Now, Obj = ret });
}
return ret;
}
}
개체를 페어링 할 때의 문제/오류에 대한 자세한 내용을 제공 할 수 있습니까? 아마도 'DatedObject'의 구조 대신 클래스를 시도해 볼 수 있습니다. 자신 만의 클래스 객체를 만들지 않으려면 Tuple – Rex
[Akavache] (https://github.com/github/Akavache)를 살펴볼 가치가 있습니다. 으로. –