示例#1
0
 public static void LoadFace(ILoadFace loadFace)
 {
     if (!LoadFormCache(loadFace))
     {
         Enqueue(loadFace);
     }
 }
示例#2
0
        public static void LoadFaceWithKnownUri(ILoadFace loadFace, string backupUri)
        {
            if (LoadFormCache(loadFace))
            {
                return;
            }

            Uri uri = new Uri(backupUri);


            Task.Factory.StartNew(() =>
            {
                FaceCache newFaceCache = new FaceCache(uri, loadFace);
                //newFaceCache.ApplyTo(loadFace);
                lock (FaceCacheDict)
                {
                    if (!FaceCacheDict.ContainsKey(loadFace.UserId))
                    {
                        FaceCacheDict.Add(loadFace.UserId, newFaceCache);
                    }
                }
                CleanCache();
                CachedCountChanged?.Invoke(FaceCacheDict.Count);
            });
        }
示例#3
0
        public static void Enqueue(ILoadFace loadFace)
        {
            Action action = new Action(() =>
            {
                LoadFaceAndWait(loadFace);
                CleanCache();
                CachedCountChanged?.Invoke(FaceCacheDict.Count);
            });

            AppendFaceUriRequest(action);
        }
示例#4
0
        public static bool LoadFormCache(ILoadFace loadFace)
        {
            if (FaceCacheDict.ContainsKey(loadFace.UserId))
            {
                FaceCache faceCache = FaceCacheDict[loadFace.UserId];
                faceCache.Expire = DateTime.UtcNow.AddMinutes(30);
                faceCache.ApplyTo(loadFace);

                return(true);
            }
            return(false);
        }
示例#5
0
 public void ApplyTo(ILoadFace loadFace)
 {
     if (IsLoading)
     {
         DownloadCompleted += (object sender, BitmapImage e) =>
         {
             loadFace.Dispatcher.Invoke(() =>
             {
                 loadFace.SetFace(e);
             });
         };
     }
     else
     {
         loadFace.Dispatcher.Invoke(() =>
         {
             loadFace.SetFace(FaceImage);
         });
     }
 }
示例#6
0
        private static void LoadFaceAndWait(ILoadFace loadFace)
        {
            if (FaceCacheDict.ContainsKey(loadFace.UserId))
            {
                FaceCache faceCache = FaceCacheDict[loadFace.UserId];
                faceCache.Expire = DateTime.UtcNow.AddMinutes(30);
                faceCache.ApplyTo(loadFace);
            }
            else
            {
                if (DateTime.Now < bolckUntil)
                {
                    return;
                }

                //DateTime startTime = DateTime.UtcNow;
                Uri uri;
                while (true)
                {
                    try
                    {
                        uri = LoadFaceUriFromApi(loadFace.UserId);
                        break;
                    }
                    catch (WebException ex)
                    {
                        if (ex.Status == WebExceptionStatus.Timeout)
                        {
                            Console.WriteLine(ex);
                            Thread.Sleep(1000);
                        }
                        else if (ex.Response != null && ((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.PreconditionFailed)
                        {
                            bolckUntil = DateTime.Now.AddMinutes(blockMinutes);
                            Console.WriteLine($"Face API has been blocked, expected until {bolckUntil}");
                            return;
                        }
                        else
                        {
                            Console.WriteLine(ex);
                            return;
                        }
                    }
                }

                if (uri != null)
                {
                    FaceCache faceCache = new FaceCache(uri, loadFace);
                    //faceCache.ApplyTo(loadFace);

                    lock (FaceCacheDict)
                    {
                        if (!FaceCacheDict.ContainsKey(loadFace.UserId))
                        {
                            FaceCacheDict.Add(loadFace.UserId, faceCache);
                        }
                    }
                }
                //DateTime endTime = DateTime.UtcNow;
                //TimeSpan timeSpan = endTime - startTime;
                //int waitTime = requestInterval - (int)timeSpan.TotalMilliseconds;
                //if (waitTime < 0)
                //    waitTime = 0;
                //Thread.Sleep(waitTime);
                Thread.Sleep(requestInterval);
            }
        }
示例#7
0
            public FaceCache(Uri uri, ILoadFace owner)
            {
                uri       = new Uri(uri.AbsoluteUri + "@24w_24h_1c_100q.jpg");
                IsLoading = true;
                Expire    = DateTime.UtcNow.AddMinutes(30);

                Task.Factory.StartNew(() =>
                {
                    MemoryStream memoryStream = null;
                    int retryCounter          = 0;
                    while (retryCounter < 3)
                    {
                        try
                        {
                            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
                            httpWebRequest.ServicePoint.ConnectionLimit = 16;
                            using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
                            {
                                using (Stream stream = httpWebResponse.GetResponseStream())
                                {
                                    memoryStream = new MemoryStream();
                                    stream.CopyTo(memoryStream);
                                    memoryStream.Position = 0;
                                }
                            }
                            break;
                        }
                        catch (WebException ex)
                        {
                            Console.WriteLine(ex);
                            if (memoryStream != null)
                            {
                                memoryStream.Dispose();
                                memoryStream = null;
                            }
                            if (ex.Response != null && ((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.NotFound)
                            {
                                break;
                            }
                            retryCounter++;
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex);
                            if (memoryStream != null)
                            {
                                memoryStream.Dispose();
                                memoryStream = null;
                            }
                            break;
                        }
                    }

                    if (memoryStream == null)
                    {
                        return;
                    }


                    owner.Dispatcher.Invoke(() =>
                    {
                        try
                        {
                            BitmapImage bitmapImage = new BitmapImage();

                            bitmapImage.BeginInit();
                            bitmapImage.StreamSource = memoryStream;
                            bitmapImage.CacheOption  = BitmapCacheOption.OnLoad;
                            bitmapImage.EndInit();

                            bitmapImage.Freeze();

                            FaceImage = bitmapImage;
                            memoryStream.Close();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex);
                            memoryStream.Close();
                        }
                    });

                    IsLoading = false;
                    DownloadCompleted?.Invoke(this, FaceImage);

                    ApplyTo(owner);
                });
            }