cookie存儲對象信息


       最近看到某公司某項目中用於保存多個城市信息到cookie中的方法,該方法的邏輯是按時間順序記錄最近訪問過的三個城市的名字及id,邏輯包插入與含排重。插入與排重的代碼如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
                  獲取cityid與cityname
    
                 if  (cityid == Utility.TypeParse.ToInt(CookieHelper. get ( "fwid1" )))
                 {
                    
                 }
                 else  if  (cityid == Utility.TypeParse.ToInt(CookieHelper. get ( "fwid2" )))
                 {
                     //CookieHelper.set("fwname3", CookieHelper.get("fwname2"), 10);
                     CookieHelper. set ( "fwname2" , CookieHelper. get ( "fwname1" ), 10);
                     CookieHelper. set ( "fwname1" , HttpUtility.UrlEncode(cityname), 10);
 
                     //CookieHelper.set("fwid3", CookieHelper.get("fwid2"), 10);
                     CookieHelper. set ( "fwid2" , CookieHelper. get ( "fwid1" ), 10);
                     CookieHelper. set ( "fwid1" , cityid.ToString(), 10);
                 }
                 else  if  (cityid == Utility.TypeParse.ToInt(CookieHelper. get ( "fwid3" )))
                 {
                     CookieHelper. set ( "fwname3" , CookieHelper. get ( "fwname2" ), 10);
                     CookieHelper. set ( "fwname2" , CookieHelper. get ( "fwname1" ), 10);
                     CookieHelper. set ( "fwname1" , HttpUtility.UrlEncode(cityname), 10);
 
                     CookieHelper. set ( "fwid3" , CookieHelper. get ( "fwid2" ), 10);
                     CookieHelper. set ( "fwid2" , CookieHelper. get ( "fwid1" ), 10);
                     CookieHelper. set ( "fwid1" , cityid.ToString(), 10);
                 }
                 else
                 {
                     //沒有相等的
                     CookieHelper. set ( "fwname3" , CookieHelper. get ( "fwname2" ), 10);
                     CookieHelper. set ( "fwname2" , CookieHelper. get ( "fwname1" ), 10);
                     CookieHelper. set ( "fwname1" , HttpUtility.UrlEncode(cityname), 10);
 
                     CookieHelper. set ( "fwid3" , CookieHelper. get ( "fwid2" ), 10);
                     CookieHelper. set ( "fwid2" , CookieHelper. get ( "fwid1" ), 10);
                     CookieHelper. set ( "fwid1" , cityid.ToString(), 10);
                 }

   點評:對於存儲的中文信息需要編碼,否則在部分瀏覽器無法存儲,這是這段代碼可以肯定的地方。但對於同類信息用到了六個cookie,反復的存取降低了代碼的效率,最好存儲在一個cookie中。另外即使考慮到數據量不大采用上面的方法,對於單個cookie最好先獲取cookie中的值到一個變量當中,在需要讀取cookie值的地方利用該變量即可,這樣可以降低對cookie的讀取次數。

   看完了上面的代碼,我們再來看看另外一塊比較合乎規范的代碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    /// <summary>
        /// 添加一對名稱地址到cookie中
        /// </summary>
        /// <param name="name">地址名稱</param>
        /// <param name="url">地址url</param>
        public  static  void  AddKeyWord( string  name,  string  url,  string  detailName= "" , int  i = 0, string  shortDomain)
         {
             List<KeyWord> kwLists =  new  List<KeyWord>();
             
             if  ( string .IsNullOrEmpty(name) ||  string .IsNullOrEmpty(url)||name.Trim().ToLower()== "null" )
                 return ;
             HttpContext context = System.Web.HttpContext.Current;
             name = context.Server.UrlDecode(name);
             cookieName = baseCookieName + shortDomain;
             string  kw = ClientCookie.Get(cookieName);
             if  (name.IndexOf( "%" ) > -1 || name.IndexOf( "%" ) > -1)
             {
                 name = context.Server.UrlDecode(name);
                 if  (name.IndexOf( "%" ) > -1 || name.IndexOf( "%" ) > -1)
                     return ;
             }
             name = name.Replace( ">" "&gt;" );
             name = name.Replace( "<" "&lt;" );
             name = name.Replace( " " "&nbsp;" );
             name = name.Replace( "\"" "&quot;" );
             name = name.Replace( "\'" "&#39;" );
             name = name.Replace( "\\" "\\\\" ); //對斜線的轉義
             name = name.Replace( "\n" "\\n" );
             name = name.Replace( "\r" "\\r" );
             if  (! string .IsNullOrEmpty(kw))
             {
                 try
                 {
                     kwLists = JsonConvert.DeserializeObject<List<KeyWord>>(kw);
                 }
                 catch  (Exception ex)
                 {
                     LogerHelper.WriteMessegeError(kw,  "解析出錯" );
                 }
                 foreach  (KeyWord myKey  in  kwLists)
                 {
                     if  (name.Trim()==myKey.name.Trim()||myKey.url == url.Trim().ToLower())
                     {
                         return ;
                     }
                 }
                
                 if  (kwLists.Count >= 3)
                 {
                     kwLists.RemoveAt(0);
                 }
               
             }
 
             kwLists.Add( new  KeyWord() { name = name.Trim(), url = url.Trim(), sort = i, detailName = detailName });
       
                 ClientCookie.Set(cookieName,JsonConvert.SerializeObject(kwLists),3,shortDomain);
            
             }

     點評:上面的方法將一個序列化的對象集合存儲在cookie當中,可以存儲更多的信息,也更加健壯。在開發過程中要不斷的學習,精益求精,否則即使工作時間再長,也只是增加了干活兒的熟練程度罷了,並不能獲得真正的能力提高。

本文出自 “鯤鵬擊浪” 博客,請務必保留此出處http://8907792.blog.51cto.com/8897792/1837185


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM