程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 如何使用PostSharp簡化緩存

如何使用PostSharp簡化緩存

編輯:關於C#
 

1.普通的緩存方法
CacheEnum.cs

/// 緩存Key
public enum CacheEnum
{
/// 時間緩存測試,DateTimeNow_{0},{0}為增加的小時數
DateTimeNow=30,
}
 

public static DateTime GetNowTime1(int a)
{
string mkey = string.Format("{0}_{1}", CacheEnum.DateTimeNow, a);
object dt = CacheMg.Get(mkey);
if (dt != null)
{
return (DateTime)dt;
}
dt = DateTime.Now.AddHours(a);
CacheMg.Add(mkey, dt, TimeSpan.FromSeconds((int)CacheEnum.DateTimeNow));
return (DateTime)dt;
}
 

2.使用postsharp
DCachedAttribute.cs
 

using System;
using PostSharp.Laos;
using com.jk39.DevTools;
namespace com.jk39.BLL.hz.yyk
{
[Serializable]
[global::System.AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = false)]
public class DCachedAttribute : OnMethodBoundaryAspect
{
private string _cacheKey;
private int _sec;

public DCachedAttribute(CacheEnum cacheKey,int sec)
{
this._cacheKey = cacheKey.ToString();
this._sec = sec;
}

public string CacheKey
{
get { return this._cacheKey; }
}
public int sec
{
get { return this._sec; }
}

public override void OnEntry(MethodExecutionEventArgs eventArgs)
{
Log4Helper.Debug("OnEntry..");
object[] pv = eventArgs.GetArguments();
string key = CacheKey;
for (int i = 0; i < pv.Length; i++)
{
key += "_" + pv[i].ToString();
}
object obj = CacheMg.Get(key) as object;
if (obj != null)
{
Log4Helper.Debug(string.Format("ReadCache:{0}",key));
eventArgs.ReturnValue = obj;
eventArgs.FlowBehavior = FlowBehavior.Return;
}

}
public override void OnSuccess(MethodExecutionEventArgs eventArgs)
{
Log4Helper.Debug("OnSuccess..");
object obj = eventArgs.ReturnValue;
if (obj != null)
{
object[] pv = eventArgs.GetArguments();
string key = CacheKey;
for (int i = 0; i < pv.Length; i++)
{
key +="_"+pv[i].ToString();
}
if (sec == 0)
{
CacheMg.Add(key, obj);
}
else
{
CacheMg.Add(key, obj,TimeSpan.FromSeconds(sec));
}
Log4Helper.Debug(string.Format("SaveCache:{0},{1}",key,sec));
}
}
}
}
 


[DCached(CacheEnum.DateTimeNow,(int)CacheEnum.DateTimeNow)]
public static DateTime GetNowTime(int a)
{
return DateTime.Now.AddHours(a);
}

3.結果
2008-12-11 11:14:46,246 [812] DEBUG - OnEntry..
2008-12-11 11:14:46,246 [812] DEBUG - OnSuccess..
2008-12-11 11:14:46,262 [812] DEBUG - SaveCache:DateTimeNow_1,30
2008-12-11 11:14:47,449 [812] DEBUG - OnEntry..
2008-12-11 11:14:47,465 [812] DEBUG - ReadCache:DateTimeNow_1
2008-12-11 11:14:47,777 [812] DEBUG - OnEntry..
2008-12-11 11:14:47,777 [812] DEBUG - ReadCache:DateTimeNow_1
2008-12-11 11:14:48,043 [812] DEBUG - OnEntry..
2008-12-11 11:14:48,043 [812] DEBUG - ReadCache:DateTimeNow_1
2008-12-11 11:14:48,262 [812] DEBUG - OnEntry..
2008-12-11 11:14:48,277 [812] DEBUG - ReadCache:DateTimeNow_1
2008-12-11 11:14:48,480 [812] DEBUG - OnEntry..
2008-12-11 11:14:48,480 [812] DEBUG - ReadCache:DateTimeNow_1
2008-12-11 11:14:48,699 [5516] DEBUG - OnEntry..
2008-12-11 11:14:48,699 [5516] DEBUG - ReadCache:DateTimeNow_1
2008-12-11 11:14:48,871 [5516] DEBUG - OnEntry..
2008-12-11 11:14:48,871 [5516] DEBUG - ReadCache:DateTimeNow_1
2008-12-11 11:14:49,090 [812] DEBUG - OnEntry..
2008-12-11 11:14:49,090 [812] DEBUG - ReadCache:DateTimeNow_1
2008-12-11 11:14:49,293 [812] DEBUG - OnEntry..
2008-12-11 11:14:49,293 [812] DEBUG - ReadCache:DateTimeNow_1
2008-12-11 11:14:49,512 [812] DEBUG - OnEntry..
2008-12-11 11:14:49,512 [812] DEBUG - ReadCache:DateTimeNow_1
2008-12-11 11:14:49,715 [812] DEBUG - OnEntry..
2008-12-11 11:14:49,715 [812] DEBUG - ReadCache:DateTimeNow_1

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved