Results 1 to 3 of 3

Thread: Caching and algorithm

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Caching and algorithm

    im trying to come up with a way of being able to cache data and if an item does not exist, it will add it to the cache.
    The cache is slightly complicated in terms of data structure but I know this is what I want. I just cannot seem to think straight in terms of adding/getting data at each stage of a check.

    I am trying to hold:

    a full class Name (i.e AssemblyName.ClassName)
    each class name will have a collection where it will contain a propertyName for the key, which gives us another collection which contains a NamedArgumentInfo.Name and that returns me a NamedArgument.TypedValue.Value.

    This is what I will be using:

    Code:
    private static ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentDictionary<string, string>>> cachedData = new ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentDictionary<string, string>>> ();

    I want to cache some reflection data and I want to look at the cache to see if I have the data, if not, it should proceed to the next step to obtain the data from the source and then add it into the cache etc...

    How can I implement this, so it does not always reflect the generic type T but look in the cache to see if we have the values stored and SetProperty on T (the only bit of reflection to be used all the time really)?

    Code:
            private static T Populate<T>(IDataReader reader) where T : class, new()
            {
                T val = new T();
    
                if (reader != null && !reader.IsClosed)
                {
                        var propsWithColumnNameAttributes = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static).Where(prop => Attribute.IsDefined(prop, typeof(ColumnNameAttribute)) && prop.CanWrite && prop.GetSetMethod() != null);
    
                        foreach (var currentProperty in propsWithColumnNameAttributes)
                        {
                            foreach (var currentAttributeForProperty in currentProperty.GetCustomAttributesData())
                            {
                                string currentAttribParamValue = null;
                                foreach (var currentNamedArgument in currentAttributeForProperty.NamedArguments) 
                                {
                                    if (String.Equals(currentNamedArgument.MemberInfo.Name, ColumnNameAttribute.PropertyNames.DataColumnNamePropertyName, StringComparison.OrdinalIgnoreCase))
                                    {
                                        currentAttribParamValue = currentNamedArgument.TypedValue.Value == null ? null : currentNamedArgument.TypedValue.Value.ToString();
                                        
                                        if (reader.DoesFieldExist(currentAttribParamValue)) // DoesFieldExist is an extension method
                                        {
                                            var dbRecordValue = reader[currentAttribParamValue] == DBNull.Value ? null : reader[currentAttribParamValue];
    
                                            // set it in the property
                                            currentProperty.SetValue(val, dbRecordValue, null);
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }           
    
                return val;
            }

    For each of the for loops I have there, I guess we need to check to see if we have that data available in the cache and if so use it. But if it is not available then add it to the cache.
    Also if the data is found in the cache, then how can we dig deep and get the right data?

    Sounds complicated but not quite.


    Example background:

    you have a class.
    Class has several properties
    each property is decorated with multiple named parameter attributes.
    I want to obtain all the attributes of this type and find out the value for each attribute for that named parameter

    I want to be able to store these details in the cache so next time round the method is called, it should have all the data in the cache and I should be able to fetch it and SetValue with the name of that property and read the value from the DataReader and store it in that property.

    Makes sense?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Caching and algorithm

    I'm not quite sure from reading that what makes this different to a normal cache? Use Type as the type of TKey, and use typeof(T) to get the key value.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Caching and algorithm

    i meant in terms of an algorithm to use to store key information and not the entire bloated object in memory

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width