最近看到某公司某项目中用于保存多个城市信息到cookie中的方法,该方法的逻辑是按时间顺序记录最近访问过的三个城市的名字及id,逻辑包插入与含排重。插入与排重的代码如下:
获取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的读取次数。
看完了上面的代码,我们再来看看另外一块比较合乎规范的代码:
/// <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(">", ">"); name = name.Replace("<", "<"); name = name.Replace(" ", " "); name = name.Replace("\"", """); name = name.Replace("\'", "'"); 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当中,可以存储更多的信息,也更加健壮。在开发过程中要不断的学习,精益求精,否则即使工作时间再长,也只是增加了干活儿的熟练程度罢了,并不能获得真正的能力提高。
转载于:https://blog.51cto.com/8907792/1837185