【问题标题】:Best way to keep/cach Dataset for reuse on a desktop application保留/缓存数据集以在桌面应用程序上重用的最佳方法
【发布时间】:2016-12-25 10:24:31
【问题描述】:

我有一个 web 服务,当他们打开应用程序时,它会向客户端桌面应用程序返回一个数据集,没有任何内容被发送回数据库。 桌面应用程序在其整个生命周期中都需要此数据集

什么是让这个数据集远离内存的最佳方法,比如在 ASP.net 中缓存?

是否有可能将其保留为 XML 并在应用程序查找数据时将其取回?

这是一个提供给客户端应用程序的产品列表,产品列表是从全局网络应用程序更新的,所以如果客户端需要最新数据,那么有办法从网络检索,但基本上只有在启动期间加载一次应用程序并继续使用它,但有 9,000 行我担心应用程序性能和内存,不确定 9,000 行数据集将消耗多少内存

示例代码将不胜感激

【问题讨论】:

  • 如果每次应用启动都发送,为什么需要保存?
  • 关于Best ways 的问题真的很难回答。尤其是在给出如此少的上下文的情况下。

标签: c# asp.net vb.net caching dataset


【解决方案1】:

您想使用 Cache Aside 模式。

基本上,它提供了一种众所周知的方式来将具体对象放入缓存中......带有过期策略(不要重新发明轮子,使用可用的)。

该模式说“给我缓存中的内容......如果它不存在,这里是去填充对象的真正方法”。

您将在瘦客户端上运行以下代码,在我有“新 ClaimsPrincipal”的地方,您将调用您的 Web 服务并获取您需要的数据。

https://msdn.microsoft.com/en-us/library/dn589799.aspx

https://blog.cdemi.io/design-patterns-cache-aside-pattern/

这是一个例子:

   public class PrincipalMemoryCacheAside // : IPrincipalCacheAside
    {
        public const string CacheKeyPrefix = "PrincipalMemoryCacheAsideKey";

        public ClaimsPrincipal GetTheClaimsPrincipal(string uniqueIdentifier)
        {
            string cacheKey = this.GetFullCacheKey(uniqueIdentifier);
            ClaimsPrincipal cachedOrFreshPrincipal = GetFromCache<ClaimsPrincipal>(
                cacheKey, 
                () =>
                {
                    ClaimsPrincipal returnPrinc = null;

                    /* You would go hit your web service here to populate your object */
                    ClaimsIdentity ci = new GenericIdentity(this.GetType().ToString());
                    ci.AddClaim(new Claim("MyType", "MyValue"));
                    returnPrinc  = new ClaimsPrincipal(ci);


                    return returnPrinc;
                });

            return cachedOrFreshPrincipal;
        }

        private TEntity GetFromCache<TEntity>(string key, Func<TEntity> valueFactory) where TEntity : class
        {

            ObjectCache cache = MemoryCache.Default;
            //// the lazy class provides lazy initializtion which will evaluate the valueFactory expression only if the item does not exist in cache
            var newValue = new Lazy<TEntity>(valueFactory);
            CacheItemPolicy policy = new CacheItemPolicy { SlidingExpiration = new TimeSpan(0, 60, 0), Priority = CacheItemPriority.NotRemovable };
            ////The line below returns existing item or adds the new value if it doesn't exist
            var value = cache.AddOrGetExisting(key, newValue, policy) as Lazy<TEntity>;
            return (value ?? newValue).Value; // Lazy<T> handles the locking itself
        }

        private string GetFullCacheKey(string uniqueIdentifier)
        {
            string returnValue = CacheKeyPrefix + uniqueIdentifier;
            return returnValue;
        }
    }

【讨论】:

    猜你喜欢
    • 2014-02-20
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多