Synclock on an object in the Cache

2019-08-29 02:27发布

Can I synclock effectively on an object stored in the cache?

Like:

SyncLock System.Web.HttpContext.Current.Cache("Some Object")
    'do some stuff that is threadsafe on this cached object'
End SyncLock

Will this stop another thread from modifying that cached object?

1条回答
做自己的国王
2楼-- · 2019-08-29 03:02

For the standard cache, yes. But it won't stop another thread replacing the object in the cache - you'll need to use a separate sync object for that. And if you happen to be using a distributed cache (e.g. Velocity) then it's not necessarily reliable either.

查看更多
登录 后发表回答