不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
推荐一个.NET缓存类:BaseCache<T>
编辑:dnawo 日期:2011-11-25
这个类原本存在于一个外包项目上,是外包公司写的,感觉比较不错,于是把它提取了出来。
一、缓存基类BaseCache<T>
二、具体缓存类及其实体类
使用示例:
当存在多个具体的缓存类时,有时需要对缓存进行更新等,一个个去调用Clear方法比较麻烦,这时可以新建一个缓存管理类。
三、缓存管理类DataCacheTaker
使用示例:
一、缓存基类BaseCache<T>
复制内容到剪贴板
程序代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
namespace ConsoleApplication1
{
/// <summary>
/// 缓存基类
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class BaseCache<T>
{
string _tablename;
static Hashtable cache = new Hashtable();
/// <summary>
/// 构造函数
/// </summary>
/// <param name="cachetable"></param>
protected BaseCache(string cachetable)
{
_tablename = cachetable;
}
/// <summary>
/// 单条记录转为实体(抽象方法)
/// </summary>
/// <param name="idr"></param>
/// <returns></returns>
protected abstract T Build(IDataReader idr);
/// <summary>
/// 加载数据到缓存
/// </summary>
/// <returns></returns>
public bool LoadIntoCache()
{
try
{
List<T> dataforcache = new List<T>();
using (IDataReader idr = SqlHelper.ExecuteReader(string.Format("select * from {0} with(nolock)", _tablename)))
{
while (idr.Read())
{
T obj = Build(idr);
dataforcache.Add(obj);
}
}
cache.Add(_tablename, dataforcache);
return true;
}
catch (Exception ex)
{
throw ex;
}
return false;
}
/// <summary>
/// 读取缓存数据
/// </summary>
public List<T> Cache
{
get
{
if (!cache.ContainsKey(_tablename))
LoadIntoCache();
return (List<T>)cache[_tablename];
}
}
/// <summary>
/// 清空缓存数据
/// </summary>
/// <returns></returns>
public bool Clear()
{
try
{
if (cache.ContainsKey(_tablename))
cache.Remove(_tablename);
return true;
}
catch { }
return false;
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Data;
namespace ConsoleApplication1
{
/// <summary>
/// 缓存基类
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class BaseCache<T>
{
string _tablename;
static Hashtable cache = new Hashtable();
/// <summary>
/// 构造函数
/// </summary>
/// <param name="cachetable"></param>
protected BaseCache(string cachetable)
{
_tablename = cachetable;
}
/// <summary>
/// 单条记录转为实体(抽象方法)
/// </summary>
/// <param name="idr"></param>
/// <returns></returns>
protected abstract T Build(IDataReader idr);
/// <summary>
/// 加载数据到缓存
/// </summary>
/// <returns></returns>
public bool LoadIntoCache()
{
try
{
List<T> dataforcache = new List<T>();
using (IDataReader idr = SqlHelper.ExecuteReader(string.Format("select * from {0} with(nolock)", _tablename)))
{
while (idr.Read())
{
T obj = Build(idr);
dataforcache.Add(obj);
}
}
cache.Add(_tablename, dataforcache);
return true;
}
catch (Exception ex)
{
throw ex;
}
return false;
}
/// <summary>
/// 读取缓存数据
/// </summary>
public List<T> Cache
{
get
{
if (!cache.ContainsKey(_tablename))
LoadIntoCache();
return (List<T>)cache[_tablename];
}
}
/// <summary>
/// 清空缓存数据
/// </summary>
/// <returns></returns>
public bool Clear()
{
try
{
if (cache.ContainsKey(_tablename))
cache.Remove(_tablename);
return true;
}
catch { }
return false;
}
}
}
二、具体缓存类及其实体类
复制内容到剪贴板
程序代码

using System;
using System.Data;
namespace ConsoleApplication1
{
/// <summary>
/// 实体类
/// </summary>
public class CacheForTable1TBData
{
int _id = 0;
string _name = string.Empty;
public CacheForTable1TBData(int id, string name)
{
_id = id;
_name = name;
}
public int Id
{
get { return _id; }
}
public string Name
{
get { return _name; }
}
}
/// <summary>
/// 具体缓存类
/// </summary>
public class CacheForTable1TB : BaseCache<CacheForTable1TBData>
{
#region 构造函数
public CacheForTable1TB()
: base("Table1TB")
{
}
#endregion
#region 抽象方法实现
protected override CacheForTable1TBData Build(IDataReader idr)
{
int id = 0;
string name = string.Empty;
if (idr["Id"] != DBNull.Value)
id = Convert.ToInt32(idr["Id"]);
if (idr["Name"] != DBNull.Value)
name = idr["Name"].ToString();
return new CacheForTable1TBData(id, name);
}
#endregion
}
}
using System.Data;
namespace ConsoleApplication1
{
/// <summary>
/// 实体类
/// </summary>
public class CacheForTable1TBData
{
int _id = 0;
string _name = string.Empty;
public CacheForTable1TBData(int id, string name)
{
_id = id;
_name = name;
}
public int Id
{
get { return _id; }
}
public string Name
{
get { return _name; }
}
}
/// <summary>
/// 具体缓存类
/// </summary>
public class CacheForTable1TB : BaseCache<CacheForTable1TBData>
{
#region 构造函数
public CacheForTable1TB()
: base("Table1TB")
{
}
#endregion
#region 抽象方法实现
protected override CacheForTable1TBData Build(IDataReader idr)
{
int id = 0;
string name = string.Empty;
if (idr["Id"] != DBNull.Value)
id = Convert.ToInt32(idr["Id"]);
if (idr["Name"] != DBNull.Value)
name = idr["Name"].ToString();
return new CacheForTable1TBData(id, name);
}
#endregion
}
}
使用示例:
复制内容到剪贴板
程序代码

using System;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var query = (new CacheForTable1TB().Cache)
.Where<CacheForTable1TBData>(item => item.Id == 10000)
.FirstOrDefault<CacheForTable1TBData>();
Console.WriteLine(query.Name);
Console.ReadKey();
}
}
}
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var query = (new CacheForTable1TB().Cache)
.Where<CacheForTable1TBData>(item => item.Id == 10000)
.FirstOrDefault<CacheForTable1TBData>();
Console.WriteLine(query.Name);
Console.ReadKey();
}
}
}
当存在多个具体的缓存类时,有时需要对缓存进行更新等,一个个去调用Clear方法比较麻烦,这时可以新建一个缓存管理类。
三、缓存管理类DataCacheTaker
复制内容到剪贴板
程序代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
/// <summary>
/// 缓存管理类
/// </summary>
public class DataCacheTaker
{
/// <summary>
///
/// </summary>
public static List<CacheForTable1TBData> Table1TB
{
get { return (new CacheForTable1TB()).Cache; }
}
/// <summary>
///
/// </summary>
public static List<CacheForTable2TBData> Table2TB
{
get { return (new CacheForTable2TB()).Cache; }
}
/// <summary>
/// 更新缓存
/// </summary>
/// <returns></returns>
public static bool RefreshCache()
{
try
{
if ((new CacheForTable1TB()).Clear()) { var tmp = (new CacheForTable1TB()).Cache; }
if ((new CacheForTable2TB()).Clear()) { var tmp = (new CacheForTable2TB()).Cache; }
return true;
}
catch { }
return false;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
/// <summary>
/// 缓存管理类
/// </summary>
public class DataCacheTaker
{
/// <summary>
///
/// </summary>
public static List<CacheForTable1TBData> Table1TB
{
get { return (new CacheForTable1TB()).Cache; }
}
/// <summary>
///
/// </summary>
public static List<CacheForTable2TBData> Table2TB
{
get { return (new CacheForTable2TB()).Cache; }
}
/// <summary>
/// 更新缓存
/// </summary>
/// <returns></returns>
public static bool RefreshCache()
{
try
{
if ((new CacheForTable1TB()).Clear()) { var tmp = (new CacheForTable1TB()).Cache; }
if ((new CacheForTable2TB()).Clear()) { var tmp = (new CacheForTable2TB()).Cache; }
return true;
}
catch { }
return false;
}
}
}
使用示例:
复制内容到剪贴板
程序代码

using System;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataCacheTaker.RefreshCache();
var query = DataCacheTaker.Table1TB
.Where<CacheForTable1TBData>(item => item.Id == 10000)
.FirstOrDefault<CacheForTable1TBData>();
Console.WriteLine(query.Name);
Console.ReadKey();
}
}
}
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataCacheTaker.RefreshCache();
var query = DataCacheTaker.Table1TB
.Where<CacheForTable1TBData>(item => item.Id == 10000)
.FirstOrDefault<CacheForTable1TBData>();
Console.WriteLine(query.Name);
Console.ReadKey();
}
}
}






评论: 0 | 引用: 0 | 查看次数: 4916
发表评论
请登录后再发表评论!