The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Data Caching in Entity Spaces Collections

Last post 02-17-2007, 5:56 PM by mfreidge. 4 replies.
Sort Posts: Previous Next
  •  02-06-2007, 10:24 AM 120

    Data Caching in Entity Spaces Collections

    Hello I'm currently using EntitySpaces version 1.5.3 and would like to add result caching to the classes (using the ASP.NET Cache object).

    I would like to have the custom entity spaces classes handle the data caching

    - so that I can call something like employeeCollection.LoadAll, and on the first use it queries the database, but on the 2nd attempt using a different instance of the class - it just retrieves the result from the ASP.NET Cache.

    I would the following forum entry, and would be very interested in any advice you can give: 

    http://www.entityspaces.net/phpbb2/viewtopic.php?t=19

    Many thanks

    Richard

    PS - glad to see the move to community server, and looking forward to the forum running faster.

    Filed under:
  •  02-06-2007, 1:11 PM 122 in reply to 120

    Re: Data Caching in Entity Spaces Collections

    You didn't ask but I just can't help myself. I'm not a big fan of caching data. It almost always leads to complicated issues and quite possibly data corruption, however, at a very fundamental level you could keep a static collection inside of the collection itself like this:

     

    Code:
    public partial class EmployeesCollection : esEmployeesCollection
    {
    	// Static contructor initializes our static instance s_coll
    	static EmployeesCollection()
    	{
    		// Force initialize it
    		s_coll.Query.Load();
    	}
    
    	// our static collection
    	private static EmployeesCollection s_coll = new EmployeesCollection();
    
    	// our static method to access into our static collection
    	static public Employees GetEmployee(int employeeId)
    	{
    		lock (s_coll)
    		{
    			Employees emp = s_coll.FindByPrimaryKey(employeeId);
    
    			if(null == emp)
    			{
    				// If we didn't find it load it
    				s_coll.QueryReset();
    				s_coll.Query.EmployeeID.Equal(employeeId);
    				if(s_coll.Query.Load())
    				{
    					emp = s_coll.FindByPrimaryKey(employeeId);
    				}
    			}
    
    			return emp;
    		}
    	}
    }

     

    And then you could use the code like this:

     

    Code:
    Employees emp1 = EmployeesCollection.GetEmployee(2);
    Employees emp2 = EmployeesCollection.GetEmployee(3);
    
    Employees empNew = new Employees();
    empNew.AddNew();
    empNew.FirstName = "Joe";
    empNew.LastName = "Shmoe";
    empNew.Save();
    
    Employees emp3 = EmployeesCollection.GetEmployee(empNew.EmployeeID.Value);

     

    However, I'm not really recommending this approach, just giving you ideas, EntitySpaces doesn't support caching at this point and it's not really on the Roadmap any longer, even the above code merely doesn't unique'ing it doesn't know when data has changed and therefore would not reflect changed data.

     


    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  02-06-2007, 5:25 PM 124 in reply to 122

    Re: Data Caching in Entity Spaces Collections

    Mike,

    I appreciate what you're saying about data caching, but doesn't caching have its place? I tend to cache frequently requested, low volume data where the data rarely changes (if at all, such as lists/enumerations/lookups). I wouldn't dream of caching frequently changing data but, personally, I think caching can be valuable if used sensibly/conservatively.


    Leigh
  •  02-07-2007, 2:03 AM 127 in reply to 122

    Re: Data Caching in Entity Spaces Collections

    Thanks for the quick response Mike.

    I think I can certainly use your suggestion for some of the lookups I'm using.

    To complicate matters a little further I now realise I don't wan't to cache the whole collection - only parts of it, and just to retrieve these parts from the cache when available.

    At the moment I've come up with the following (which appears to work fine), but I haven't been able to find documentation on OnQueryLoaded procedure.
    - I will need to manually clear the cache should any updates occur.

    Any comments on whether this approach is a good/bad idea would be appreciated. 

    Many thanks

    Richard 

    I've added the following custom load function to the Collection business object
     

            public bool Load(int CategoryID, int ObjectID)
            {
                string strKey = "CategoryCachePrefix_" + CategoryID + ":" + ObjectID;

                if (HttpContext.Current.Cache[strKey] != null)
                {       
                     return this.OnQueryLoaded((DataTable)HttpContext.Current.Cache[strKey]);
                }
                else
                {
                    Query.Select(Query.Name, Query.MoreFields1)
                    .Where
                    (
                        Query.CategoryID.Equal(CategoryID),
                        Query.ObjectID.Equal(ObjectID)
                    )
                    .OrderBy
                    (
                        Query.Name.Ascending
                    );
                   

                    if (Query.Load())
                    {
                        HttpContext.Current.Cache[strKey] = this.Table;
                        return true;

                    }

                     else

                        return false;
                  
                }

            }

    Filed under:
  •  02-17-2007, 5:56 PM 368 in reply to 127

    Re: Data Caching in Entity Spaces Collections

    I would suggest to create derived class, e.g CachedCategory to separate business layer and Cached functionality.
    Also it's better to use HttpRuntime.Cache instead of HttpContext.Current.Cache


    Michael Freidgeim
    Blog: http://geekswithblogs.net/mnf/
View as RSS news feed in XML