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
NVARCHAR(MAX) now…
Recently, I need to write a Trigger to duplicate and save a copy of revision automatically into the database, and at that time, i encoutered an error:
Cannot use text, ntext, or image columns in the ‘inserted’ and ‘deleted’ tables.
This cause me so much trouble since what else should i use if I am not allowed to use text/ntext? This make me wandering around google for a while, and finally there is a solution...
The NTEXT, TEXT, IMAGE is deprecated since 2005 and will be remove in the future release, maybe 2008R2 since MSSQL2008 still work fine with NTEXT, TEXT
- NTEXT -> replaced by NVARCHAR(MAX)
- TEXT -> replaced by VARCHAR(MAX)
- IMAGE -> replaced by VARBINARY(MAX)
And of course, for those who wondering, the maximum limit of VARCHAR is 8000 and NVARCHAR is 4000 (2 bytes for each unicode character) due to SQL Server uses 8KB page to store data to disk. But of course, Varchar(MAX), NVarchar(MAX) and VarBinary(MAX) data types in SQL Server 2005 which allows you to save upto 2GB in a single variable. The best part is that It allows you to use these data types as stored procedure parameters, internal variables etc.
So I suggest you should stop using NTEXT and TEXT and switch to NVARCHAR/VARCHAR soon!
Convert INT to VARCHAR in SQL (MSSQL)
At some point you will need to change INT to VARCHAR for the purpose of manipulation like substring or to display it in the status windows when execute of stored procedure or trigger...
here is the code which you can use to convert / cast the INT to VARCHAR:
CONVERT(TYPE, VALUE)
in this case ( my case ), this was used:
PRINT 'New revision updated for post id ' + CONVERT(VARCHAR(20), @intPID) + '!'
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)