コード例 #1
0
        public virtual async Task RemoveCacheAsync(params string[] cacheKeys)
        {
            var cacheManager = await CachingUtils.GetCacheManagerAsync(Redis);

            if (cacheKeys == null)
            {
                return;
            }

            foreach (var cacheKey in cacheKeys)
            {
                cacheManager.Remove(cacheKey);
            }
        }
コード例 #2
0
        public static async Task <int> SumAsync(IDatabase database, string tableName, IRedis redis, string columnName, Query query = null)
        {
            var xQuery = NewQuery(tableName, query);

            xQuery.AsSum(columnName);
            var compileInfo = await CompileAsync(database, tableName, redis, xQuery);

            if (compileInfo.Caching != null && compileInfo.Caching.Action == CachingAction.Get)
            {
                var cacheManager = await CachingUtils.GetCacheManagerAsync(redis);

                return(await cacheManager.GetOrCreateAsync(compileInfo.Caching.CacheKey,
                                                           async() => await _SumAsync(database, compileInfo)
                                                           ));
            }

            return(await _SumAsync(database, compileInfo));
        }
コード例 #3
0
        public static async Task <bool> ExistsAsync(IDatabase database, string tableName, IRedis redis, Query query = null)
        {
            var xQuery = NewQuery(tableName, query);

            xQuery.ClearComponent("select").SelectRaw("COUNT(1)").ClearComponent("order");
            var compileInfo = await CompileAsync(database, tableName, redis, xQuery);

            if (compileInfo.Caching != null && compileInfo.Caching.Action == CachingAction.Get)
            {
                var cacheManager = await CachingUtils.GetCacheManagerAsync(redis);

                return(await cacheManager.GetOrCreateAsync(compileInfo.Caching.CacheKey,
                                                           async() => await _ExistsAsync(database, compileInfo)
                                                           ));
            }

            return(await _ExistsAsync(database, compileInfo));
        }
コード例 #4
0
        public static async Task <int> CountAsync(IDatabase database, string tableName, IRedis redis, Query query = null)
        {
            var xQuery = NewQuery(tableName, query);

            xQuery.ClearComponent("order").AsCount();
            var compileInfo = await CompileAsync(database, tableName, redis, xQuery);

            if (compileInfo.Caching != null && compileInfo.Caching.Action == CachingAction.Get)
            {
                var cacheManager = await CachingUtils.GetCacheManagerAsync(redis);

                var value = await cacheManager.GetOrCreateAsync(
                    compileInfo.Caching.CacheKey,
                    async() => (await _CountAsync(database, compileInfo)).ToString()
                    );

                return(Utilities.ToInt(value));
            }

            return(await _CountAsync(database, compileInfo));
        }
コード例 #5
0
 public virtual async Task <ICacheManager <object> > GetCacheManagerAsync()
 {
     return(await CachingUtils.GetCacheManagerAsync(Redis));
 }
コード例 #6
0
        private static async Task <CompileInfo> CompileAsync(IDatabase database, string tableName, IRedis redis, Query query)
        {
            var method = query.Method;

            if (method == "update")
            {
                query.Method = "select";
            }

            string sql;
            Dictionary <string, object> namedBindings;

            var compiler = DbUtils.GetCompiler(database.DatabaseType, database.ConnectionString);
            var compiled = compiler.Compile(query);

            if (method == "update")
            {
                var bindings = new List <object>();

                var setList    = new List <string>();
                var components = query.GetComponents("update");
                components.Add(new BasicCondition
                {
                    Column = nameof(Entity.LastModifiedDate),
                    Value  = DateTime.Now
                });
                foreach (var clause in components)
                {
                    if (clause is RawCondition raw)
                    {
                        var set = compiler.WrapIdentifiers(raw.Expression);
                        if (setList.Contains(set, StringComparer.OrdinalIgnoreCase))
                        {
                            continue;
                        }
                        setList.Add(set);
                        if (raw.Bindings != null)
                        {
                            bindings.AddRange(raw.Bindings);
                        }
                    }
                    else if (clause is BasicCondition basic)
                    {
                        var set = compiler.Wrap(basic.Column) + " = ?";
                        if (setList.Contains(set, StringComparer.OrdinalIgnoreCase))
                        {
                            continue;
                        }
                        setList.Add(set);
                        bindings.Add(basic.Value);
                    }
                }
                bindings.AddRange(compiled.Bindings);

                var result = new SqlResult
                {
                    Query = query
                };
                var where = compiler.CompileWheres(result);
                sql       = $"UPDATE {tableName} SET { string.Join(", ", setList)} {where}";

                //sql = Helper.ExpandParameters(sql, "?", bindings.ToArray());
                sql = Helper.ReplaceAll(sql, "?", i => "@p" + i);

                namedBindings = Helper.Flatten(bindings).Select((v, i) => new { i, v })
                                .ToDictionary(x => "@p" + x.i, x => x.v);
            }
            else
            {
                sql           = compiled.Sql;
                namedBindings = compiled.NamedBindings;
            }

            var compileInfo = new CompileInfo
            {
                Sql           = sql,
                NamedBindings = namedBindings
            };

            var cacheList = query.GetComponents <CachingCondition>("cache");

            if (cacheList != null && cacheList.Count > 0)
            {
                var cacheManager = await CachingUtils.GetCacheManagerAsync(redis);

                foreach (var caching in cacheList)
                {
                    if (caching.Action == CachingAction.Remove && caching.CacheKeysToRemove != null)
                    {
                        foreach (var cacheKey in caching.CacheKeysToRemove)
                        {
                            cacheManager.Remove(cacheKey);
                        }
                    }
                    else if (caching.Action == CachingAction.Get)
                    {
                        compileInfo.Caching = caching;
                    }
                }
            }

            return(compileInfo);
        }