Ejemplo n.º 1
0
        /**
         * @ 创建单个对象的SQL类型的缓存策略,该方法适用于单个字段类型对象
         * @ T 要创建的缓存对象类型,仅限单个类型
         * @ key 缓存的键
         * @ dbConnectionStr 数据库连接字符串
         * @ cmdText sql语句,必须包含架构信息,并且不能使用“*”号代替要查询的字段
         * @ offset 缓存过期时间
         * @ sourceChange 数据源更改通知事件
         * @ priority 缓存逐出的优先级
         * */
        public static CacheItem CreateSQLCache(string key, string dbConnectionStr, string cmdText, DateTimeOffset offset, EventHandler sourceChange, CacheItemPriority priority = CacheItemPriority.Default)
        {
            SqlCacheExpiration sqlCache  = GetSqlCache(key, dbConnectionStr, cmdText, offset, sourceChange);
            CacheItem          cacheItem = null;
            CacheItemPolicy    policy    = null;

            cacheItem = sqlCache.CreateSQLCacheScalar(key, cmdText, offset, out policy, priority);

            Cache.Set(cacheItem, policy);
            return(cacheItem);
        }
Ejemplo n.º 2
0
        /**
         * @ 创建SQL类型的缓存策略
         * @ T 要创建的缓存对象类型,仅限单个类型
         * @ key 缓存的键
         * @ dbConnectionStr 数据库连接字符串
         * @ cmdText sql语句,必须包含架构信息,并且不能使用“*”号代替要查询的字段
         * @ offset 缓存过期时间
         * @ sourceChange 数据源更改通知事件
         * @ cacheType 缓存类型
         * @ priority 缓存逐出的优先级
         * */
        public static CacheItem CreateSQLCache <T>(string key, string dbConnectionStr, string cmdText, DateTimeOffset offset, EventHandler sourceChange, SqlCacheOption cacheType, CacheItemPriority priority = CacheItemPriority.Default) where T : class, new()
        {
            SqlCacheExpiration sqlCache  = GetSqlCache(key, dbConnectionStr, cmdText, offset, sourceChange);
            CacheItem          cacheItem = null;
            CacheItemPolicy    policy    = null;

            switch (cacheType)
            {
            case SqlCacheOption.Single:
                cacheItem = sqlCache.CreateSQLCacheSingle <T>(key, cmdText, offset, out policy, priority);
                break;

            case SqlCacheOption.List:
                cacheItem = sqlCache.CreateSQLCacheList <T>(key, cmdText, offset, out policy, priority);
                break;
            }
            Cache.Set(cacheItem, policy);
            return(cacheItem);
        }
Ejemplo n.º 3
0
        /**
         * @ 创建 SqlCacheExpiration 对象
         * @ T 要创建的缓存对象类型,仅限单个类型
         * @ key 缓存的键
         * @ dbConnectionStr 数据库连接字符串
         * @ cmdText sql语句,必须包含架构信息,并且不能使用“*”号代替要查询的字段
         * @ offset 缓存过期时间
         * @ sourceChange 数据源更改通知事件
         * */

        private static SqlCacheExpiration GetSqlCache(string key, string dbConnectionStr, string cmdText, DateTimeOffset offset, EventHandler sourceChange)
        {
            if (string.IsNullOrEmpty(dbConnectionStr))
            {
                throw new ArgumentNullException("dbConnectionStr不能为空");
            }
            if (string.IsNullOrEmpty(cmdText))
            {
                throw new ArgumentNullException("cmdText不能为空");
            }
            SqlCacheExpiration sqlCache = new SqlCacheExpiration(dbConnectionStr);

            sqlCache.SourceChange += delegate(object sender, EventArgs e)
            {
                if (sourceChange != null)
                {
                    sourceChange(sender, e);
                }
            };

            return(sqlCache);
        }