Tag: .NET

Caching in .NET

In every application you have some data that is more read-more, write-once or twice. For example you can have the list of Cities of a country, the list of Countries of the world or list of exchange currency. This data is modified rarely. Also, you can have data that is not very sensitive to be real-time , such as the list of invoices for the day.
In .NET 3.5 you have several options
1. ASP.NET caching – and implementing in other applications with HttpRuntime  ( even if MS says “The Cache class is not intended for use outside of ASP.NET applications”)

2. Enterprise caching block : hard to configure

3. memcached , velocity, sharedcache and other third party providers – that comes with their options and configuration

In .NET 4.0 you have a new kid : objectcache and , more important , an implementation : MemoryCache

http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache%28v=VS.100%29.aspx

What is very good is that now you can cache in Memory what do you want – and apply easily to your Business Layer. More, the object is a singleton for the application – that is even better (see the test on the final of the post)

What it is missing is an easy implementation for List and an implementation to remove data after a defined time.

So I decided to do my implementation for that (ok, it is wrong to have both implementations in a single class – but you can separate easily )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Caching;

namespace CachingData{

    /// <summary>
    /// List<int> i = new List<int>() { 1, 10, 100 };
            //CacheData_List<List<int>, int> data = new CacheData_List<List<int>, int>(2);
            //data.Add(i);
            //Assert.AreEqual(3, data.Items().Count, "must be 3");
            //data = new CacheData_List<List<int>, int>();
            //Assert.AreEqual(3, data.Items().Count, "must be 3");
            //Assert.IsTrue(data.ContainsData, "must have data");
            //Thread.Sleep(1000 * 3);
            //Assert.IsFalse(data.ContainsData, "must not have data");
    /// </summary>
    /// <typeparam name="T">and generic ILIST </typeparam>
    /// <typeparam name="U"></typeparam>
    public class CacheData_List<T, U>
        where T:class,IList<U>, new()

    {
        /// <summary>
        /// use this for adding in cache
        /// </summary>
        public event EventHandler RemovedCacheItem;

        private System.Timers.Timer timerEvent;
        private MemoryCache buffer;
        private int TimeToLiveSeconds;

        private DateTimeOffset dto;
        private string Key;
        /// <summary>
        /// default constructor - cache 600 seconds = 10 minutes
        /// </summary>
        public CacheData_List()
            : this(600)
        {
        }
        /// <summary>
        /// constructor cache the mentioned TimeSeconds time
        /// </summary>
        /// <param name="TimeSeconds">value of time for cache</param>
        public CacheData_List(int TimeSeconds)
        {
            TimeToLiveSeconds = TimeSeconds;
            timerEvent=new System.Timers.Timer(TimeToLiveSeconds * 1000);
            timerEvent.AutoReset = true;
            timerEvent.Elapsed += new System.Timers.ElapsedEventHandler(timerEvent_Elapsed);
            dto = new DateTimeOffset(DateTime.UtcNow.AddSeconds(TimeToLiveSeconds));
            Key = typeof(T).ToString();
            buffer = MemoryCache.Default;
        }

        void timerEvent_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (RemovedCacheItem != null)
            {
                RemovedCacheItem(this, EventArgs.Empty);
            }
        }
        /// <summary>
        /// remove item from cache
        /// </summary>
        public void Remove()
        {
            if (buffer.Contains(Key))
            {
                buffer.Remove(Key);

            }
            dto=new DateTimeOffset(DateTime.UtcNow.AddSeconds(TimeToLiveSeconds));
        }
        /// <summary>
        /// add multiple items to cache
        /// </summary>
        /// <param name="items">items to add to the cache</param>
        public void Add(T items)
        {

            if (buffer.Contains(Key))
            {
                T data = Items();
                foreach (var t in data)
                {
                    items.Add(t);
                }
                buffer.Remove(Key);
            }
            buffer.Add(Key, items, dto);
        }
        /// <summary>
        /// add a item to the IList of the cache
        /// </summary>
        /// <param name="item">an item to add</param>
        public void AddItem(U item)
        {

            T data=new T();
            if (buffer.Contains(Key))
            {
                data = buffer.Get(Key) as T;
                buffer.Remove(Key);
            }

            data.Add(item);
            buffer.Add(Key, data,dto);
        }
        /// <summary>
        /// usefull if you do not intercept the removed event
        /// </summary>
        public bool ContainsData
        {
            get
            {
                return buffer.Contains(Key);
            }

        }
        /// <summary>
        /// retrieve items
        /// </summary>
        /// <returns></returns>
        public T Items()
        {
            if (!buffer.Contains(Key))
                return null;

            return buffer.Get(Key) as T;
        }
    }
}

Please note that the test for usage is in the summary :

</pre>
List i = new List() { 1, 10, 100 };
CacheData_List <List<int>, int> data = new CacheData_List<List<int>, int>(2);
data.Add(i);
Assert.AreEqual(3, data.Items().Count, "must be 3");
data = new CacheData_List<List<int>, int>();
Assert.AreEqual(3, data.Items().Count, "must be 3");
Assert.IsTrue(data.ContainsData, "must have data");
Thread.Sleep(1000 * 3);
Assert.IsFalse(data.ContainsData, "must not have data");

You can download the file from

http://msprogrammer.serviciipeweb.ro/wp-content/uploads/2010/05/CachingData.zip

(Last Note : for synchonization maybe it was better to lock on readerwriterlockslim

Intercept errors

Errors interception and rising

Description

We will speak here about the errors raised by external conditions (bugs) and the errors rose by the application.

There is not application without bugs. The bugs are not desired for an application – but programmers are human beings and, by consequence, they cannot verify every possible path that go an application to the failure. Maybe the space on hard is too little, other time the network between database and application is broken – there are many conditions to bring failure to an application. And you have to manage priority between

· repairing bugs

· evolving the application

· Resolving failure cases.

The errors raised by the application are usually logical errors, like the fact that a birthday date for a person cannot be tomorrow. Sometimes you will intercept errors from other components and raise your own error (like the fact that a component should have a unique name – you will intercept the index failure raised by your database and then raise your own error).

How to intercept error in applications

We will discuss here how and where to intercept errors. There are two principles that I guide my interception of errors:

1. Occam’s razor : in components(business layer, repository) intercept only the errors that you are sure to know what to do with (such as an ID is missing from a database). Usually here you can raise your own exception and let the GUI handle it. In

2. In GUI layer intercept all exception and , if possible, suggest to the user an alternative way to do.You must also log the error in order to can be retrieved later.

Please read the logging and instrumentation to see a common way to describe the path that a error has been flow through the application

Examples

First example is how to intercept an error

Second will be how to intercept and raise own errors

Third will be about intercepting errors in a Console application

1)We will first intercept an error and write a custom message for this error .

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data.SqlClient;

namespace exceptionintercept

{

class Program

{

static void Main(string[] args)

{

using (SqlConnection sc = new SqlConnection())

{

sc.ConnectionString = "Data Source=(local);Initial Catalog=adaad!@#$%^;Trusted_Connection=true";

try

{

sc.Open();

}

catch (SqlException ex)

{

if (ex.Number == 2)

{

Console.WriteLine("no database connection : “+ ex.Message);

return;

}

throw;

}

}

}

}

}

As you see we will intercept only the SqlException and put the custom error message only if error message number for the SqlException is 2 – no network access. Usually it is not a good idea to show your connection string in the output shown to the user.

Download project from http://msprogrammer.serviciipeweb.ro/wp-content/uploads/exception.zip

2) Intercept and raise error

We will do now a more advanced interception of connection : we will see if the machine that has the database answers to the ping. If yes, probably only the database is down , not the PC or the connection.

public void VerifyConnection(string MyConnection)

{

using (SqlConnection sc = new SqlConnection())

{

sc.ConnectionString = MyConnection;

try

{

sc.Open();

}

catch (SqlException ex)

{

//TODO : log the exception

Ping p = new Ping();

try

{

PingReply pr = p.Send(sc.DataSource);

}

catch (PingException)

{

//TODO : log the exception

throw new PCException(sc.DataSource, ex);

}

throw new BasicDatabaseException(sc.DataSource,ex);

}

}

}

3) Any application should intercept globally errors, in order to log / save state if there is something that works not well and it is not handled.

static void Main(string[] args)

{

System.AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

int i = 1;

i = 1 / (i - 1);

}

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)

{

//TODO : log

Console.WriteLine("from program : unexpected error occured " + e.ExceptionObject);

}

Download project from http://msprogrammer.serviciipeweb.ro/wp-content/uploads/exception.zip

TODO

intercept global errors in WinForms, ASP.NET , WPF, Silverlight application

Other resources

ELMAH – intercept ASP.NET errors , http://code.google.com/p/elmah/

Logging : http://msprogrammer.serviciipeweb.ro/2010/04/19/logging-and-instrumentation/

My programmer tools in 2009

you can download the PDF from http://msprogrammer.serviciipeweb.ro/wp-content/uploads/2010/03/tools.pdf

Tool

Description

Link

SharpZipLib Zip files programatically http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx
nunit test http://www.nunit.org/index.php
svn source control http://subversion.apache.org/
hudson continous integration http://hudson-ci.org/
EntityFramework sql server database to .NET code
filehelpers parse csv and other files http://www.filehelpers.com/
log4net logging http://logging.apache.org/log4net/
lumisoft parsing email messages http://www.codeproject.com/KB/vista/SMTP_POP3_IMAP_server.aspx
moq mocking in unit test http://code.google.com/p/moq/
automapper transferring data between dal and bll http://www.codeplex.com/AutoMapper
pagedlist use for paging in ASP.NET MVC http://pagedlist.codeplex.com/
log4postsharp logging every method with easy http://code.google.com/p/postsharp-user-plugins/wiki/Log4PostSharp
postsharp see log4postsharp, version 1.5 still free http://www.sharpcrafters.com/
webdeployment project to deploy web projects http://www.microsoft.com/downloads/details.aspx?FamilyID=0AA30AE8-C73B-4BDD-BB1B-FE697256C459&amp;displaylang=en&displaylang=en
IIS SEO Toolkit verify my sites http://www.iis.net/expand/SEOToolkit
nbehave testing with words http://nbehave.org/
reflector analyze code, others and mine http://www.red-gate.com/products/reflector/
fiddler analyze ajax requests http://www.fiddler2.com/fiddler2
build utilities from apache svn commit if different http://msbuildtasks.tigris.org/
selenium testing web interfaces http://seleniumhq.org/
HtmAgilityPack saving web pages http://htmlagilitypack.codeplex.com/

Utilities

Description

Link

notepad++ no notepad http://notepad-plus.sourceforge.net/uk/site.htm
paint graphic editor
paint.net graphic editor http://www.paint.net/
7zip free archiver-un-archiver http://www.7-zip.org/
sysinternals bunch of windows utilities http://technet.microsoft.com/en-us/sysinternals/default.aspx
windows live writer blog http://download.live.com/writer
psr.exe new method to create help
logparser parse fast files http://www.microsoft.com/downloads/details.aspx?FamilyID=890cd06b-abf8-4c25-91b2-f8d975cf8c07&displaylang=en
foxitreader pdf reader www.foxitsoftware.com/pdf/reader
yahoo instant messaging www.yahoo.com
skype instant messaging http://www.skype.com/intl/en/
dosbox old games redivivus http://www.dosbox.com/
vlc movie player http://www.videolan.org/vlc/
dvddecrypter and dvdshrink backup my movies http://www.mrbass.org/dvdrip/
magicdisk load iso files http://www.magiciso.com/tutorials/miso-magicdisc-overview.htm
freecommander norton commander http://www.freecommander.com/
winmerge compare files / folders http://winmerge.org/downloads/

Firefox addons

https://addons.mozilla.org/en-US/firefox/collection/ignatandrei

firebug
yahoo slow
webdeveloper
weave
morningcofee
measureit
htmlvalidator
faviconizetab
exchangebcebnr
colorzilla
addoncollector
googlenotebook
downloadstatusbar
goolepagespeed

As I was saying, you can download the PDF from http://msprogrammer.serviciipeweb.ro/wp-content/uploads/2010/03/tools.pdf