문제

For a website I have in my azure cloud and I work on, I need to implement a "hybrid" cache.

Meaning, my website displays a table of records from a certain db, and provides an option to either add new records or update existing ones. For the reads, I need to implement in-process cache, and for the writes (adding, updates) I need to have out-of-process cache.

I'm pretty new at C# and azure. I'll be glad to get some help to begin with...

Currently, I use simple sql commands to display, add or update:

protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
                GridView1.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
        {
            string InsertCommand = "INSERT INTO [Students] ([Student_Name], [GPA]) VALUES (@Student_Name, @GPA)";
            string connString = SqlAzureDataSource.ConnectionString;
            using (SqlConnection conn = new SqlConnection(connString))
            {
                using (SqlCommand comm = new SqlCommand())
                {
                    comm.Connection = conn;
                    comm.CommandText = InsertCommand;
                    comm.Parameters.AddWithValue("@Student_Name", TextBox1.Text);
                    comm.Parameters.AddWithValue("@GPA", TextBox2.Text);

                    try
                    {
                        conn.Open();
                        comm.ExecuteNonQuery();
                        this.DataBind();
                    }
                    catch (SqlException ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                    }
                }
            }
        }
// Similar for UPDATE

I'll appreciate any help given

Thanks

도움이 되었습니까?

해결책

Link on how to use Azure caching. Here is a link on Windows Server AppFabric

Architecturally, the caching will have to be implemented a level above the SQL Query.

Btw, If you are new to C# and are developing a web based app may I suggest you look at Entity Framework, a nice MVVM pattern and maybe use ASP MVC. Entity framework alone will save you a lot of time and the code will be much more maintainable.

EDIT

The right way to add items to a cache - Here is an example. Let us assume you have a table called Employees. You can create a dataset and fill in the results of your query into that dataset. You can save that in the cache.

Again, please see Entity framework which will allow you do this with ease. Building on the above example, EF will easily allow you to create a class Employee in C# and and fill in the results of your query into that class.

The cache code in both cases will sit on top of the SQL Query. So create a separate class with GetEmployeeDetails() as a method. Issue a call to getEmployeeDetails() which hits the cache to see if the data exists. If not, it hits the SQL.

Move out everything from page_load to their own classes.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top