CacheItem.cs
850 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
namespace Quiz.Utility.Cache
{
/// <summary>
/// 提供缓存项的基本包装。
/// </summary>
/// <typeparam name="T"></typeparam>
public sealed class CacheItem<T> : IDisposable
{
public CacheItem(CacheItemInitCallback<T> initcall)
{
this.initcall = initcall;
syncroot = new object();
}
~CacheItem()
{
this.Dispose();
}
public void Dispose()
{
initcall -= initcall;
this.cachedata = default(T);
}
CacheItemInitCallback<T> initcall;
T cachedata;
object syncroot;
public T CacheData { get { return this.cachedata; } }
public void InitCache()
{
lock (this.syncroot) { cachedata = initcall(); }
}
}
}