Performance on site collection with multiple groups (over 5000)

Aug 14, 2013 at 12:25 PM
Hello!
I have a performance on site collection with multiple groups (over 5000).
So I have changed GroupExistsInSiteCollection method with this quick code with caching:
public bool GroupExistsInSiteCollection(SPWeb web, string groupName)
        {
            SPGroup group = null;
            if (HttpContext.Current != null)
            {
                var cachedGroupId = HttpContext.Current.Cache[groupName];
                if (cachedGroupId != null)
                {
                    try
                    {
                        web.SiteGroups.GetByID((int)cachedGroupId);
                        return true;
                    }
                    catch (SPException)
                    {
                        HttpContext.Current.Cache.Remove(groupName);
                        return false;
                    }
                }
                else
                {
                    try
                    {
                        group = web.SiteGroups[groupName];
                        HttpContext.Current.Cache[groupName] = group.ID;
                        return true;
                    }
                    catch (SPException)
                    {
                        return false;
                    }
                }
            }

            try
            {
                group = web.SiteGroups[groupName];
                return true;
            }
            catch (SPException)
            {
                return false;
            }         
        }
It's bad, but still better then default iteration through all site groups