캐시 기반 레이어를 만들 수 있습니다. 메모리에만 유지되거나 메모리와 디스크 모두에 유지되도록하려는 경우 고려해야합니다.
저는 다중 레벨 (메모리, 디스크, 네트워크) 캐시 시스템을 제시합니다. 나는이 아키텍처를 성공과 함께 사용했다. 싱글 톤 클래스 및 C# 이벤트와 함께 작동합니다. 처리기에는 많은 구독자 이있을 수 있습니다. 따라서 UI 화면은 데이터를 사용할 수있을 때 수신 할 수 있으며 요청 당 여러 번 콜백 할 수 있습니다. 디자인은 가능한 한 빨리 데이터를 반환하지만 (메모리에서 먼저) 데이터를 새로 고치기 위해 디스크 및 네트워크 호출을 수행합니다. UI가 메모리에서 데이터를 신속하게 다시 호출 한 다음 네트워크 요청이 완료되면 다시 호출하기 때문에 UI가 여러 번 호출 될 수 있습니다. 스레드 안전성을 돕기 위해 잠금을 사용하거나 ConcurrentDictionary 또는 ConcurrentQueue와 같은 스레드 안전 콜렉션을 사용할 수 있습니다. 고려해야 할 더 많은 첨단 사례가 있지만 이것은 이벤트가있는 다중 레벨 캐시 시스템의 기본 아키텍처입니다.
// pseudo code
class CacheHelper
{
// todo: create singleton
RequestData()
{
if (data in memory)
{
// this quickly gets the app some data
Fetch data from dictionary/list/etc
NotifySubscribers(this, data)
}
if (data in disk)
{
// this gets data persisted after user force closes app then reopens
Fetch data from sqlite, file storage, etc
NotifySubscribers(this, data)
copy data from disk to memory
}
// always request data from server too to keep it fresh
RequestLatestDataFromServer(); // you can put a cap on this to limit network requests
}
HandleRequestLatestDataFromServerComplete(obj, args)
{
// this is the data returned from server
ALSO copy server result to memory and copy to disk
NotifySubscribers(this, data);
}
/// To use this in a UI class
ViewDidAppear()
{
CacheHelper.Instance.HandleRequestLatestDataFromServerComplete += ReceivedData;
}
ViewWillDisappear()
{
CacheHelper.Instance.HandleRequestLatestDataFromServerComplete -= ReceivedData;
}
void ReceivedData(Object obj, EventArgs args)
{
// update your UI
// This may get called a few times, for each level of cache retrieved
}
또한 캐시 시스템에 대해 Akavache를 볼 수도 있습니다 (https://github.com/akavache/Akavache). – jaybers