Usage:
Fame a data set var liststring = new list<string> () {"A", "B", "C"}; Cache key String key = "Cokey"; Get instance var cookiesmanager = Cookiesmanager<list<string>>. GetInstance (); Insert Cache Cookiesmanager.add (key, liststring, Cookiesmanager.minutes * 30);//Expires 30 minutes //add has other overloads above is the most basic // Get list<string> cookieslist = Cookiesmanager[key]; Other methods Cookiesmanager.containskey (key); Cookiesmanager.remove (key);//delete Cookiesmanager.removeall (c = c.contains ("Sales_"));//delete key contains Sales_ The cookie cookiesmanager.getallkey ();//Get all keys
Code:
Using system;using system.collections.generic;using system.linq;using system.text;using System.Web;namespace syntacticsugar{//<summary>//* Description: Cookies operation class//* * Founding Date: 2015-6-9//* * Modified:-//* * Sunkaixu AN//* * Instructions for use://</summary>//<typeparam name= "V" > Value </typeparam> public class Cookiesman Ager<v>: ihttpstorageobject<v> {#region global variable private static cookiesmanager<v> _instan CE = null; private static readonly Object _instancelock = new Object (); #endregion///<summary>///For instance (Singleton mode)///</summary>/ <returns></returns> public static cookiesmanager<v> getinstance () { if (_instance = = null) lock (_instancelock) if (_instance = = null) _instance = new cookiesmanager<v> (); Return _instance; }///<summary>///Add cookies, note value Max 4K (default 1 days)///</summary>//<param Nam E= "key" >key</param>//<param name= "value" >value</param> public override void Add (Stri Ng key, V value) {ADD (key, value, day); }///<summary>///Add cookies, note value max 4K//</summary>//<param name= "key" ></param>//<param name= "value" ></param>//<param name= "Cookiesdurationinseconds "> Effective time unit seconds </param> public void Add (string key, V value, int cookiesdurationinseconds) {H Ttpresponse response = HttpContext.Current.Response; if (response! = null) {HttpCookie cookie = response. Cookies[key]; if (cookie = null) {if (typeof (V) = = typeof (String)) { String SetValue = value. ToString (); ADD (key, Cookiesdurationinseconds, Cookie, setValue, response); } else {System.Web.Script.Serialization.JavaScriptSerializer JSS = new System.Web.Script.Serialization.JavaScriptSerializer (); String setValue = JSS. Serialize (value); ADD (key, Cookiesdurationinseconds, Cookie, setValue, response); }}}} private void Add (string key, int cookiesdurationinseconds, HttpCookie Cook ie, string setValue, HttpResponse response) {if (!string. IsNullOrEmpty (key) && cookies. HasKeys) cookies. Values.set (key, SetValue); else if (!string. IsNullOrEmpty (setValue)) cookie. Value = SetValue; if (cookiesdurationinseconds > 0) cookie. Expires = DateTime.Now.AddSeconds (cOokiesdurationinseconds); Response. Setcookie (cookie); } public override bool ContainsKey (string key) {return Get (key)! = NULL; public override V Get (string key) {String value = String. Empty; if (context. Request.cookies[key]! = null) value = context. Request.cookies[key]. Value; if (typeof (v) = = typeof (String)) {return (v) Convert.changetype (value, typeof (V)); } else {System.Web.Script.Serialization.JavaScriptSerializer JSS = new System.Web.Sc Ript. Serialization.javascriptserializer (); Return JSS. Deserialize<v> (value); }} public override Ienumerable<string> Getallkey () {var allkeylist = context. Request.Cookies.AllKeys.ToList (); foreach (var key in allkeylist) {yield return key; } } public override void Remove (string key) {HttpRequest request = HttpContext.Current.Request ; if (Request! = null) {HttpCookie cookie = Request. Cookies[key]; Cookies. Expires = DateTime.Now.AddDays (-1); if (cookie = null) {if (!string. IsNullOrEmpty (key) && cookies. HasKeys) cookies. Values.remove (key); else request. Cookies.remove (key); }}} public override void RemoveAll () {foreach (var key in Getallkey ()) {Remove (key); }} public override void RemoveAll (func<string, bool> removeexpression) {var remove List = Getallkey (). Where (removeexpression). ToList (); foreach (var key in removelist) {Remove (key); } } public override V This[string key] {get {return get (key);} } }}
Using System;namespace syntacticsugar{public abstract class ihttpstorageobject<v> {public int Minutes =; public int Hour = *; public int day = * *; Public System.Web.HttpContext context = System.Web.HttpContext.Current; public abstract void Add (string key, V value); public abstract bool ContainsKey (string key); Public abstract V Get (string key); Public abstract global::system.collections.generic.ienumerable<string> Getallkey (); public abstract void Remove (string key); public abstract void RemoveAll (); public abstract void RemoveAll (func<string, bool> removeexpression); Public abstract V this[string key] {get;}} }
C # syntax sugar cookie Operation class ASP.