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