Tuesday, December 17, 2013

Threading to JavaScript 1

JavaScript is a single-threaded environment. Simply if you wants run multiple scripts at the same time you cant achieve that requirement with JavaScript. Think about the scenario in a web site you wants to retrieve data through AJAX, needs to do manipulation in UI and DOM at the same time. But you can do this because of script execution happens within a single thread. So in currently what you do normally achieve this kind of goal. You use techniques like setTimeout(), setInterval(), XMLHttpRequest, and event handlers. But in HTML5 gives to you better option than these workarounds. That is Web Worker.

A web worker is a JavaScript that runs in the background, independently of other scripts, without affecting the performance of the page. perfect for keeping your UI refresh, performant, and responsive for users. There are two types of web workers, Dedicated Workers and Shared Workers. In this article discuss about Dedicated web worker.

Let's do small demo about web worker. Create HTML content like below with two buttons. Those two buttons bind with 'onClick' event.

Web worker demo

Create script like below with two onClick events. First of all check whether browser support web worker. After check Worker object create or not. If it not create new object. If the web worker file exists, the browser will spawn a new worker thread, which is downloaded asynchronously. The worker will not begin until the file has completely downloaded and executed. If file not exists returns an 404, the worker will fail silently. Communication between a work and its parent page is done using an event model and the postMessage() method.


Following script shows the content of doWebWorker.js file. In this javascript file capture message from main page send data back to main page. And also worker terminate operation contain in this separate script file.
var message;
that = this;
self.addEventListener('message', function (e) {  
 //basic demo
 message = e.data;
 that.sendMessage(message);
}, false);

function sendMessage(message)
{
 self.postMessage(message);
}

When click Start Worker button send message to the web worker and eventlistener of web worker catch it send back to the main page. When click Stop Worker button terminate web worker.



Tuesday, December 10, 2013

Caching in ASP.NET MVC : What, Why and How

What is Caching in ASP.NET MVC ?

Caching is process of storing frequently used information and within high speed memory.


Why do we need caching in ASP.NET MVC ?

Most of the times ASP.NET application requirements makes the scenario to get user and page data from the server in all most every request. Because of this kind of accessing data from server each time directly effect the application performance. To avoid this kind performance and latency issues we can use Caching mechanism.

How do we manage caching in ASP.NET MVC ?

Page Output Caching
- In this method browser cache HTTP GET request for predefined time period. When user request same URL again that call not going to server, instead of that it returns already cache page. Following code snippet show the way of doing it.

[OutputCache(Duration=60, VaryByParam="None", Location="ServerAndClient")] Public ActionResult Index() { return View("Index"); }

Duration defined the time period caching should happen. Setting by location says where we wants to do the caching. In this code it done by both of server and client. Because of that user who request this url from another browser also not send the request to the server. We can defined place to locate the cache. VaryByParam create the different version of cache based on the form or query string parameters. You can create cache profile in the web config and use it in with controller.

Donut and Donut Hole Caching
- Donut and Donut hole caching also belongs to the page caching mechanism. But these two methods not cache full page like in output cache. Donut cache is the server side methodology and it cache entire page other than pieces of dynamic content. Donut hole cache do the opposite of it cache selected sections of the page and not cache entire remaining part of the page.

Windows App Fabric Caching Service
- In web farm solution implementing the caching mechanism getting more complex because of each server share the information created in one sever. To solve this kind of scenario use windows app fabric caching service. ASP.NET is the cache client. Cache client can hold the cache in it local store and communicate with cache cluster. When request the data first look in the local store then it look for the cache cluster. If cache cluster not contain that information looking for then it has go to original information source and get it.  

Data Caching 
- In your business application most of the time request same data frequently. This process directly effect to the performance of the application. Data caching is the solution for this kind scenario. Data caching can apply to the layers between data access and business logic. In between these two layers can cache the frequently request data then each time no need to make database request. Data caching use ObjectCache  and MemoryCache objects for the implementation. 

Application Caching 
- In this method use Application Cache API (AppCache) in HTML5. This give access to the local browser cache. To use application cache needs to create application cache manifest, reference manifest and transfer manifest to the client. When application cache enabled browser fetch it only three cases,
a). When makes the any changes to the manifest
b). When clear the cache by user
c). When cache updated by programmatically