2020年1月31日 星期五

[C#] 非同步觀念紀錄

1.偏向處理IO bound(讀取記憶體/硬碟密集),但CPU Loading較低的工作,
  如:讀文件、資料庫、網路要求資料(webservice)
 
2.是並行處理 concurrent, 與平行處理不同

3.一般建議任何方法(method)超過50ms才能完成,建議改非同步

4.4~5條thread就很夠處理一般程式(ex.賽車換輪胎範例)

5.非同步目的是提高執行效能與回應速度

6.thread safey : 多執行序環境下也可以確保得到預期結果

7.Critical Section: 任意一段程式碼,不允許多執行緒同時執行這段程式碼 ex.lock

8.Deadlock: 當兩個執行緒都嘗試鎖定另一個執行緒已經鎖定的資源時,就會發生死結,導致兩個執行緒都不能繼續執行


9.Context Switch: thread 記錄當下狀態,並換成其他thread(是會消耗成本)

10.非同步進程 : APM, EAP, TPL, TAP

11.blocking thread, 建議不用 .result wait waitall .thread join等會讓執行等待的方式, 改用WhenAll(tasks),await

12.不要混用同步與非同步方法

13.正常情況,不要使用 async void MethodAsync(){}, 改用回傳Task才能捕捉異常與執行結果

14.await Task.Delay 替代 Thread.Sleep

2020年1月16日 星期四

[MVC] 自訂Authentication

複習一下 :

    public class CustomAuthenticationFilter : IAuthenticationFilter
    {
        public void OnAuthentication(AuthenticationContext filterContext)
        {
            if (filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), inherit: true)
                || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), inherit: true))
            {
                return;
            }

            if (filterContext.Principal.Identity.IsAuthenticated && filterContext.Principal.Identity is FormsIdentity)
            {
             
            }
            else
            {
                filterContext.Result = new HttpUnauthorizedResult();
            }
        }

        public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
        {
            if (filterContext.Result == null || filterContext.Result is HttpUnauthorizedResult)
            {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary
            {
                {"controller","Default"},
                {"action","Logon"},
                {"returnUrl",filterContext.HttpContext.Request.RawUrl }
            });
            }
            //or do something , add challenge to response

        }
    }


參考:https://dotblogs.com.tw/libtong/2017/10/03/105527

2020年1月12日 星期日

[SQL] 學習 IN和EXISTS用法的差別


引用:https://www.jianshu.com/p/f212527d76ff


引用 :https://read01.com/zh-tw/ggKA5P.html#.Xhq8usgkuUk

exist會針對子查詢的表使用索引.
 not exist會對主子查詢都會使用索引.
 in與子查詢一起使用的時候,只能針對主查詢使用索引.
 not in則不會使用任何索引. 注意,一直以來認為exists比in效率高的說法是不準確的


紀錄一下