可以在存储库中使用 spring.net 更新部分缓存吗?

发布于 2025-01-03 18:51:51 字数 793 浏览 3 评论 0原文

我正在使用 spring.net 构建一个带有缓存的存储库。我可以更新/添加/删除缓存列表中的一项,而无需重建整个列表吗?

从他们的网站查看文档和示例项目,每当更新/添加/删除一项时,他们总是会清除缓存。因此,只要您只读取一个对象或对象列表,缓存就可以很好地工作,但是仅仅因为我更改了一项就必须重建整个缓存感觉很愚蠢?

例子:

// Cache per item and a list of items
[CacheResult("DefaultCache", "'AllMovies'", TimeToLive = "2m")]
[CacheResultItems("DefaultCache", "'Movie-' + ID")]
public IEnumerable<Movie> FindAll()
{
    return movies.Values;
}

// Update or add an item invalidating the list of objects
[InvalidateCache("DefaultCache", Keys = "'AllMovies'")]
public void Save([CacheParameter("DefaultCache", "'Movie-' + ID")]Movie movie)
{
    if (this.movies.ContainsKey(movie.ID))
    {
        this.movies[movie.ID] = movie;
    }
    else
    {
        this.movies.Add(movie.ID, movie);
    }
}

I'm building a repository with caching using spring.net. Can I update/add/delete one item in the cached list without having to rebuild the whole list?

Looking at the documentation and the example project from their site they always clear the cache whenever they update/add/delete one item. Therefore as long as you only read an object or the list of objects the caching works well but it feels stupid having to rebuild the whole cache just because I change one item?

Example:

// Cache per item and a list of items
[CacheResult("DefaultCache", "'AllMovies'", TimeToLive = "2m")]
[CacheResultItems("DefaultCache", "'Movie-' + ID")]
public IEnumerable<Movie> FindAll()
{
    return movies.Values;
}

// Update or add an item invalidating the list of objects
[InvalidateCache("DefaultCache", Keys = "'AllMovies'")]
public void Save([CacheParameter("DefaultCache", "'Movie-' + ID")]Movie movie)
{
    if (this.movies.ContainsKey(movie.ID))
    {
        this.movies[movie.ID] = movie;
    }
    else
    {
        this.movies.Add(movie.ID, movie);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚然 2025-01-10 18:51:52

在我看来,将可变的东西存储在缓存中似乎会带来可怕的副作用。恕我直言,如果您想从缓存列表中添加/删除条目,这就是您所需要的。

CacheResultAdviceInvalidateCacheAdvice 的实现允许存储和无效对象(键)-> 对象(值)组合。您可以添加另一个层并检索每部电影的电影,但我认为这只是过早优化的情况(具有相反的效果)。

CacheResultAdvice

InvalidateCacheAdvice

编辑:
顺便提一句。如果您使用成熟的 ORM 寻找集成的二级缓存,如果您想避免访问数据库服务器: http://www.klopfenstein.net/lorenz.aspx/using-syscache-as-secondary-cache-in-nhibernate

Having mutable things stored in the cache seems to me a fountain of horrible side effects. Imho that is what you would need if you want to add/remove entries from a cached list.

The implementation of CacheResultAdvice and InvalidateCacheAdvice allows to store and invalidate an object (key) -> object (value) combination. You could add another layer and retrieve movie per movie but I think that it is just a case of premature optimization (with the opposite effect).

CacheResultAdvice

InvalidateCacheAdvice

Edit:
Btw. if you use a mature ORM look for integrated level2 caching, if you want to avoid hitting the db server: http://www.klopfenstein.net/lorenz.aspx/using-syscache-as-secondary-cache-in-nhibernate

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文