将对象部分存储在缓存中

时间:2015-02-18 06:44:30

标签: c#

我想只缓存一个对象的少数属性值。我也希望这可以以某种方式配置。我应该使用自定义属性来装饰属性并在存储缓存时解决这个问题吗?使用反射会很昂贵,因为缓存会很短暂。我仍然无法弄清楚如何使这个东西可配置。

这是我的代码

public static void GetCacheProperties<T>(T item) where T : new()
{
    var type = item.GetType();

    var properties = item.GetType().GetProperties();
    foreach (var property in properties)
    {
        if(property.GetCustomAttributes().Any(u=>u..GetType() == typeof(CustomCacheAttribute)))
        {
           var value = type.GetProperty(property.Name).GetValue(item, null);
        }
    }

   //Do I create a new object with just the retrieved property values to store in cache ?
}

0 个答案:

没有答案