コード例 #1
0
        protected TReturn CommandExecute <TReturn>(bool?enableCache, Func <TReturn> execQuery, string sql, object param, string cacheKey, TimeSpan?expire, int?pageIndex = default, int?pageSize = default)
        {
            if (!IsEnableCache(enableCache))
            {
                return(execQuery());
            }
            cacheKey = CacheKeyBuilder.Generate(sql, param, cacheKey, pageIndex, pageSize);
            Logger.LogDebug("Get query results from cache.");
            var cache = Cache.TryGet <TReturn>(cacheKey);

            if (cache.ExistKey)
            {
                Logger.LogDebug("Get value from cache successfully.");
                return(cache.Value);
            }
            Logger.LogDebug("The cache does not exist, acquire a lock, queue to query data from the database.");
            lock (Lock)
            {
                Logger.LogDebug("The lock has been acquired, try again to get the value from the cache.");
                var cacheResult = Cache.TryGet <TReturn>(cacheKey);
                if (cacheResult.ExistKey)
                {
                    Logger.LogDebug("Try again, get value from cache successfully.");
                    return(cacheResult.Value);
                }
                Logger.LogDebug("Try again, still fail to get the value from the cache, start to get the value from the data.");
                var result = execQuery();
                Cache.TrySet(cacheKey, result, expire ?? CacheConfiguration.Expire);
                Logger.LogDebug("Get value from data and write to cache.");
                return(result);
            }
        }
コード例 #2
0
        protected TReturn CommandExecute <TReturn>(bool?enableCache, Func <TReturn> execQuery, string sql, object param, string cacheKey, TimeSpan?expire, int?pageIndex = default, int?pageSize = default)
        {
            if (!IsEnableCache(enableCache))
            {
                return(execQuery());
            }
            cacheKey = CacheKeyBuilder.Generate(sql, param, cacheKey, pageIndex, pageSize);
            var cache = Cache.TryGet <TReturn>(cacheKey);

            if (cache.HasKey)
            {
                return(cache.Value);
            }
            var result = execQuery();

            Cache.TrySet(cacheKey, result, expire ?? CacheConfiguration.Expire);
            return(result);
        }