DataCache Class

Posts   
 
    
ChrisA
User
Posts: 3
Joined: 20-Jul-2005
# Posted on: 01-Aug-2005 07:48:23   

Trying to learn so I figured I'd ask for some feedback... I've built a DataCaching object for category/lookup table data that is used frequently in drop downs and menus... At first I was caching EntityCollections, but that seemed cumbersome... So I started making them into ArrayLists of strings.. The next thing I did was create a Hashtable to hold Category-SubCategory lookups... Lazy loads any relationship subLookups.. This can end up with strings repeating in different ArrayLists in the Hashtable, but it seems like its worth it with the lack of SQL calls...

I made the Class an application Singleton... Any call by an administrator to change data associated with the DataCache causes it to be disposed...

It seems to work, but I can't test it with multi-user traffic... Any blatant or not-so blatant problems I might be overlooking... (besides my complete mangling of naming conventions...)

I've been trying to add a timer to make the cache timeout after a period of no access, but that hasn't been very succesful yet...

Thanks... chris

    
public class DataCache
    {
        private ArrayList _category = new ArrayList();
        private ArrayList _subcategory = new ArrayList();
        private Hashtable _catSubCategory = new Hashtable();
        private static DataCache _instance;

        private DataCache()
        {
            CategoryCollection categories = new CategoryCollection();
            categories.GetMulti(null, 0, null);
            foreach (CategoryEntity cat in categoires)
            {
                _category.Add(cat.Category);
                        }
            categories= null;

            SubCategoryCollection subcats= new SubCategoryCollection();
            subcats.GetMulti(null, 0, null);
            foreach (SubCategoryEntity subcat in subcats)
            {
                _subcategory.Add(subcat.SubCategory);
            }
            subcats = null;
        }

        public static DataCache getDataCache()
        {
            if (_instance == null)
            {
                _instance = new DataCache();
            }
            return _instance;
        }

        public ArrayList getCategories()
        {
            return _category;
        }

        public ArrayList getCatSubCats(string category)
        {
            if (_catSubCategory[category] == null)
            {
                CategoryEntity myCategory = new CategoryEntity(category);
                SubCategoryCollection subcats = new SubCategoryCollection();
                subcats = myCategory.GetMultiSubCategoryCollectionViaProduct(true);
                ArrayList subcatList= new ArrayList();
                foreach (SubCategoryEntity subcat in subcats)
                {
                    subcatList.Add(subcat.SubCategory);
                }
                myCategory = null;
                _catSubCategory[category] = subcatList;
                return subcatList;
            }
            else
            {
                return (ArrayList)_catSubCategory[category];
            }
        }

        public static void Dispose()
        {
            _instance = null;
        }

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 01-Aug-2005 09:25:29   

Take a look at the Context class sourcecode, it contains an entity cache. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
ChrisA
User
Posts: 3
Joined: 20-Jul-2005
# Posted on: 01-Aug-2005 16:42:33   

Otis wrote:

Take a look at the Context class sourcecode, it contains an entity cache. simple_smile

I'm caching Collections though, but as ArrayLists... I'm not sure if you mean the .Net Context class, or some LLBLGen Context class (which I can't seem to find if there is one....)

I'm a little confused and a relative newbie, so clue me in a little more on what you are referencing so I can look into it more...

ChrisA
User
Posts: 3
Joined: 20-Jul-2005
# Posted on: 01-Aug-2005 18:02:22   

I'm also caching things as simple ArrayLists of strings... No entities or collections actually cached... Just for category type lookups...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 01-Aug-2005 21:53:09   

ChrisA wrote:

Otis wrote:

Take a look at the Context class sourcecode, it contains an entity cache. simple_smile

I'm caching Collections though, but as ArrayLists... I'm not sure if you mean the .Net Context class, or some LLBLGen Context class (which I can't seem to find if there is one....)

The LLBLGen Pro Context class, in the ORMSupport classes (introduced in 1.0.2004.2). It uses a double hashtable structure internally to cache entities, per type.

I'm a little confused and a relative newbie, so clue me in a little more on what you are referencing so I can look into it more...

Caching is in fact pretty simple. You have to have a store in which you store per key an object, which is then cached under that key and because you use a key system, it's very fast to retrieve a cached object again.

Collections don't have a unique object id, so identifying is not easy unless you use a system where you provide the key under which a collection is stored.

You then use internally a hashtable. For example, for caching all country entities, you could file that collection under 'Countries'.

Be aware that if you use your cache in a multi-threaded environment, multiple threads will get a reference to the same collection. It's therefore key that the data which is cached is only used read-only, including the collection itself.

Frans Bouma | Lead developer LLBLGen Pro