-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't Detach() a document already loaded for update #6648
Changes from all commits
534f675
a7e8ed2
c27ac3a
888a230
bbb962e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,22 +39,23 @@ public SessionHelper(ISession session) | |
} | ||
|
||
/// <inheritdocs/> | ||
public async Task<T> GetForCachingAsync<T>(Func<T> factory = null) where T : class, new() | ||
public async Task<(bool, T)> GetForCachingAsync<T>(Func<T> factory = null) where T : class, new() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So here, in the
Yes i agree, before the change it was already using a factory, would you mean I will think about it, but again i'm open to any suggestions, let me know |
||
{ | ||
if (_loaded.TryGetValue(typeof(T), out var loaded)) | ||
{ | ||
_session.Detach(loaded); | ||
// Return the already loaded document but indicating that it should not be cached. | ||
return (false, loaded as T); | ||
} | ||
|
||
var document = await _session.Query<T>().FirstOrDefaultAsync(); | ||
|
||
if (document != null) | ||
{ | ||
_session.Detach(document); | ||
return document; | ||
return (true, document); | ||
} | ||
|
||
return factory?.Invoke() ?? new T(); | ||
return (true, factory?.Invoke() ?? new T()); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the "caching" information should not leak at this level. It should always be either cacheable or not. Another solution is to change this method to two:
And the user would check if Get returned null before calling Create. Right now it's more like a GetOrCreate() and the cachability of the result will vary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, i'm open to any suggestions
That said, this is no more the case in the
distributedCache
branch, it is done at theIDocumentStore
level and only checked at theIDocumentManager<>
level, then any service using anIDocumentManager<>
don't have to care about this.Yes i agree but this is the singularity i described in my comments. In some rare cases the caller should not create a new instance but should not cache the returned one. But this only happens in a given scope where we can't refresh the cache for a document that has already been loaded for update and that may have been flushed.
Yes exactly, before it was already a
GetOrCreate()
with a factory, but, because of the above, even the result is not null it doesn't mean that it can be cachedI will think about it