Esempio n. 1
0
        /// <summary>
        /// 异步更新秘钥
        /// </summary>
        /// <param name="session"></param>
        /// <param name="secretVersion"></param>
        private void AsynUpdateSecret(string session, Nullable <Int64> secretVersion)
        {
            string cacheKey = GenerateSecretKey(session, secretVersion);

            lock (AsynQueueKeyLock)
            {
                // 不需要重复提交秘钥请求
                if (asynQueueKey.ContainsKey(cacheKey))
                {
                    return;
                }
                SecretContext secretContext = GetSecret(session, GenerateSecretKey(session, secretVersion));
                if (secretContext != null && secretContext.IsValid())
                {
                    return;
                }

                asynQueueKey.Add(cacheKey, EmptyObject);
            }


            WaitCallback secretApiCallback = (state) =>
            {
                try
                {
                    CallSecretApi(session, secretVersion);
                }
                catch (Exception e)
                {
                    Log.Error(string.Format("asyn update secret error: {0}", e.Message));
                }
                finally
                {
                    lock (AsynQueueKeyLock)
                    {
                        asynQueueKey.Remove(cacheKey);
                    }
                }
            };

            try
            {
                ThreadPool.QueueUserWorkItem(secretApiCallback);
            }
            catch (Exception e)
            {
                lock (AsynQueueKeyLock)
                {
                    asynQueueKey.Remove(cacheKey);
                }
                Log.Error(string.Format("add QueueUserWorkItem error: {0}", e.Message));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 获取秘钥
        /// </summary>
        /// <param name="session"></param>
        /// <param name="secretVersion"></param>
        /// <returns></returns>
        public SecretContext GetSecret(string session, Nullable <Int64> secretVersion)
        {
            SecretContext secretContext = GetSecret(session, GenerateSecretKey(session, secretVersion));

            if (secretContext != null)
            {
                if (secretContext.IsValid())
                {
                    return(secretContext);
                }

                if (secretContext.IsMaxValid())
                {
                    // 异步更新秘钥
                    AsynUpdateSecret(session, secretVersion);
                    return(secretContext);
                }

                string cacheKey = GenerateSecretKey(session, secretVersion);
                lock (CacheLock)
                {
                    if (session != null)
                    {
                        AppUserSecretCache.Remove(cacheKey);
                    }
                    else
                    {
                        AppSecretCache.Remove(cacheKey);
                    }
                }
                // 同步调用获取秘钥
                return(CallSecretApi(session, secretVersion));
            }
            else
            {
                // 同步调用获取秘钥
                return(CallSecretApi(session, secretVersion));
            }
        }