private void AssertThread(ImageCacheInfo cacheInfo, int taskId)
 {
     if (cacheInfo.GetCurrentTaskId() != taskId)
     {
         throw new TaskCanceledException();
     }
 }
        /// <summary>キャッシュ情報を更新する</summary>
        /// <param name="userInfo">ユーザ情報</param>
        /// <param name="cacheInfo">キャッシュ情報</param>
        /// <returns>キャッシュ情報</returns>
        private ImageCacheInfo UpdateCache(IUserInfo userInfo, ImageCacheInfo cacheInfo)
        {
            if (cacheInfo.FileName != Path.GetFileName(userInfo.ProfileImageUrl))
            {
                this.SetImage(cacheInfo, userInfo);
            }

            return(cacheInfo);
        }
        /// <summary>キャッシュ情報を生成する</summary>
        /// <param name="userInfo">ユーザ情報</param>
        /// <returns>キャッシュ情報</returns>
        private ImageCacheInfo CreateCache(IUserInfo userInfo)
        {
            var cache = new ImageCacheInfo
            {
                FileName = Path.GetFileName(userInfo.ProfileImageUrl),
            };

            this.SetImage(cache, userInfo);

            return(cache);
        }
        /// <summary>キャッシュ情報に画像データを格納する</summary>
        /// <param name="cacheInfo"></param>
        /// <param name="userInfo"></param>
        private void SetImage(ImageCacheInfo cacheInfo, IUserInfo userInfo)
        {
            int taskId = cacheInfo.CreateTaskId();

            Task.Run(() => this.LoadImage(cacheInfo, userInfo))
            .ContinueWith(
                task => cacheInfo.Image = task.Result,
                CancellationToken.None,
                TaskContinuationOptions.OnlyOnRanToCompletion,
                TaskScheduler.FromCurrentSynchronizationContext());
        }
        /// <summary>キャッシュ情報から画像データを取得する</summary>
        /// <param name="cacheInfo">キャッシュ情報</param>
        /// <param name="userInfo">ユーザ情報</param>
        /// <returns>画像データ</returns>
        private BitmapImage LoadImage(ImageCacheInfo cacheInfo, IUserInfo userInfo)
        {
            int taskId = cacheInfo.GetCurrentTaskId();

            this.AssertThread(cacheInfo, taskId);
            var imageUrl = userInfo.ProfileImageUrl;

            if (string.IsNullOrEmpty(imageUrl))
            {
                return(null);
            }

            var imageFileName = Path.GetFileName(imageUrl);
            var @params       = CreateParameter(userInfo);

            this.AssertThread(cacheInfo, taskId);
            if (this.TryFindCache(@params, imageFileName, out var imageData))
            {
                this.AssertThread(cacheInfo, taskId);
                return(ImageUtil.CreateBitmapImage(new MemoryStream(imageData)));
            }

            this.AssertThread(cacheInfo, taskId);
            if (this.TryDownloadImage(userInfo, out var imageStream))
            {
                this.AssertThread(cacheInfo, taskId);
                //@params.Add("filename", imageFileName);
                //@params.Add("image_data", imageStream.ToArray());

                //this.WriteCache(@params);

                this.AssertThread(cacheInfo, taskId);

                return(ImageUtil.CreateBitmapImage(imageStream));
            }

            this.AssertThread(cacheInfo, taskId);

            return(null);
        }
        /// <summary>弱参照中のキャッシュ情報を更新する</summary>
        /// <param name="userInfo"></param>
        /// <param name="reference"></param>
        /// <returns></returns>
        private ImageCacheInfo UpdateCacheReference(IUserInfo userInfo, ImageCacheInfo reference)
        {
            this.UpdateCache(userInfo, reference);

            return(reference);
        }