Example #1
0
        public async Task <TEntity> Get <TEntity>(string p_keyName, string p_keyVal)
            where TEntity : Entity
        {
            if (_primaryKey.Equals(p_keyName, StringComparison.OrdinalIgnoreCase))
            {
                var val = await _db.Value.StringGetAsync($"{_prefix}:{_primaryKey}:{p_keyVal}");

                if (val.HasValue)
                {
                    return(RpcKit.ParseString <TEntity>(val));
                }
            }
            else if (_otherKeys != null)
            {
                foreach (var item in _otherKeys)
                {
                    if (!item.Equals(p_keyName, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    // lua脚本:先查到主键值,再用主键值查询json
                    var result = await _db.Value.ScriptEvaluateAsync(_sha1LuaGet, new RedisKey[] { $"{_prefix}:{item}:{p_keyVal}" }, new RedisValue[] { $"{_prefix}:{_primaryKey}" });

                    if (!result.IsNull)
                    {
                        return(RpcKit.ParseString <TEntity>((string)result));
                    }
                    break;
                }
            }
            return(default);
Example #2
0
        public Task Cache <TEntity>(TEntity p_entity)
            where TEntity : Entity
        {
            Throw.IfNull(p_entity);
            string val = RpcKit.GetObjectString(p_entity);
            string id  = p_entity.Str(_primaryKey);

            // 只主键
            if (_otherKeys == null)
            {
                return(_db.Value.StringSetAsync($"{_prefix}:{_primaryKey}:{id}", val, _expiry));
            }

            // 多个缓存键
            var batch = _db.Value.CreateBatch();
            var tasks = new List <Task>();

            tasks.Add(batch.StringSetAsync($"{_prefix}:{_primaryKey}:{id}", val, _expiry));
            foreach (var item in _otherKeys)
            {
                // 缓存的值为主键值,不是实体的json!
                var itemVal = p_entity.Str(item);
                if (itemVal != string.Empty)
                {
                    tasks.Add(batch.StringSetAsync($"{_prefix}:{item}:{itemVal}", id, _expiry));
                }
            }
            batch.Execute();
            return(Task.WhenAll(tasks));
        }
Example #3
0
        async Task Response()
        {
            if (_result.Count == 0)
            {
                return;
            }

            _context.Response.BodyWriter.Write(RpcKit.GetObjectBytes(_result));
            await _context.Response.BodyWriter.FlushAsync();

            Log.Information("接收 {0} 个文件", _result.Count);
        }
Example #4
0
 /// <summary>
 /// 获取在线推送的内容
 /// </summary>
 /// <returns></returns>
 public string GetOnlineMsg()
 {
     return(RpcKit.GetCallString(MethodName, Params));
 }
Example #5
0
        /// <summary>
        /// 根据Api名称构造调用时的json串
        /// </summary>
        /// <param name="p_methodName">Api名称</param>
        /// <returns></returns>
        public static string GetMethodCall(string p_methodName)
        {
            ApiMethod sm = GetMethod(p_methodName);

            if (sm == null)
            {
                return(null);
            }

            List <object> funParams = new List <object>();

            foreach (ParameterInfo param in sm.Method.GetParameters())
            {
                Type type = param.ParameterType;
                if (type == typeof(string))
                {
                    funParams.Add("");
                }
                else if (type == typeof(Dict) || type == typeof(object))
                {
                    Dict dict = new Dict();
                    dict["键"] = "值";
                    funParams.Add(dict);
                }
                else if (type == typeof(Table))
                {
                    Table tbl = new Table {
                        { "列名1" }
                    };
                    tbl.NewRow("");
                    funParams.Add(tbl);
                }
                else if (type == typeof(bool))
                {
                    funParams.Add(true);
                }
                else if (type == typeof(Int32))
                {
                    funParams.Add(0);
                }
                else if (type == typeof(Int64))
                {
                    funParams.Add(0L);
                }
                else if (type == typeof(double))
                {
                    funParams.Add(0d);
                }
                else if (type == typeof(DateTime))
                {
                    funParams.Add(DateTime.Now);
                }

                else if (type == typeof(byte[]))
                {
                    funParams.Add(new byte[0]);
                }
                else if (type == typeof(string[]))
                {
                    funParams.Add(new string[1] {
                        ""
                    });
                }
                else if (type == typeof(List <string>))
                {
                    List <string> ls = new List <string>();
                    ls.Add("");
                    funParams.Add(ls);
                }
                else if (type == typeof(List <int>))
                {
                    funParams.Add(new List <int>()
                    {
                        1, 2, 3, 4
                    });
                }
                else if (type == typeof(List <double>))
                {
                    funParams.Add(new List <double>()
                    {
                        200.0d, 100d, 50.123d, 123.45d
                    });
                }
                else if (type.IsArray)
                {
                    funParams.Add(Activator.CreateInstance(type, 1));
                }
                else
                {
                    funParams.Add(Activator.CreateInstance(type));
                }
            }

            // 序列化Json串 RPC 调用请求,含有缩进
            return(RpcKit.GetCallString(p_methodName, funParams, true));
        }