예제 #1
0
        public Task <int> Delete(IWorkContext context, string eTag = null)
        {
            if (eTag.IsEmpty() && _cache.TryGetValue(out HeaderDoc <UserRoleDoc> value))
            {
                eTag = value?.ETag;
            }

            _cache.Clear();
            return(_roleRepository.Delete(context, ActorKey.VectorKey, eTag));
        }
예제 #2
0
 /// <summary>
 /// 读取缓存操作
 /// </summary>
 /// <returns></returns>
 public virtual T GetCache()
 {
     if (!CacheObject.TryGetValue(CacheKey, out T val))
     {
         // 如果缓存值不存在
         val = SetCache();
     }
     return(val);
 }
예제 #3
0
        /// <summary>
        /// Find certificate by thumbprint.  Certificates that have expired will not
        /// be returned and if "throwOnNotFound" is specified, an exception will be
        /// thrown.
        /// </summary>
        /// <param name="tag">tag</param>
        /// <param name="context">work context</param>
        /// <param name="throwOnNotFound">if true, throw exception if not found</param>
        /// <exception cref="ProgramExitException">Certificate is not found</exception>
        /// <returns>X509 certificate</returns>
        /// <exception cref="CertificateNotFoundException">when certificate valid certificate was not found</exception>
        public X509Certificate2 GetCertificate(IWorkContext context, bool?throwOnNotFound = null)
        {
            context = context.With(_tag);
            X509Certificate2 certificate;

            Exception?saveException = null;

            throwOnNotFound = throwOnNotFound ?? LocalCertificateKey.RequirePrivateKey;

            lock (_lock)
            {
                if (_cachedCertificate.TryGetValue(out certificate))
                {
                    return(certificate);
                }

                using (X509Store store = new X509Store(LocalCertificateKey.StoreName, LocalCertificateKey.StoreLocation))
                {
                    context.Telemetry.Verbose(context, $"Looking for certificate for {this}");

                    try
                    {
                        store.Open(OpenFlags.ReadOnly);
                        X509Certificate2Collection certificateList = store.Certificates.Find(X509FindType.FindByThumbprint, LocalCertificateKey.Thumbprint, validOnly: false);

                        if (certificateList?.Count != 0)
                        {
                            _cachedCertificate.Set(
                                certificateList
                                .OfType <X509Certificate2>()
                                .Where(x => !LocalCertificateKey.RequirePrivateKey || x.HasPrivateKey)
                                .Where(x => DateTime.Now <= x.NotAfter)
                                .FirstOrDefault()
                                );
                        }
                    }
                    catch (Exception ex)
                    {
                        context.Telemetry.Warning(context, $"Exception: {ex}");
                        _cachedCertificate.Clear();
                        saveException = ex;
                    }
                }

                context.Telemetry.Verbose(context, $"{(_cachedCertificate != null ? "Found" : "Not found")} certificate for {this}");

                if (!_cachedCertificate !.TryGetValue(out certificate) && throwOnNotFound == true)
                {
                    throw new CertificateNotFoundException($"Certificate not found: {LocalCertificateKey.ToString()}");
                }

                return(certificate);
            }
        }
예제 #4
0
        public void TryTest()
        {
            string item  = "Item to be cached";
            var    cache = new CacheObject <string>(TimeSpan.FromMilliseconds(100)).Set(item);

            cache.TryGetValue(out string value).Should().Be(true);
            value.Should().NotBeNullOrEmpty();
            value.Should().Be(item);

            Thread.Sleep(TimeSpan.FromMilliseconds(200));
            cache.TryGetValue(out value).Should().BeFalse();
            value.Should().BeNullOrEmpty();
        }
예제 #5
0
        /// <summary>
        /// find certificate by thumbprint
        /// </summary>
        /// <param name="tag">tag</param>
        /// <param name="context">work context</param>
        /// <param name="throwOnNotFound">if true, throw exception if not found</param>
        /// <exception cref="ProgramExitException">Certificate is not found</exception>
        /// <returns>X509 certificate</returns>
        public X509Certificate2 GetCertificate(IWorkContext context, bool?throwOnNotFound = null)
        {
            context = context.WithTag(_tag);
            Exception        saveException = null;
            X509Certificate2 certificate   = null;

            throwOnNotFound = throwOnNotFound ?? LocalCertificateKey.RequirePrivateKey;

            lock (_lock)
            {
                if (_cachedCertificate.TryGetValue(out certificate))
                {
                    return(certificate);
                }

                using (X509Store store = new X509Store(LocalCertificateKey.StoreName, LocalCertificateKey.StoreLocation))
                {
                    ToolboxEventSource.Log.Verbose(context, $"Looking for certificate for {this}");

                    try
                    {
                        store.Open(OpenFlags.ReadOnly);
                        X509Certificate2Collection certificateList = store.Certificates.Find(X509FindType.FindByThumbprint, LocalCertificateKey.Thumbprint, validOnly: false);

                        if (certificateList?.Count != 0)
                        {
                            certificate = certificateList
                                          .OfType <X509Certificate2>()
                                          .FirstOrDefault(x => !LocalCertificateKey.RequirePrivateKey || x.HasPrivateKey);

                            _cachedCertificate.Set(certificate);
                        }
                    }
                    catch (Exception ex)
                    {
                        ToolboxEventSource.Log.Warning(context, $"Exception: {ex}");
                        _cachedCertificate.Clear();
                        saveException = ex;
                    }
                }

                ToolboxEventSource.Log.Verbose(context, $"{(_cachedCertificate != null ? "Found" : "Not found")} certificate for {this}");
                if (certificate == null && throwOnNotFound == true)
                {
                    throw new ArgumentException($"Cannot find certificate for {this}", saveException);
                }

                return(certificate);
            }
        }
예제 #6
0
        private async Task <ArticleDirectory?> GetDirectory()
        {
            ArticleDirectory?subject;

            if (_cache.TryGetValue(out subject))
            {
                return(subject);
            }

            _logger.LogTrace($"{nameof(GetDirectory)} - fetching directory");

            subject = await _directoryClient.Get();

            _cache.Set(subject);

            return(subject);
        }