I'm trying to write a function which will cache objects in memory.
The function below works, but performance is unbearable. What would you recommend to improve performance?
public void SerializeInsertCache(object item)
{
//Get type of item object item
Type type = item.GetType();
//Get all of attributes type of item object
TypeAttributes attributesType = type.Attributes;
//Get object type full name for cach key
string rootName = type.Name;
string name = type.Name;
PropertyInfo[] properties = type.GetProperties();
PropertyInfo idRoot = properties.Where(p => p.Name == "Id").SingleOrDefault();
int rootId = (int)idRoot.GetValue(item);
string rootKey = "";
foreach (var property in properties)
{
string propertyName = property.Name;
var propertyValue = property.GetValue(item);
Type propertyType = property.PropertyType;
string propertyTypeName = propertyType.Name;
bool cacheStatus = true;
SerializeInsertCache(property);//recursive function
}
SaveInCache(item);
}
propertyName
,propertyValue
,propertyType
,propertyTypeName
&cacheStatus
are assigned to but never used; get rid of them and you'll get some performance gain. \$\endgroup\$Dictionary<string, object>()
, and retrieve with a(ExpectedType)cache.Get("key_name")
. I may be way off the mark though which is why this is a comment. \$\endgroup\$item
in to theSaveInCache
method, and discard everything you reflected. \$\endgroup\$