using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Data.SqlClient;
using System.Configuration;

namespace BookDemo.QA
{
    public class CatalogCache : BaseCache<List<QACatalog>>
    {
        public const string CACHE_KEY = "QACatalog";
        public CatalogCache()
            : base(CACHE_KEY)
        {
            base._timeout = 1000;
        }

        public override List<QACatalog> Update()
        {
            //设置缓存依赖参数
            System.Web.Caching.SqlCacheDependency sqlCacheDependency =
                new System.Web.Caching.SqlCacheDependency(
                    "BookDemo",//对应Web.config中sqlCacheDependency\databases参数中的name
                    "QACatalog"//对应数据库表的名称
                    );
            
            BookDemoEntities ctx = new BookDemoEntities();
            List<QACatalog> catalogs = (from c in ctx.QACatalog select c).ToList();//从数据库获取目录列表

            base.SetData(catalogs, base._timeout, sqlCacheDependency, null);
            /*TODO:如果整个系统对目录的缓存同步要求很严密那么这里应当
              同时更新QuestionAndAnswersCache等内容，这样的同步更新同样适用于用户缓存等处*/
            return catalogs;
        }
    }
}
