CacheItem.cs 850 Bytes
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(); }
        }
    }
}