0
votes

i try to use effective caching but i face to face a problem. For example; i have 5 user they have used my app. user1,2,3,4 only fill grid by searcing(Caching is run!!!). on the other hand user5 adding new row. i want to refresh my cach data when adding new row. i read Multi threading to do that

code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Collections;

namespace WebApp.Caching.Threading
{
    public partial class _Default : System.Web.UI.Page
    {

        protected void Page_Init(object sender, EventArgs e)
        {

            FillCache();

        }
        void FillCache()
        {
            using (var myCtx = new DataClasses1DataContext())
            {
                if (!(FlyAntCache.Exists("test")))
                {
                    List<Table_1> toolStoreList = myCtx.Table_1s.ToList();
                    FlyAntCache.Add(toolStoreList, "test");
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

                WaitCallback method1 = new WaitCallback(ControlAllChanging);
                bool isQueued = ThreadPool.QueueUserWorkItem(method1, new ManualResetEvent(false));

        }

        protected void ControlAllChanging(object state)
        {
            if (FlyAntCache.Exists("test"))
            {
                using (var myCtx = new DataClasses1DataContext())
                {
                    List<Table_1> list;
                    list = myCtx.Table_1s.ToList();
                    List<Table_1> listCache = FlyAntCache.Get<List<Table_1>>("test");
                    bool IsIntersect = list.Except(listCache).Count() > 0;

                    if (IsIntersect)
                    {
                        FlyAntCache.Clear("test");
                        FillCache();
                    }

                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            // Search
            using (var  myCtx = new DataClasses1DataContext())
            {
                var Qry = myCtx.Table_1s.
                                  FromCache<Table_1>("test").
                                  AsQueryable().Where(t => t.ad == TextBox1.Text.Trim());
                GridView1.DataSource = Qry;
                GridView1.DataBind();
            }

        }
     }
}

My Scenario:

LOOK please :http://i53.tinypic.com/20pdc41.png enter image description here

i really control if another user change my data, i must refresh my cache. is there any sensitivity to CAPTURE any new changing update new row save. for example : 1) i must capture new update . this mechanizm must run when changes occurs 2) i must capture new save. this mechanizm must run when new row adds

2

2 Answers

2
votes

I'm still not quite sure what you're asking. My best guess is it sounds like you're trying to let a cache know when its data is stale.

Most caching implementations have this built in. Basically, you can expire a cache item (usually be removing it from the cache) when it has been updated.

For example, if you're just using the plain old built in caching that comes with ASP.net:

private static Cache Cache;

public void AddItem(string data)
{
    //Do a database call to add the data

    //This will force clients to requery the source when GetItems is called again.
    Cache.Remove("test");  
}

public List<string> GetItems()
{
    //Attempt to get the data from cache
    List<string> data = Cache.Get("test") as string;

    //Check to see if we got it from cache
    if (data == null)
    {
        //We didn't get it from cache, so load it from 
        // wherever it comes from.
        data = "From database or something";

        //Put it in cache for the next user
        Cache["test"] = data;
    }

    return data;
}

UPDATE I updated the code sample to return a list of strings instead of just a string. This should make it more obvious what is happening.

To reiterate, the GetItems() call retrieves a list of strings. If that list is in cache, the cached list is returned. Otherwise, the list is retrieved and cached / returned.

The AddItem method explicitly removes the list from the cache, forcing the requery of the data source.

0
votes

Not sure but are you looking for events? You could set up events in your caching mechanism to fire when an update occurs.

Here is a googled example