用法:

   //声名一个数据集合
var listString = new List<string>() { "a", "b", "c" };
//缓存key
string key = "cokey"; //获取实例
var cookiesManager = CookiesManager<List<string>>.GetInstance(); //插入缓存
cookiesManager.Add(key, listString, cookiesManager.Minutes * 30);//过期30分钟
//add有其它重载 上面是最基本的 //获取
List<string> cookiesList = cookiesManager[key]; //其它方法
cookiesManager.ContainsKey(key); cookiesManager.Remove(key);//删除 cookiesManager.RemoveAll(c => c.Contains("sales_"));//删除key包含sales_的cookies cookiesManager.GetAllKey();//获取所有key

  

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web; namespace SyntacticSugar
{ /// <summary>
/// ** 描述:cookies操作类
/// ** 创始时间:2015-6-9
/// ** 修改时间:-
/// ** 作者:sunkaixuan
/// ** 使用说明:
/// </summary>
/// <typeparam name="V">值</typeparam>
public class CookiesManager<V> : IHttpStorageObject<V>
{ #region 全局变量
private static CookiesManager<V> _instance = null;
private static readonly object _instanceLock = new object();
#endregion /// <summary>
/// 获取实例 (单例模式)
/// </summary>
/// <returns></returns>
public static CookiesManager<V> GetInstance()
{
if (_instance == null)
lock (_instanceLock)
if (_instance == null)
_instance = new CookiesManager<V>();
return _instance;
} /// <summary>
/// 添加cookies ,注意value最大4K (默认1天)
/// </summary>
/// <param name="key">key</param>
/// <param name="value">value</param>
public override void Add(string key, V value)
{
Add(key, value, Day);
}
/// <summary>
/// 添加cookies ,注意value最大4K
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="cookiesDurationInSeconds">有效时间单位秒</param>
public void Add(string key, V value, int cookiesDurationInSeconds)
{
HttpResponse 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 cookie, string setValue, HttpResponse response)
{
if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
cookie.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.Script.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];
cookie.Expires = DateTime.Now.AddDays(-1);
if (cookie != null)
{
if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
cookie.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 removeList = 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 = 60;
public int Hour = 60 * 60;
public int Day = 60 * 60 * 24;
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; }
}
}

  

最新文章

  1. upload&amp;&amp;download
  2. Manacher算法
  3. 【1】第一次电话面试---上海EMC
  4. 如何在其他电脑上运行VS2005编译的DEBUG版应用程序
  5. Linux环境安装jdk
  6. Android Studio NDK 学习之接受Java传入的字符串
  7. TFS(Taobao File System)安装方法
  8. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
  9. ubuntu netbeans compile ygopro client with google protobuf lib
  10. 调试出不来 断点不起作用 调试技巧 MyEclipse进不了调试
  11. Win7 补丁装不上怎么办?
  12. yarn的调度器
  13. [图形学] 计算机图形学 with OpenGL开篇
  14. Ansible安装及配置
  15. 51NOD-1486 大大走格子
  16. Linux第二周作业
  17. 协议中UART的两种模式 【转】
  18. 第一个iOS程序:Hello iOS
  19. 查看系统资源使用情况:vmstat
  20. Linux设置程序开机启动-tomcat开机启动

热门文章

  1. Win7下VS2010使用“ASP.Net 3.5 Claims-aware Template”创建ClaimsAwareWebSite报&quot;HRESULT: 0x80041FEB&quot;错误的解决办法
  2. SFTP+OpenSSH+ChrootDirectory设置
  3. android获取本机的IP地址和mac物理地址
  4. Excel 二级下拉菜单
  5. JAVA常见错误处理方法 和 JVM内存结构
  6. How To Create a Personal Balance Sheet
  7. 高吞吐量的分布式发布订阅消息系统Kafka-- 管理工具 Kafka Manager
  8. java web项目启动时自动加载自定义properties文件
  9. VC++ 学习笔记(序):神一样的语言
  10. SQL Server 几种锁的区别