EF Cache Design and EF Cache Design

Source: Internet
Author: User

EF Cache Design and EF Cache Design

For the database cache of EF, EF itself also has, but cannot be flexibly controlled, and the entity object is no longer available after it is released, the same entity object cannot be used (the entity object does not support multithreading), it is basically released after use, and an EF extension framework also provides cache operations (source code: https://github.com/loresoft/EntityFramework.Extended ), general operations:

// The default cache var tasks = db. tasks. where (t => t. completeDate = null ). fromCache (); // set the cache time to 300 svar tasks = db. tasks. where (t => t. assignedId = myUserId & t. completeDate = null ). fromCache (CachePolicy. withDurationExpiration (TimeSpan. fromSeconds (300 )));

 

I have read how the source code of this extended framework parses the linq expression, mainly by inheriting the ExpressionVisitor (using the visitor mode), where some code is the most important:

            private Expression Evaluate(Expression e)            {                if (e.NodeType == ExpressionType.Constant)                {                    return e;                }                LambdaExpression lambda = Expression.Lambda(e);                Delegate fn = lambda.Compile();                return Expression.Constant(fn.DynamicInvoke(null), e.Type);            }

 

 

The above code calls DynamicInvoke to parse the variables in Where into actual values, such as t in the above Where. assignedId will be resolved to the actual value; it is good to use, but the performance analysis is performed, it is found that DynamicInvoke calls are very cost-effective (you can see the dynamic header and know the membership fee performance), so either use it hard, or re-parse the Expression, the code above is improved as follows:

Private Expression Evaluate (Expression e) {if (e. nodeType = ExpressionType. constant) {return e;} if (e is MemberExpression) {var expmember = ExpressionVisitorHelper. getMemberExpression (e); if (expmember! = Null) {return expmember;} return ExpressionVisitorHelper. getMethodCallExpression (e);} // ExpressionVisitorHelper class: internal static class ExpressionVisitorHelper {public static Expression GetMethodCallExpression (Expression e) {object val = null; if (e is MethodCallExpression) {val = DoToGetMethodCallVal (e as MethodCallExpression);} else if (e is UnaryExpression) {val = GetUnaryExpressionV Al (e as UnaryExpression);} if (val = null | val. GetType ()! = E. type) {LambdaExpression lambda = Expression. lambda (e); return Expression. constant (lambda. compile (). dynamicInvoke (null), e. type);} else {return Expression. constant (val, e. type) ;}} public static Expression GetMemberExpression (Expression exp) {var val = GetMemberExpVal (exp); if (val! = Null) {return Expression. constant (val, exp. type);} return null;} static object GetUnaryExpressionVal (UnaryExpression exp) {var val = GetMemberExpVal (exp. operand); if (val = null) {val = GetConstantExpVal (exp. operand, null);} return val;} static object DoToGetMethodCallVal (MethodCallExpression expMethod) {// possible problem expMethod. the Object is a MethodCallExpression, for example, l. serverid. startsWith (model. nam E. trimEnd (). trimStart (). toLower () object val = null; if (expMethod. object is MethodCallExpression) {Expression tempExp = expMethod. object; MethodCallExpression tempExpMethod; var listMemberExp = new List <MethodCallExpression> (); while (tempExp is MethodCallExpression) {tempExpMethod = tempExp as MethodCallExpression; listMemberExp. add (tempExpMethod); tempExp = tempExpMethod. object;} // perform the first Met Hod call (TrimEnd): tempExpMethod = listMemberExp [listMemberExp. count-1]; val = GetMemberExpVal (tempExpMethod. object); val = GetMethodCallVal (tempExpMethod, val); // call other methods (TrimStart) for (int I = listMemberExp. count-2; I> = 0; I --) {val = GetMethodCallVal (listMemberExp [I], val) ;}// call the last Method (ToLower) val = GetMethodCallVal (expMethod, val);} else {val = GetMemberExpVal (expMethod. O Bject); val = GetMethodCallVal (expMethod, val);} return val;} static object GetMethodCallVal (MethodCallExpression expMethod, object val) {if (val! = Null) {switch (expMethod. method. name) {case "ToString": case "ToLower": case "ToUpper": {val = expMethod. method. invoke (val, null);} break; case "Trim": case "TrimStart": case "TrimEnd": {object [] objArray = null; if (expMethod. arguments! = Null & expMethod. arguments. count> 0) {NewArrayExpression arg = null; ConstantExpression expConst = null; objArray = new object [expMethod. arguments. count]; char [] valArray = null; for (int I = 0, j = 0; I <expMethod. arguments. count; I ++) {arg = expMethod. arguments [I] as NewArrayExpression; if (arg! = Null & arg. Expressions! = Null & arg. expressions. count> 0) {valArray = new char [arg. expressions. count]; for (j = 0; j <arg. expressions. count; j ++) {expConst = arg. expressions [j] as ConstantExpression; if (expConst! = Null) {valArray [j] = (char) expConst. value ;}} objArray [I] = valArray ;}} val = expMethod. method. invoke (val, objArray);} break; default: break;} return val;} static object GetMemberExpVal (Expression exp) {if (exp is MemberExpression) {var node = exp as MemberExpression; if (node. expression is MemberExpression) {MemberExpression lastMemberExp; List <MemberExpression> listMemberExp; var ConstExp = MemberExpToConstantExp (node. Expression, out lastMemberExp, out listMemberExp); var constVal = GetConstantExpVal (constExp, lastMemberExp. Member); if (constVal! = Null) {if (listMemberExp! = Null & listMemberExp. count> 0) {for (int I = listMemberExp. count-1; I> = 0; I --) {constVal = GetMemberVal (listMemberExp [I]. member, constVal);} return GetMemberVal (node. member, constVal);} else {return GetMemberVal (node. member, constVal) ;}} else if (node. expression is ConstantExpression) {return GetConstantExpVal (node. expression, node. member);} else if (node. expression = null ){// Get static member data return GetConstantExpVal (node. expression, node. member) ;}} return null;} static object GetConstantExpVal (Expression exp, MemberInfo member) {object val = null; if (exp is ConstantExpression) {val = (exp as ConstantExpression ). value; if (member! = Null) {val = GetMemberVal (member, val) ;}} else if (exp = null) {// get static member data val = GetMemberVal (member, member. name);} return val;} static object GetMemberVal (MemberInfo member, object obj) {object val = null; if (member is PropertyInfo) {val = (member as PropertyInfo ). getValue (obj);} else if (member is FieldInfo) {val = (member as FieldInfo ). getValue (obj);} return val;} // get the last Membe RExpression and ConstantExpression static Expression MemberExpToConstantExp (Expression exp, out MemberExpression lastMemberExp, out List <MemberExpression> listMemberExp) {lastMemberExp = null; listMemberExp = new List <MemberExpression> (); while (exp is MemberExpression) {if (lastMemberExp! = Null) {listMemberExp. Add (lastMemberExp);} lastMemberExp = exp as MemberExpression; exp = lastMemberExp. Expression;} return exp ;}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.