实现数据缓存(类似Cache功能)
来源:互联网 发布:npm 安装node sass 编辑:程序博客网 时间:2024/05/16 04:55
using System;
using System.Threading;
using System.Collections;
namespace MyCache
{
/// <summary>
/// 实现缓存
/// </summary>
public sealed class MyCache
{
private static Hashtable Hash = Hashtable.Synchronized(new Hashtable());
private static Thread thread;
static MyCache()
{
thread = new Thread(new ThreadStart(AutoRemoveData));
thread.Start();
}
~MyCache()
{
Hash.Clear();
Hash = null;
thread.Abort();
thread = null;
}
private static void AutoRemoveData()
{
while (true)
{
foreach(object Obj in new ArrayList(Hash.Keys))
{
//清除超时没有访问量的 讨论版所占内存
if (((Data)Hash[Obj]).time< System.DateTime.Now)
{
try
{
Remove(Obj.ToString());
}
catch{}
}
}
Thread.Sleep(1000);
}
}
public static object Get(string Key)
{
if (Hash[Key] == null)
return null;
else
return ((Data)Hash[Key]).Base;
}
//超过几分钟不访问:在每次访问的时候,更改到期时间(未加代码)
public static void Set(object BaseData, string Key, int CacheTime)
{
Set(BaseData, Key, DateTime.Now.AddMinutes(CacheTime), null);
}
public static void Set(object BaseData, string Key, int CacheTime, string CallBack)
{
Set(BaseData, Key, DateTime.Now.AddMinutes(CacheTime), CallBack);
}
public static void Set(object BaseData, string Key, DateTime RemoveTime)
{
Set(BaseData, Key, RemoveTime, null);
}
public static void Set(object BaseData, string Key, DateTime RemoveTime, string CallBack)
{
if (Hash[Key] != null)
Remove(Key);
Data d = new Data(BaseData,RemoveTime,CallBack);
Hash.Add(Key,d);
}
private static void Remove(string Key)
{
if (Hash[Key] == null)
return;
Data d = (Data)Hash[Key];
if (d.CallBack != null & d.CallBack !="" & d.Base.GetType().GetMethod(d.CallBack) != null)
{
try
{
d.Base.GetType().InvokeMember(d.CallBack,System.Reflection.BindingFlags.InvokeMethod, null,d.Base,null);
}
catch
{}
}
Hash.Remove(Key);
}
private sealed class Data
{
public DateTime time;
public object Base;
public string CallBack="";
public bool IsLast = false;
public Data(object data, DateTime RemoveTime, string CallBack)
{
this.Base = data;
this.time = RemoveTime;
this.CallBack = CallBack;
}
}
}
}
- 实现数据缓存(类似Cache功能)
- 使用shareperference实现缓存cache功能
- Webview离线功能(优先cache缓存+cache缓存管理)
- Webview离线功能(优先cache缓存+cache缓存管理)!
- Webview离线功能(优先cache缓存+cache缓存管理)
- Webview离线功能(优先cache缓存+cache缓存管理)!
- Webview离线功能(优先cache缓存+cache缓存管理)
- 实现cache缓存(Python)
- Android实现类似数据清理的功能
- HttpRuntime.Cache缓存数据
- cache 缓存 数据结构设计
- jquery cache(数据缓存)
- Cache清除缓存数据
- 8.5 数据缓存Cache
- django cache缓存数据
- 数据缓存cache中
- 【so easy~】 Webview离线功能(优先cache缓存+cache缓存管理)!
- Mysql 数据库缓存cache功能
- Refactoring Patterns (Forth Part)
- Refactoring Patterns (Fifth Part)
- c语言学习笔记一i++,i+=1等哪个更快
- Refactoring Patterns (Sixth Part)
- 送给电脑桌前的您
- 实现数据缓存(类似Cache功能)
- 十大恶心笑话-胆小者勿进
- 找这样的男人有安全感
- Delphi程序返回值
- 请问怎么处理ntext类型的数据
- 男生看见美女的四大经典表情
- Xmlhttp实现 静态刷新
- ::给lake2留言::
- 在Sql中对本周日期进行查询