private static int GetCountImmediate(IConfigurationSession session, OrganizationId orgId, string systemAddressListName)
        {
            AddressBookBase systemAddressList = SystemAddressListMemberCount.ReadSystemAddressListFromAD(session, orgId, systemAddressListName);
            SystemAddressListMemberCountCacheValue systemAddressListMemberCountCacheValue = new SystemAddressListMemberCountCacheValue(systemAddressList);

            return(systemAddressListMemberCountCacheValue.GetMemberCountImmediate(session));
        }
        private static int GetCount(IConfigurationSession configSession, OrganizationId organizationId, string systemAddressListName, int countQuota, bool useCache)
        {
            Organization organization = configSession.GetOrgContainer();

            if (organization == null || !object.Equals(organization.Identity, organizationId.ConfigurationUnit))
            {
                organization = configSession.Read <ExchangeConfigurationUnit>(organizationId.ConfigurationUnit);
                if (organization == null)
                {
                    throw new TenantOrgContainerNotFoundException(organizationId.ToString());
                }
            }
            if (!organization.IsAddressListPagingEnabled)
            {
                return(SystemAddressListMemberCount.GetBruteForceCountImmediate(configSession.DomainController, organizationId, systemAddressListName, countQuota));
            }
            if (useCache)
            {
                return(SystemAddressListMemberCount.GetCountFromCache(configSession, organizationId, systemAddressListName, countQuota));
            }
            return(SystemAddressListMemberCount.GetCountImmediate(configSession, organizationId, systemAddressListName));
        }
        private static int GetCountFromCache(IConfigurationSession session, OrganizationId orgId, string systemAddressListName, int quota)
        {
            int?num = null;
            SystemAddressListMemberCountCacheKey   key = new SystemAddressListMemberCountCacheKey(orgId, systemAddressListName);
            SystemAddressListMemberCountCacheValue systemAddressListMemberCountCacheValue = null;

            try
            {
                if (!SystemAddressListMemberCount.cachedAddressListLock.TryEnterReadLock(SystemAddressListMemberCount.readerLockTimeout))
                {
                    throw new TransientException(DirectoryStrings.ErrorTimeoutReadingSystemAddressListCache);
                }
                SystemAddressListMemberCount.memberCountCache.TryGetValue(key, out systemAddressListMemberCountCacheValue);
            }
            finally
            {
                try
                {
                    SystemAddressListMemberCount.cachedAddressListLock.ExitReadLock();
                }
                catch (SynchronizationLockException)
                {
                }
            }
            if (systemAddressListMemberCountCacheValue == null)
            {
                try
                {
                    if (!SystemAddressListMemberCount.cachedAddressListLock.TryEnterUpgradeableReadLock(SystemAddressListMemberCount.readerLockTimeout))
                    {
                        throw new TransientException(DirectoryStrings.ErrorTimeoutReadingSystemAddressListCache);
                    }
                    if (!SystemAddressListMemberCount.memberCountCache.TryGetValue(key, out systemAddressListMemberCountCacheValue))
                    {
                        AddressBookBase systemAddressList = SystemAddressListMemberCount.ReadSystemAddressListFromAD(session, orgId, systemAddressListName);
                        systemAddressListMemberCountCacheValue = new SystemAddressListMemberCountCacheValue(systemAddressList);
                        num = new int?(systemAddressListMemberCountCacheValue.InitializeMemberCount(session, ExDateTime.UtcNow, quota));
                        if (!SystemAddressListMemberCount.cachedAddressListLock.TryEnterWriteLock(SystemAddressListMemberCount.writerLockTimeout))
                        {
                            throw new TransientException(DirectoryStrings.ErrorTimeoutWritingSystemAddressListCache);
                        }
                        try
                        {
                            SystemAddressListMemberCount.memberCountCache.Add(key, systemAddressListMemberCountCacheValue);
                        }
                        finally
                        {
                            SystemAddressListMemberCount.cachedAddressListLock.ExitWriteLock();
                        }
                    }
                }
                finally
                {
                    try
                    {
                        SystemAddressListMemberCount.cachedAddressListLock.ExitUpgradeableReadLock();
                    }
                    catch (SynchronizationLockException)
                    {
                    }
                }
            }
            if (num == null)
            {
                num = new int?(systemAddressListMemberCountCacheValue.GetMemberCount(session, ExDateTime.UtcNow, quota));
            }
            return(num.Value);
        }
        internal static bool IsQuotaExceded(IConfigurationSession configSession, OrganizationId organizationId, string systemAddressListName, int countQuota)
        {
            int count = SystemAddressListMemberCount.GetCount(configSession, organizationId, systemAddressListName, countQuota, true);

            return(count >= countQuota);
        }
 internal static int GetCount(IConfigurationSession configSession, OrganizationId organizationId, string systemAddressListName, bool useCache)
 {
     return(SystemAddressListMemberCount.GetCount(configSession, organizationId, systemAddressListName, int.MaxValue, useCache));
 }