Subsonic 是否有必要使用亚音速3简单存储库缓存延迟加载属性的数据?

Subsonic 是否有必要使用亚音速3简单存储库缓存延迟加载属性的数据?,subsonic,Subsonic,我在Customer类中添加了一个名为Orders的lazyloaded属性。您认为将数据缓存在私有字段中是否明智 private IList<Order> _orders; [SubSonicIgnore] public IList<Order> Orders { get { if (_orders == null) { var repository = new SimpleRepository(

我在Customer类中添加了一个名为Orders的lazyloaded属性。您认为将数据缓存在私有字段中是否明智

private IList<Order> _orders;

[SubSonicIgnore]
public IList<Order> Orders
{
    get
    {
        if (_orders == null)
        {
            var repository = new SimpleRepository("MyConnectionString", SimpleRepositoryOptions.None);
            _orders = repository.Find<Order>(x => x.CustomerId == this.CustomerId);
        }
        return _orders;
    }
}
私人IList\u订单;
[亚音速忽略]
公共秩序
{
得到
{
如果(_orders==null)
{
var repository=newsimplerepository(“MyConnectionString”,SimpleRepositoryOptions.None);
_orders=repository.Find(x=>x.CustomerId==this.CustomerId);
}
退货单;
}
}
还是最好不要这样缓存它:

[SubSonicIgnore]
public IList<Order> Orders
{
    get
    {
        var repository = new SimpleRepository("MyConnectionString", SimpleRepositoryOptions.None);
        return repository.Find<Order>(x => x.CustomerId == this.CustomerId);
    }
}
[亚音速忽略]
公共秩序
{
得到
{
var repository=newsimplerepository(“MyConnectionString”,SimpleRepositoryOptions.None);
返回repository.Find(x=>x.CustomerId==this.CustomerId);
}
}

我之所以这样问是因为我认为为了性能起见缓存数据是一个好主意,但同时我担心缓存数据可能会导致数据与从数据库中插入/删除记录的其他进程不同步。

在您的情况下,缓存的订单将在客户对象的生命周期内存在。如果需要清除缓存的订单,只需为客户重新查询即可。

如果我是您,我会添加一个名称指定存在缓存的附加属性,添加一个自定义cacheScope对象(如transactionScope,只要存在该范围,缓存就会存在),或者在文档中指定哪些属性将执行子对象的缓存以及缓存的时间。

我不会删除缓存。我会将其作为附加属性保留在那里。如果你需要它,你会得到它。

感谢您展示您的缓存逻辑。这是我的。在我的例子中,父对象的预期寿命很短,我不希望父对象/子对象的总数据超过100条记录,我希望所有子对象数据都会被使用。如果我的数据发生更改,那么我需要重新设计我在这个特定实例中使用的缓存逻辑:

private static List<HostHeader> _cachedHostHeaders;

public List<HostHeader> CachedHostHeaders
{
    get
    {
        if (_cachedHostHeaders == null)
            _cachedHostHeaders = this.HostHeaders.ToList();

        return _cachedHostHeaders.Where(i => i.SiteID == this.ID).ToList();
    }
}
私有静态列表\u cachedHostHeader;
公共列表CachedHostHeader
{
得到
{
if(_cachedHostHeaders==null)
_cachedHostHeaders=this.HostHeaders.ToList();
返回_cachedHostHeaders.Where(i=>i.SiteID==this.ID).ToList();
}
}