Store cache data locally

I am developing a C # Winform application, it is a client and connects to a web service to receive data. The data returned by the webservice is a DataTable. The client will display it in the DataGridView.

My problem is that: the client will get more time to get all the data from the server (the web service is not local with the client). So I have to use a stream to get data. This is my model:

The client creates a stream to receive data -> the stream completes and passes events to the client -> client mapping data on a datagridview in the form.

However, when the user closes the form, the user can open this form at a different time, and the client must receive the data again. This solution will lead to a slow client.

So, I am thinking about cached data:

Client <--- get / add / edit / delete ---> Cached Data - get / add / change / delete ---> Server (web service)

Please give me some advice. Example: should cached data be developed in another application that is the same host with the client? Or cached data runs in the client. Please give me some methods to implement this solution.

If you have any examples, please give me.

Thank.

UPDATE: Hello everyone, maybe you are thinking about my problem so far. I just want to cache data in the life of the client. I think cache data should be kept in memory. And when the client wants to receive data, he will check the cache.

+3
source share
6

Enterprise Library . , , - , (, , , ..) .

EntLib 3.1, .NET 2.0. ( , ) EntLibs, .

+1

# 2.0, System.Web , ASP.NET:

using System.Web;
using System.Web.Caching;

Cache webCache;

webCache = HttpContext.Current.Cache;

// See if there a cached item already
cachedObject = webCache.Get("MyCacheItem");

if (cachedObject == null)
{
    // If there nothing in the cache, call the web service to get a new item
    webServiceResult = new Object();

    // Cache the web service result for five minutes
    webCache.Add("MyCacheItem", webServiceResult, null, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);
}
else
{
    // Item already in the cache - cast it to the right type
    webServiceResult = (object)cachedObject;
}

System.Web, .

.NET 4.0, System.Runtime.Caching. , System.Runtime.Caching, :

using System.Runtime.Caching;

MemoryCache cache;
object cachedObject;
object webServiceResult;

cache = new MemoryCache("StackOverflow");

cachedObject = cache.Get("MyCacheItem");

if (cachedObject == null)
{
    // Call the web service
    webServiceResult = new Object();

    cache.Add("MyCacheItem", webServiceResult, DateTime.Now.AddMinutes(5));
}
else
{
    webServiceResult = (object)cachedObject;
}

. -, , - , .

+5
+2

, , . , ( , ..).

:

, :

public class SampleDataSerializer
{
    public static void Deserialize<T>(out T data, Stream stm)
    {
        var xs = new XmlSerializer(typeof(T));
        data = (T)xs.Deserialize(stm);
    }

    public static void Serialize<T>(T data, Stream stm)
    {
        try
        {
            var xs = new XmlSerializer(typeof(T));
            xs.Serialize(stm, data);
        }
        catch (Exception e)
        {
            throw;
        }
    }
}

, , , Serialize Deserialize , (, XmlDocuments ..).

IsolStorage ( ):

public class SampleIsolatedStorageManager : IDisposable
{
    private string filename;
    private string directoryname;
    IsolatedStorageFile isf;

    public SampleIsolatedStorageManager()
    {
        filename = string.Empty;
        directoryname = string.Empty;

        // create an ISF scoped to domain user...
        isf = IsolatedStorageFile.GetStore(IsolatedStorageScope.User |
            IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain,
            typeof(System.Security.Policy.Url), typeof(System.Security.Policy.Url));
    }

    public void Save<T>(T parm)
    {
        using (IsolatedStorageFileStream stm = GetStreamByStoredType<T>(FileMode.Create))
        {
            SampleDataSerializer.Serialize<T>(parm, stm);
        }
    }

    public T Restore<T>() where T : new()
    {
        try
        {
            if (GetFileNameByType<T>().Length > 0)
            {
                T result = new T();

                using (IsolatedStorageFileStream stm = GetStreamByStoredType<T>(FileMode.Open))
                {
                    SampleDataSerializer.Deserialize<T>(out result, stm);
                }

                return result;
            }
            else
            {
                return default(T);
            }
        }
        catch
        {
            try
            {
                Clear<T>();
            }
            catch
            {
            }

            return default(T);
        }
    }

    public void Clear<T>()
    {
        if (isf.GetFileNames(GetFileNameByType<T>()).Length > 0)
        {
            isf.DeleteFile(GetFileNameByType<T>());
        }
    }

    private string GetFileNameByType<T>()
    {
        return typeof(T).Name + ".cache";
    }

    private IsolatedStorageFileStream GetStreamByStoredType<T>(FileMode mode)
    {
        var stm = new IsolatedStorageFileStream(GetFileNameByType<T>(), mode, isf);
        return stm;
    }

    #region IDisposable Members

    public void Dispose()
    {
        isf.Close();
    }
}

, :

using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;

:

var myClass = new MyClass();
myClass.name = "something";
using (var mgr = new SampleIsolatedStorageManager())
{
    mgr.Save<MyClass>(myClass);
}

. , :

using (var mgr = new SampleIsolatedStorageManager())
{
    mgr.Restore<MyClass>();
}

: , , . , . , .

!

+1
0

In our implementation, each row in the database has an updated time stamp. Each time our client application accesses the table, we select the last last updated timestamp from the cache and send this value to the server. The server responds with all lines with newer timestamps.

0
source

Source: https://habr.com/ru/post/1750089/


All Articles