第一个速度体验:两个架构和一个性能问题 [英] First velocity experiences: two architectural and a performance question

查看:83
本文介绍了第一个速度体验:两个架构和一个性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我使用Velocity启动了一个项目(启用了LOCAL缓存!!!)作为我的VS 2010 ASP.NET网站中缓存服务的基础。为了安全起见,我添加了一个用于启用/禁用缓存的应用程序设置:如果它处于启用状态,则使用Velocity缓存,如果它处于关闭状态,则执行"正常"的WCF调用。现在我有一些问题:

问题1:
当我创建一个带有区域的缓存时,我首先默认要删除该区域。删除某个区域的唯一方法似乎是将其嵌入到try finally构造中。

代码:

      内部静态无效CreateCache(字符串区域)
        {
           尝试
            {
                Global.CurrentCache.RemoveRegion(region);
            }
            catch
            {
                // HACK:需要施工,因为似乎没有检查某个地区是否存在
            }


            Global.CurrentCache.CreateRegion(region,false);


       这是正确的吗?是否有更好/更简洁的方法来删除区域?


问题2:
使用HttpApplicationState定义属性以获取当前缓存是否安全?所以我现在不想使用HttpContext.Current.Application而不是使用HttpContext.Current.Application)?

global.asax.cs中的代码:

        public static DataCache CurrentCache
        {
           获取
            {
                if(HttpContext.Current.Application == null)返回null;


              ;   DataCache dataCache;
               如果(HttpContext.Current.Application [c_DataCacheSessionKey]!= NULL)
                {
                   dataCache =(DataCache)(HttpContext.Current.Application [c_DataCacheSessionKey]);
                }
               其他
                {
                   VAR工厂=新DataCacheFactory();
                   数据高速缓存= factory.GetDefaultCache();
                    HttpContext.Current.Application [c_DataCacheSessionKey] =数据高速缓存;
                }


                return dataCache;
            }
        }


问题2:
我有一项服务,用于在包含物种的某些数据(生物物种,如蝴蝶的拉丁名称)中进行通配符搜索。现在我将WCF时间与速度时间进行比较。我注意到使用WCF服务呼叫大约快4到5倍,有时甚至快20倍。

以下是我使用的代码:

存储在缓存中的DataContract:

    [DataContract]
   公共类SpecieMinimalContract
    {
        [DataMember]
        public int Id {get;组; }


        [DataMember]
        public string Name {get;组; }


        [DataMember]
       公共布尔? IsType {get;组; }
    }

我正在执行的查询:


            if(Properties.Settings.Default.UseVelocityCache)
            {
                Wrogger.LogMessage(String.Format("{0} - 开始查询缓存...",logId));
            ;&NBSP;&NBSP;&NBSP;&NBSP; species = GetAllCachedSpecies.Where(specieLambda => specieLambda.Key.ToLower()。包含(wildCard.ToLower()))。选择(specieLambda => specieLambda.Value).Cast< SpecieMinimalContract>()。ToList();
                Wrogger.LogMessage(String.Format("{0} - 完成查询缓存(计数:{1},持续时间:{2})",logId,species.Count(),(DateTime.Now - startTime)));
            }

注意:
1。 Wrogger是一个用于记录的类的包装器
2。 GetAllCachedSpecies是一个包装器属性,用于获取正确的区域并确保始终加载缓存,代码:

       内部IEnumerable< KeyValuePair< string,object>> GetAllCachedSpecies
        {
           获取
            {
                if(!CacheUtilities.CheckCacheLoaded(c_SpecieCacheLoadedKey,c_SpecieCacheRegion))
                {
                   Wrogger.LogMessage(的String.Format( "{0} - 缓存出乎意料空,重装!", "属性[GetAllCachedSpecies]"));


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; PopulateCacheWithAllSpecies();
                }


                return Global.CurrentCache.GetObjectsInRegion(c_SpecieCacheRegion);
            }
        }






 

解决方案



1。如果区域不存在,RemoveRegion不会抛出异常。因此删除不存在的区域是好的。如果您在不存在的区域调用此函数,它将返回"false"。


2。关于第二个问题,更好的解决方案是创建一次DataCacheFactory并将其句柄保持在Application状态。并且每当在应用程序状态中找不到缓存时,您可以使用存储的缓存工厂句柄来获取该缓存。 DataCacheFactory是非常耗费资源的,只有在需要不同类型的高速缓存时,才应创建新的数据高速缓存工厂。使用本地缓存进行缓存。除了这个事实,你描述的模式很好。

3。无法真正评论缓存的性能比普通的Web服务慢。取决于Web服务的架构方式:)。我只能说很多工作已经用于提高缓存性能,但仍有很多工作正在进行中。到RTM的时候,我希望它能满足你的表现期望。


Hello I started a project using Velocity (with LOCAL cache enabled!!!) as a base for caching services in my VS 2010 ASP.NET website. To be on the safe side I added an application setting for enabling/disabling the cache: If it is on it uses Velocity caching and if it is off it does 'normal' WCF calls. Now I have some questions:

Question 1:
When I create a cache with a region, I first default want to remove the region. The only way to remove a region seems to be to embed it in a try finally construction.

Code:

       internal static void CreateCache(string region)
        {
            try
            {
                Global.CurrentCache.RemoveRegion(region);
            }
            catch
            {
                //HACK: Construction needed because there seems not to be a check for existance of a region
            }

            Global.CurrentCache.CreateRegion(region, false);

        }


Is this correct? Isn't there a better/neater way to remove a region?


Question 2:
Is it safe to define a property to get the current cache using the HttpApplicationState (so instead of using HttpContext.Current.Session I now like to use HttpContext.Current.Application)?

Code in global.asax.cs:

        public static DataCache CurrentCache
        {
            get
            {
                if (HttpContext.Current.Application == null) return null;

                DataCache dataCache;
                if (HttpContext.Current.Application[c_DataCacheSessionKey] != null)
                {
                    dataCache = (DataCache)(HttpContext.Current.Application[c_DataCacheSessionKey]);
                }
                else
                {
                    var factory = new DataCacheFactory();
                    dataCache = factory.GetDefaultCache();
                    HttpContext.Current.Application[c_DataCacheSessionKey] = dataCache;
                }

                return dataCache;
            }
        }

Question 2:
I have a service for doing wildcard searching in some data containing species (biological species, like latin names of butterflies). Now I compared WCF timings with Velocity timings. I noticed using WCF service calls is roughly 4 to 5 times faster and sometimes even 20 times faster.

Here is the code I am using:

The DataContract stored in the cache:

    [DataContract]
    public class SpecieMinimalContract
    {
        [DataMember]
        public int Id { get; set; }

        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public bool? IsType { get; set; }
    }

The query I am executing:


            if (Properties.Settings.Default.UseVelocityCache)
            {
                Wrogger.LogMessage(String.Format("{0} - Starting querying cache...", logId));
                species = GetAllCachedSpecies.Where(specieLambda => specieLambda.Key.ToLower().Contains(wildCard.ToLower())).Select(specieLambda => specieLambda.Value).Cast<SpecieMinimalContract>().ToList();
                Wrogger.LogMessage(String.Format("{0} - Finished querying cache (count: {1}, duration: {2})", logId, species.Count(), (DateTime.Now - startTime)));
            }


Note:
1. Wrogger is a Wrapper around a class used for logging
2. GetAllCachedSpecies is a wrapper property to get the correct region and to ensure the cache is always loaded, code:

        internal IEnumerable<KeyValuePair<string, object>> GetAllCachedSpecies
        {
            get
            {
                if (!CacheUtilities.CheckCacheLoaded(c_SpecieCacheLoadedKey, c_SpecieCacheRegion))
                {
                    Wrogger.LogMessage(String.Format("{0} - Cache unexpectedly empty, reloading!", "Property[GetAllCachedSpecies]"));

                    PopulateCacheWithAllSpecies();
                }

                return Global.CurrentCache.GetObjectsInRegion(c_SpecieCacheRegion);
            }
        }






 

解决方案



1. RemoveRegion does not throw exception if region doesn't exist. So removing on non-existent region is fine. It will return you a 'false' if you call this function on a non-existent region. 


2. About the second question, better solution will be to create DataCacheFactory once and  to keep its handle in Application state. and whenever a cache is not found in the application state, you can use stored cache factory handle to get that cache. DataCacheFactory is quite resource intensive, New data cache factory should only be created if a different kind of cache is required than e.g. cache with a local cache. Other than this fact, pattern you described is fine.

3. Can't really comment about the performance of cache being slower than a normal web-service. Depend upon how the web-service has been architected :). All i can say that a lot of work has gone into making cache performant and still a lot of work is going on. By the time RTM comes, I hope it will meet your performance-expectation.


这篇关于第一个速度体验:两个架构和一个性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆