110,536
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// Redis缓存机制(适用于静态数据)
/// </summary>
/// <param name="context"></param>
/// <param name="t">类名</param>
/// <param name="mth">方法名</param>
/// <param name="key">设置Redis中的键名</param>
/// <param name="ts">设置参数类型</param>
/// <param name="o">设置参数名称</param>
/// <param name="type">设置数据库类型</param>
public void RedisCache(HttpContext context, Type t, string mth, string key, Type[] ts, object[] o, DataBaseType type)
{
string json = String.Empty;
if (RedisBase.Item_Exist(key))//判断Redis中,key是否存在;若存在,直接从redis取值,不存在则执行数据库访问方法,这种机制减缓了数据库的压力,提升了系统性能.
{
json = RedisBase.GetValue(key);//根据key获取value
}
else
{
object className = Activator.CreateInstance(t);//反射方法
MethodInfo mInfo = null;
object obj = null;
if (ts != null && o != null)//执行带有多种参数的方法
{
mInfo = t.GetMethod(mth, ts);
obj = mInfo.Invoke(className, o);
}
else//执行无参数的方法
{
mInfo = t.GetMethod(mth);
obj = mInfo.Invoke(className, null);
}
switch (type)//根据数据库类型处理字符串,因富基融通建库时使用的是西欧字符集.
{
case DataBaseType.Oracle:
json = Convert8859P1ToGB2312(JsonConvert.SerializeObject(obj));
break;
case DataBaseType.SQL_Server:
json = JsonConvert.SerializeObject(obj);
break;
}
RedisBase.Item_Set<string>(key, json, 120);//根据key设置value
}
context.Response.Write(json);
context.Response.End();
}
public delegate string JsonHandler<T>(T t);//我定义了委托,就不知道如何使用了
public string SendJson_SQL<T>(T t)
{
return JsonConvert.SerializeObject(t);
}
public string SendJson_Ora<T>(T t)
{
return Convert8859P1ToGB2312(JsonConvert.SerializeObject(t));
}
FatherHandler fth=new FatherHandler();
fth.RedisCache(context, d.GetType(), "GetShop", "ShopInfo", null,null,XXX.MsSql);
用什么方法,可以实现呢
fth.RedisCache2(context, d.GetType(), "GetShop", "ShopInfo", ????);
public delegate string JsonHandler<T>(T t);
调用的时候
public void RedisCache1(HttpContext context, Type type, string mth, string key, Type[] ts, object[] o, JsonHandler<object> t)
是不是没意义?必须要用到Action FuncFunc<string> jsonFunc
//改为
Func<object,string> jsonFunc
//然后方法传递
jsonFunc(obj);
(obj) =>
{
return Convert8859P1ToGB2312(JsonConvert.SerializeObject(obj));
});
/// <summary>
/// Redis缓存机制(适用于静态数据)
/// </summary>
/// <param name="context"></param>
/// <param name="t">类名</param>
/// <param name="mth">方法名</param>
/// <param name="key">设置Redis中的键名</param>
/// <param name="ts">设置参数类型</param>
/// <param name="o">设置参数名称</param>
/// <param name="type">设置数据库类型</param>
private void RedisCache(HttpContext context, Type t, string mth, string key, Type[] ts, object[] o, Func<string> jsonFunc)
{
string json = String.Empty;
if (RedisBase.Item_Exist(key))//判断Redis中,key是否存在;若存在,直接从redis取值,不存在则执行数据库访问方法,这种机制减缓了数据库的压力,提升了系统性能.
{
json = RedisBase.GetValue(key);//根据key获取value
}
else
{
object className = Activator.CreateInstance(t);//反射方法
MethodInfo mInfo = null;
object obj = null;
if (ts != null && o != null)//执行带有多种参数的方法
{
mInfo = t.GetMethod(mth, ts);
obj = mInfo.Invoke(className, o);
}
else//执行无参数的方法
{
mInfo = t.GetMethod(mth);
obj = mInfo.Invoke(className, null);
}
json = jsonFunc();
RedisBase.Item_Set<string>(key, json, 120);//根据key设置value
}
context.Response.Write(json);
context.Response.End();
}
public void RedisCacheForOracle(HttpContext context, Type t, string mth, string key, Type[] ts, object[] o)
{
RedisCache(context, t, mth, key, ts, o, () =>
{
return Convert8859P1ToGB2312(JsonConvert.SerializeObject(obj));
});
}
public void RedisCacheForMsSql(HttpContext context, Type t, string mth, string key, Type[] ts, object[] o)
{
RedisCache(context, t, mth, key, ts, o, () =>
{
return JsonConvert.SerializeObject(obj);
});
}