Example #1
0
        /// <summary>
        /// 通过配置获取源数据,缓存介入,并对缓存进行填充
        /// <code>baiyu 2014-12-19</code>
        /// </summary>
        /// <param name="isCache">if set to <c>true</c> [is cache].</param>
        /// <param name="invokeConfig">The invoke configuration.</param>
        /// <param name="prams">The prams.</param>
        /// <returns>System.Object.</returns>
        private static object invokeData(bool isCache, m_CacheConfig invokeConfig, params string[] prams)
        {
            if (invokeConfig == null) return null;
            if (prams == null || prams.Length == 0) return null;

            m_CacheConfig mcc = invokeConfig;

            if (!isCache)
            {
                var obj = invokeData(invokeConfig, prams);
                if (obj == null) return obj;
                if (com_TypeHelper.IsCollection(obj.GetType()))
                {
                    Dictionary<string, object> dic = new Dictionary<string, object>(prams.Length);
                    ArrayList al = new ArrayList(prams.Length);

                    var arrayObj = (IEnumerable)obj;
                    foreach (var item in arrayObj)
                    {
                        var t = item.GetType();
                        PropertyInfo[] pis = t.GetProperties();
                        if (pis == null) return null;
                        foreach (var pi in pis)
                        {
                            object[] cacheAttrs = pi.GetCustomAttributes(typeof(Attr_CachePrimaryKeyAttribute), false);
                            if (cacheAttrs == null || cacheAttrs.Length == 0) continue;

                            foreach (Attr_CachePrimaryKeyAttribute attr in cacheAttrs)
                            {
                                if (attr == null) return null;
                                string key = pi.GetValue(item, null).ToString();
                                dic.Add(key, item);
                            }
                        }
                    }
                    foreach (var item in prams)
                    {
                        if (dic.ContainsKey(item) && dic[item] != null)
                            al.Add(dic[item]);
                    }
                    return al.ToArray();
                }
                return obj;
            }
            else
            {
                string pre = mcc.Pre;

                Dictionary<string, string> keyDic = new Dictionary<string, string>(prams.Length);

                foreach (var p in prams)
                {
                    string cacheKey = string.Concat(pre, p);
                    if (!keyDic.ContainsKey(cacheKey))
                        keyDic.Add(cacheKey, p);
                }
                string[] cacheKeys = keyDic.Keys.ToArray();
                IDictionary<string, object> dic = srv_MemcacheCacheManager.Get(cacheKeys);
                if (dic == null || dic.Count == 0) return null;
                List<string> addObjKeys = new List<string>();
                foreach (KeyValuePair<string, object> item in dic)
                {
                    var obj = item.Value;
                    if (obj == null) addObjKeys.Add(keyDic[item.Key]);
                }

                if (addObjKeys.Count == 0)
                {
                    //因Memcache批量获取数据不排序,需要按照Key重新排序
                    ArrayList all = new ArrayList(prams.Length);
                    foreach (string key in cacheKeys)
                    {
                        if (dic.ContainsKey(key) && dic[key] != null)
                            all.Add(dic[key]);
                    }
                    return all.ToArray();
                }
                else
                {
                    var obj = invokeData(mcc, addObjKeys.ToArray());
                    if (obj != null)
                    {
                        Type objType = obj.GetType();

                        if (com_TypeHelper.IsCollection(objType))
                        {
                            var arryObj = (IEnumerable)obj;
                            foreach (var o in arryObj)
                            {
                                string cacheKey = string.Concat(pre, o.ToPrimaryKey());
                                srv_MemcacheCacheManager.Add(cacheKey, o, mcc.ExpTime);
                                dic[cacheKey] = o;
                            }
                        }
                        else
                        {
                            string cacheKey = string.Concat(pre, obj.ToPrimaryKey());
                            srv_MemcacheCacheManager.Add(cacheKey, obj, mcc.ExpTime);
                            dic[cacheKey] = obj;
                        }
                    }
                    //因Memcache批量获取数据不排序,需要按照Key重新排序
                    ArrayList all = new ArrayList(prams.Length);
                    foreach (string key in cacheKeys)
                    {
                        if (dic.ContainsKey(key) && dic[key] != null)
                            all.Add(dic[key]);
                    }
                    return all.ToArray();
                }
            }
        }
Example #2
0
        /// <summary>
        /// 根据配置获取源数据
        /// <code>baiyu 2014-12-19</code>
        /// </summary>
        /// <param name="invokeConfig">The invoke configuration.</param>
        /// <param name="prams">The prams.</param>
        /// <returns>System.Object.</returns>
        private static object invokeData(m_CacheConfig invokeConfig, params string[] prams)
        {
            if (invokeConfig == null) return null;
            if (prams == null || prams.Length == 0) return null;

            m_CacheConfig mcc = invokeConfig;

            string invokePrams = string.Join(",", prams);

            //Type type = Type.GetType(Assembly.CreateQualifiedName(mcc.AssemblyPath, mcc.ClassName));
            //MethodInfo mi = type.GetMethod(mcc.MethodName);
            //var instance = mi.IsStatic ? null : Activator.CreateInstance(type);

            ArrayList objMethod = com_GlobalDic.Pop(mcc.ID) as ArrayList;
            MethodInfo mi = objMethod[1] as MethodInfo;

            ParameterInfo pi = mi.GetParameters()[0];
            var invokeP = Convert.ChangeType(invokePrams, pi.ParameterType);
            object[] parameter = new object[] { invokeP };

            //com_DynamicMethodExecutor executor = new com_DynamicMethodExecutor(mi);
            //var obj = executor.Execute(instance, parameter);

            var obj = mi.Invoke(objMethod[0], new object[] { invokeP });

            return obj;
        }