Memcached and Asp.net mvc
Today I was hanging around on Google and found a lot of useful stuff about Memcached and .net. Currently, I am using System.Web.Cache to cache objects for my webpage stuff. It's good, but not enough for scalable since it's often remove my object when run on low memory. So I go on with Memcached instead. I found a lot of library which could help us develop faster. Here are some library/solution that I found:
http://ayende.com/Blog/archive/2008/06/06/Scratching-an-itch-NMemcached.aspx
http://www.codeproject.com/KB/aspnet/memcached_aspnet.aspx
http://maxi326.wordpress.com/2009/11/02/use-memcached-in-net-application-with-linq/
http://zvolkov.com/blog/post/2009/06/18/Using-MemCached-with-NHibernate.aspx
http://latebound.blogspot.com/2008/10/using-memcached-from-c.html
http://www.codeplex.com/memcachedproviders
http://code.google.com/p/beitmemcached/
http://sourceforge.net/projects/memcacheddotnet/
http://www.codeplex.com/EnyimMemcached/
and the list go on...
What is MemCached? To put this in simple term, it's a cache store / hash table, where you can store almost everything, like objects, on it. Basically, it's very useful since there are parts where you don't want to cache your whole webpage but you just want to cache your object which you finished process or pulled out from database. It can save you hundreds of ms query time, server loads, and process time, which is very very very useful.
There are some downside if you run Memcached on Windows, the latest update for Memcached for windows that i can found is 1.2.5 I found a latest version here: 1.4.4, and the lates for linux is 1.4.4 (at the time I write this post). There are some solution for this problem. By using another computer/server installed as linux, you can solely run Memcached for cache purpose on multiple server. If you are not having more than 1 server, then just fall back to use System.Web.Cache since it's quite good for production already. You can also try out Velocity, which currently on it's CTP and I am not sure about whether Microsoft going to charge users for this.
I suggest you use MemCached Provider, it's the simplest to implement memcached in asp.net mvc, the following web.config instruction provided by MemCached Provider
Open Web.config file and add the following section to configSections tag
<section name="cacheProvider" type="MemcachedProviders.Cache.CacheProviderSection, MemcachedProviders" allowDefinition="MachineToApplication" restartOnExternalChanges="true"/> <sectiongroup name="enyim.com"> <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" /> </sectiongroup> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
Add the following section to configure Enyim’s client to point to Memcached servers
<enyim .com> <memcached> <servers> <!-- put your own server(s) here--> <add address="127.0.0.1" port="11211" /> </servers> <socketpool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00" /> </memcached> </enyim>
Add the following section to configure Memcached Cache Provider. keySuffix attribute allows for adding suffix to cache provider keys in order to simulate namespaces.
<cacheprovider defaultProvider="MemcachedCacheProvider"> <providers> <add name="MemcachedCacheProvider" type="MemcachedProviders.Cache.MemcachedCacheProvider, MemcachedProviders" keySuffix="_MySuffix_" defaultExpireTime="2000"/> </providers> </cacheprovider>
Add following section to configure log4net
<log4net> <!-- Define some output appenders --> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionpattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!--<threshold value="OFF" />--> <!-- Setup the root category, add the appenders and set the default priority --> <root> <priority value="WARN"/> <appender -ref ref="ConsoleAppender"> <filter type="log4net.Filter.LevelRangeFilter"> <levelmin value="WARN"/> <levelmax value="FATAL"/> </filter> </appender> </root> </log4net>
Provided you include all the reference libraries, for example, this is my category controller, which will cache the object if it's not existed in the MemCached's memory.
1 2 3 4 5 6 7 8 9 10 11 12 | public ActionResult Category() { var returnObj = DistCache.Get<list <CategoryObject>>("fullCatListObject"); if (returnObj == null) { P015.Modules.Categories.Categories _cat = new P015.Modules.Categories.Categories(); returnObj = _cat.getCatObject(); DistCache.Add("fullCatListObject", returnObj, new TimeSpan(29, 0, 0, 0)); } return View(returnObj); } </list> |
One thing you need to remember when using this library is to have all your objects that you want to save in memcached serialized. For example, this is my CategoryObject class use for the above example:
CategoryObject.cs
[Serializable] public class CategoryObject { public string CAT_NAME { get; set; } public int CAT_ID { get; set; } public int LEVEL { get; set; } }
In case you are using Linq, or more specific, linq to sql, you can open your DBML file in design mode, change the Serialize Mode option to Unidirectional.
This works for me, so it should works for you too. Enjoy
Cache individual object in asp.net mvc
Upon writing a controller for exporting image on the fly, I struggled with the problem that: "Output cache does not cache HttpHeader (in this case, "Location" header)". Which lead me to despair since each time i called the controller to output image, it will have to connect to database and do a couple of query, which is not practical in real life. So finally, i found out that i can easily cache objects into a cache provider in Asp.net Mvc. Very useful, all my queries go back to 0 (traced through Ling2sql profiler).
You can use this piece of code in anywhere of your code where you want to cache any object.
(Notice: this code was used in a controller, so I used HttpContext.Cache. But if you use else where that make this code not working, try HttpContext.Current.Cache)
1 2 3 4 5 6 7 8 9 10 | if (HttpContext.Cache["ObjName"] != null) { link = (string)HttpContext.Cache["ObjName"]; return link; } else { HttpContext.Cache.Add("ObjName", link, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(30, 0, 0, 0, 0), System.Web.Caching.CacheItemPriority.High, null); return link; } |
What it did was to check if the ObjName exist in the context, if not, create one and dump the data into it. Remember when you call back from HttpContext, it's an object so you always need a cast. If you don't know about argument, let the intellisense do the work for you.
Time to exand further about the topic: HttpContext.Cache.Add
Builtin Async Controller in ASP.net MVC 2
Well, it's Christmas Eve at the time I post this post in my place. I hope you enjoy your Christmas with your family and happy new year to all of you who read this post of mine.
------------------------
One thing that make me feel more powerful when using ASP.net MVC is the newly introduce Asynchronous Controller Action.
Imagine, you have a front page with multiple widget, like news, weather, personal information, new forum thread etc... You are going to run it from top to bottom, query, query... Your basic solution for this is to query news first since it's kinda important, then weather, etc...
Why it's not a good solution? Because if you going to query everything like that, your program need to wait for each query to finish before continue to work with the code behind...
Better Solution? I could suggest you using Async Controller in this case. Separate each query into different Data Context, query to the database, Close Current Data Context, and process the info with code behind, separately and process at the same time. This helps you bring down processing time twice or thrice as fast than the normal approach...
Asynchronous Process has been introduce to ASP.net long before, but now, apply it to asp.net MVC even easier than what you really expect.
- Inherit to "AsyncController" instead of "Controller" class
- Write async method
- Write Complete method
- Write functions to handle async request
First step:
Look out for the head of your controller, you will always see this:
public class MyController : Controller
change it to:
public class MyController : AsyncController
Don't worry because everything will work just as normal even if you don't use AsyncController.
2nd Step, write the MethodAsync and MethodCompleted. The important is the prefix in this case, Async and Completed.
- Method is the action name, like Create, Delete, Details
- Async is in Void type, and everything request first will come to this void method, you can get the input from form, url here in this method. Async will pass parameter to Completed using AsyncManager.Parameters that you can see later in this article
- Completed is an ActionResult method. just like your normal Create, Delete action. You can return a View or RedirectResult here. The param of this method corresponding to the AsyncManager.Parameter that was used in MethodAsync
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | // Http://local/Controller/Method/ // As you can see i am using the attribute that can be use in the normal Actions [AcceptVerbs(HttpVerbs.Post), ActionName("Method")] public void MethodAsync(int id, string hello) { // This Increment() use to identify how many async process are there, // once everything is back to 0, MethodCompleted is fire-up AsyncManager.OutstandingOperations.Increment(); // This used to handle functions that are not Async Ready // since they don't have any async event ready. // using this you can start with any method. // the first int, string is the parameter data type, the last bool is the return type. // DoTestAsync is the method name that we going to write later on Func<int , string, bool> doTestHandler = DoTestAsync; // Begin async // ID, Hello is the param passing to method DoTestAsync doTestHandler.BeginInvoke(id, hello, ar => { var handler = (Func</int><int , string, bool>)ar.AsyncState; // AsyncManager.Parameters["returned"] now cantaining the result of // method DoTestAsync after finish the process. The datatype is bool. AsyncManager.Parameters["returned"] = handler.EndInvoke(ar); AsyncManager.OutstandingOperations.Decrement(); }, doTagHandler); } // This will process after the Async finished. The param of // this method is what we have used with AsyncManager.Parameters, // in this case, returned with type bool public ActionResult MethodCompleted(bool returned) { // You can actually make use of the param here to make some ViewData or something returned = !returned; // Since it's a ActionResult, i must return a View or something related. // return View(); -> will use the default view (Controller\Method.aspx) // i want to show some Content: return Content(returned.ToString()); } // Now we will write the method for our async controller public bool DoTestAsync(int a, string b) { // Everything you need to do with the param in here, // include query to your database or anything you can // posibly think of. For me, i will just return something. return true; } </int> |
The result of this is:
"False"
Isn't it simple, what you need is just to follow this and there you go, you can now have a Web application that run twice or thrice as fast.
Good luck...
Query a set of IDs from SQL using LINQ to SQL
Just not so long ago, I have a hard time while doing this in LINQ to SQL since i have my datacontext running on LINQ to SQL. I was force to do a N+1 querry, which is extremely expensive on both CPU and Time since i put my SQL Server else where that not in local and each query cost me 200ms in time.
My N+1 query was look like this:
SELECT * FROM Image WHERE IMG_ID = 1 SELECT * FROM Image WHERE IMG_ID = 3 SELECT * FROM Image WHERE IMG_ID = 7 SELECT * FROM Image WHERE IMG_ID = 9
however, later i found out a way to solve this by using "Contains" from IList to query "WHERE IN(x,x,x)".
Here is one function in my Repository:
1 2 3 4 5 6 | public List<Image> Images(IList<int> idList) { return (from d in _db.Images .Where<Image>(d => idList.Contains<int>(d.IMG_ID)) select d).ToList<Image>(); } |
or better:
1 | return _db.Images.Where<Image>(x => idList.Contains<int>(x.IMG_ID)).ToList<Image>(); |
these will output:
SELECT * FROM Image WHERE IMG_ID IN(1,3,7,9)
Convert latin accent to non-latin accent (Normalization Form)
I'm sure some of you have been tried to remove the special accent sign like "ắ ế í u ư đ" and replace it to a new string without accent for tagging, sluging, permalink purpose in your webpage...
this function use for asp.net / C# to replace everything to Normalization Form...
Tiếng việt:
Chắc hẳn một số bạn muốn thay các chữ có dấu tiếng vịêt thành chữ không dấu để lưu vào cơ sở dữ liệu, làm permalink hoặc một số chức năng khác, đây là function để thay thế toàn bộ chữ có dấu thành không dấu trong tiếng việt.
Chúc các bạn may mắn / Good luck
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | public static string LatinToAscii(string InString) { string newString = string.Empty, charString; char ch; int charsCopied; for (int i = 0; i < InString.Length; i++) { charString = InString.Substring(i, 1); charString = charString.Normalize(NormalizationForm.FormKD); // If the character doesn't decompose, leave it as-is if (charString.Length == 1) newString += charString; else { charsCopied = 0; for (int j = 0; j < charString.Length; j++) { ch = charString[j]; // If the char is 7-bit ASCII, add if (ch < 128) { newString += ch; charsCopied++; } } /* If we've decomposed non-ASCII, give it back * in its entirety, since we only mean to decompose * Latin chars. */ if (charsCopied == 0) newString += InString.Substring(i, 1); } } return newString.Replace('đ', 'd'); } |
Modify from source: http://www.codeproject.com/KB/cs/UnicodeNormalization.aspx
Adding CDN Functionality to ASP.net MVC
I bet everyone develop webpage today are most likely to use a CDN for deliver images, video. Unlike in rails, MVC does not have a way to link with CDN, so i write this extension for your HtmlHelper so that you can easily link to a CDN that preconfig in your Web.config
First, we need to add the ff. to web.config
<appsettings> <add key="cdn" value="cdn.ducdigital.com, cn.ducdigital.com" /> </appsettings>
and then use the ff class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public static class CDNHelperExtension { public static string CDNLink(this HtmlHelper html, string path) { string[] CDN = ConfigurationSettings.AppSettings["cdn"].Replace(" ", "").Split(','); Random r = new Random(); return CDNLink(html, path, r.Next(0, CDN.Count())); } public static string CDNLink(this HtmlHelper html, string path, int ServerNumber) { string[] CDN = ConfigurationSettings.AppSettings["cdn"].Replace(" ", "").Split(','); if (ServerNumber >= CDN.Count()) ServerNumber = 0; return "http://" + CDN[ServerNumber] + path; } } |
in your View, just call:
< %= Html.CDNLink("/images/") %>
Good luck!